Question

For this question you need to create a function in Octave called MyFunction that accepts two...

For this question you need to create a function in Octave called MyFunction that accepts two inputs, a list L and an integer K, and returns two values minsum and maxsum. A template function including the function prototype (first line) has already been created for you to use (do not change this). I will only mark the contents of the MyFunction.m file. Any code added to the main.m file is for your testing only and will not be marked.

Your function needs to calculate the minimum and maximum values that can be obtained from adding any K values in the list of numbers (without repetition) and return these in minsum and maxsum respectively.

You may not use the built-in sort function and the word "sort" may not appear anywhere in your function (including comments). If the marking script finds the word sort anywhere in your code, you will get 0 for this question.

Example input:

L = [1, 2, 3, 4, 5]

K = 3

Example output:

minsum = 6

maxsum = 12

In the above example, minsum = 6 as the result of adding the three numbers 1, 2 and 3, maxsum = 12 as the result of adding the three numbers 3, 4 and 5.

Requested files

main.m

    1 % DO NOT MODIFY THIS FILE
    2 % ADD ALL YOUR CODE IN THE MyFunction.m FILE
    3 
    4 [minsum, maxsum] = MyFunction([1,2,3,4,5], 3)

MyFunction.m

    1 function [minsum, maxsum] = MyFunction(L, K)
    2 
    3 
    4 end
0 0
Add a comment Improve this question Transcribed image text
Answer #1

MATLAB code to calculate minsum and maxsum of a list

Language : MATLAB

Code:

%MyFunction to calculate minsum and maxsum
function [minsum, maxsum] = MyFunction(L, K)
%initializing empty list
B = []
%sorting list L and adding the elements in list B
while ~isempty(L)
indmin = (L==min(L))
B = [B,L(indmin)]
L(indmin) = []
end
%initializing minsum and maxsum to 0
maxsum=0
minsum=0
%for loop to calculate minsum
for i=1:K
minsum+=B(i)
end
%for loop to calculate maxsum
for s=length(B):-1:length(B)-(K-1)
maxsum+=B(s)
end
end

Code Screenshot:

MyFunction.m RUN 4 8 9 1 %MyFunction to calculate minsum and maxsum 2 function [minsum, maxsum] = MyFunction(L, K) 3 %initial

Output:

octave:9> source(MyFunction.m) L = 4 2 5 3 1 = 6 minsum maxsum [2

Add a comment
Know the answer?
Add Answer to:
For this question you need to create a function in Octave called MyFunction that accepts two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • use MatLab to answer these questions 1. (10 points) Create an m-file called addup.m Use a...

    use MatLab to answer these questions 1. (10 points) Create an m-file called addup.m Use a for loop with k = 1 to 8 to sum the terms in this sequence: x(k) = 1/3 Before the loop set sumx = 0 Then add each term to sumx inside the loop. (You do not need to store the individual values of the sequence; it is sufficient to add each term to the sum.) After the loop, display sumx with either disp()...

  • Write a C++ code based this question n this assignment you will create three additional functions...

    Write a C++ code based this question n this assignment you will create three additional functions for each one of the data structures created in class. You will be provided with a header file which you will modify and a demo program for each data structure. If your functions are implemented correctly the demo programs should compile and execute correctly. Part 0 (Comments) 1. Over the course of the semester we have discussed comments for each data structure. Please add...

  • Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys...

    Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys that are found in both dictionaries and returns a new dictionary that contains key:value pairs where the key in the new dictionary is the key found in both dictionaries, dictionl and diction2, and the new key's value being a list of the values of dictionl.key and diction2.key values concatenated together. Assumptions 1) 2) Both dictionaries can be empty and an empty dictionary is returned...

  • Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that...

    Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input a list of numbers and returns a list of the first sequence within that is increasing order, and has a length of at least 2. If no increasing sequential numbers are found, (ie. the input list is in descending order) then naturally a list of just the first value is returned. Increasing sequence means for a number to be valid it...

  • Homework 4 – Create your own Function Create your own function in C that accepts one...

    Homework 4 – Create your own Function Create your own function in C that accepts one input number and returns a double number. The themes for the functions should be one of the following:  Calculates 3.14 times of the square of input number. For example, if 2 is input then 12.56 should be returned. (e.g. 3.14*2*2 = 12.56)  Divides the number by 3 and returns the result. For example, if 6 was input then 2.0 should be returned....

  • l Question 2 -JS and Python a) Write JavaScript code to create a function called "sortstingsDesc"...

    l Question 2 -JS and Python a) Write JavaScript code to create a function called "sortstingsDesc" that will sort an array of string suppiied as a parameter in descending order. (4) Write Python code to create a function called "addToFront" that will accept two parameters the list of information and the value to be added and place the value to the beginning of the list. [4) b)

  • Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified...

    Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified letter in a string. This function has to be recursive; you may not use loops!   CodeRunner has been set to search for keywords in your answer that might indicate the use of loops, so please avoid them. For example: Test Result text = 'welcome' letter = 'e' result = freq_of(letter, text) print(f'{text} : {result} {letter}') welcome : 2 e and A list can be...

  • python Define a function called print_values which takes a dictionary object as a parameter. The function...

    python Define a function called print_values which takes a dictionary object as a parameter. The function should print all values in the dictionary. However, the order is based on the sorted keys in the dictionary. For example, if we have the following dictionary: {'b':36, 'a':12, 'c':24} The output is: 12 36 24 A faulty solution has been provided below. Identify the fault and submit a corrected version of this code. def print_values(dict1): for k in list(dict1.keys()).sort(): print(dict1[k], end=" ") For...

  • 3. In the main function, pthread create is called to create four child threads. A data file is divided into four blocks...

    3. In the main function, pthread create is called to create four child threads. A data file is divided into four blocks. Each child thread handles one block hild . After all four child threads complete, the main thread print out a final result. Write pseudocode to solve the above program. In your code, OVERLOOK the detail of opening a file, divide a file or handle a block. For example, use the comment //open a file to indicate the action...

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT