How can i solve this exam practise question

5 Comments

Show effort to get help.
Upload code as text.
This time I edited your post for you. Next time, please use the tools explained on this page to make your question more readable.
Where did the second input to your function go? And why is your end statement missing?
As a side-note: it may be very difficult to complete assignments like this on a mobile device.
(original question and comments by Comfort Chauke (the name of OP before it was changed to Wilson) recovered from Google cache)
Comfort Chauke on 11 Jun 2020 at 9:39:
How can i solve this exam practise question
Comfort Chauke on 11 Jun 2020 at 10:33:
function [minsum, maxsum]=MyFunction(L)
sum=0;
for i=1:length (L)
sum=sum+L(i);
end
min=L(1);
max=L(1);
for i=1:length (L)
if L(i)<min
min=L(i);
end
if L(i)>max
max=L(i);
end
end
minsum=sum-max
maxsum=sum-min
Comfort Chauke on 11 Jun 2020 at 12:03:
That's my problem i don't know how make calculate the minimum and maximum values that can be obtained from adding K elements
(Answers Dev) Restored edit

Sign in to comment.

 Accepted Answer

Rik
Rik on 11 Jun 2020
Since you aren't allowed to use efficient tools (e.g. sort), you are forced to write slow code. I would not try to use eval to cheat here, although I doubt the grader will detect that, especially if you encapsulate it in str2func.
BUT: you are in luck. You were given the task to get the numbers without repitition. The unique function will automatically sort the output for you.

16 Comments

So how can i modify my code so that it gives me the correct results?
Have you read the documentation for the unique function? You should look up the Matlab documentation, not that of Octave.
I'm not going to give you a fully working example, because that would encourage you to cheat.
You need to convert the input array to a sorted list of unique values. Then you need to calculate the sum of the first K and last K values. What code would you write that does that? Try something. If it doesn't work, post your code here and I may be able to give more specific guidance.
for j = 1:K
A=A+X(j);
end
After that loop, j will be left set to K.
for i = X(j):X(j+2)
B=B+X(i);
end
You are pulling out the data values starting from the K'th (in increasing order), and the K+2'nd, and constructing a vector of values from those. For example if the values in increasing order were 1 2 4 8 15 and K were 3, then you would pick out the 3rd element (4) and the 5th element (15) and construct the vector 4:15 .
Then you would try to use those values to index X in creating the sum B.
This is not what you should be doing. You should be letting i run from the K'th-last value to the last value. In the example with K = 3 and those X values, then you would be wanting to add the 4, 8, and 15 -- which are index 3, 4, 5, the 3 last values.
If K is one, K:length(X) will return length(X) elements, not K.
I really cannot seem to get it right
How would you explain in words what you need?
I need an algorithm that can calculate the max-min sum obtained by adding K values in L array
E.g if L=1 2 3 4 5 , K=2 then the max sum would be 4+5=9 and min sum would be 1+2=3
if K=5 the max sum would equal the min sum which is 1+2+3+4+5=15
So for K=2, your need the sum of the elements with index 4 and 5. How can you determine these values given K and the length of L? What is the first index you need to use and what is the last index you need to use?
Comfort
Comfort on 12 Jun 2020
Edited: Comfort on 12 Jun 2020
for K=2 it will be K+2:length(L)
Let's test that:
K=2;L=rand(1,8);
K+2:length(L) %should return [7 8]
ans =
4 5 6 7 8
So no, that is not correct.
Go back a step. What are we trying to do? We want to select only some indices. What defines those indices? Well, we want to have the last K indices. So what then defines the starting value of our index vector? The calculation must involve the length of your input vector.
So anything that doesn't have K and length(L) (or numel(L)) in it before the colon can't be the correct solution.
What about length(L) - (K-1):length(L)
That is indeed the correct code, although I would slightly change the parentheses to make the steps a bit more clear:
( length(L)-K+1 ):length(L)
And now you have the indices you can also try to eliminate the loop and use an array operation to add up all relevant values.
Use semicolons inside your code to suppress all outputs. The printing should be done in the calling function. So then you can use [minsum, maxsum]=MyFunction([1,2,3,4,5],3) and it will print those results.
Alright....Thank you so much
You're welcome. If you feel I have solved your question, please consider marking my answer as accepted answer. If not, feel free to comment with your remaining issues.

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 11 Jun 2020
Edited: madhan ravi on 11 Jun 2020
doc mink
doc maxk
doc sum

2 Comments

Since this question is supposed to be run in Octave, the students are forced to use other functions, since mink and maxk were introduced in R2017b, but aren't implemented in Octave.
Aha thanks Rik , didn't know that.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 11 Jun 2020

Commented:

on 12 Oct 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!