0

I have a code that is supposed to do the following:

The first function called on, named CalcNum1.m, will find the sum of all the values in any sized array using a for loop instead of the sum function. This sum divided by 2 will be saved to the variable Num1. Finally, in the second function named PrintTerms.m, reorder your terms using the built in sort function. Now, find how many terms (starting with the first and the smallest), when adding upon one another, are necessary to surpass the value of Num1. Print to the user how many terms are necessary.

Here is my code for the main script:

B = input ('Enter matrix B');
[Num1,sum] = CalcNum1(B);
[Q] = PrintTerms(B, Num1);

And here is my code for the functions

function [sum, Num1] = CalcNum1(B)

n = numel(B);
sum1 =0;
for i = 1:n
    sum1 = sum1 + B(i);

end
sum = sum1;
Num1 = sum/2;
end




function [Q] = PrintTerms( B, Num1 )

sort (B)
sum1 = 0;
i = 0;
count = 0;
while sum1<=Num1
    i = i+1
    sum1 = sum1 + B(i)
    count = count+1
end  
 Q = count;
 sum1
    fprintf(' This many terms necessary %.2f',Q)
end
user2928537
  • 31
  • 2
  • 8
  • So what's the question? And: is this homework? – Luis Mendo Nov 21 '13 at 21:54
  • Although you are told to not use `sum`, you should never overload such functions (unless you actually want to create a function the does all the stuff `sum` does, adding some additional feature (but then you would have to be *very* skilled)), as it will create a lot of confusion if you forget to clear the variable... And someone on this forum will complain about the use of [`i` as a variable name in MATLAB](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab). – Stewie Griffin Nov 21 '13 at 22:13
  • Question is second part of function not working properly. I m trying to debug, but haven't quite got it. Not homework. Just extra problems. I'm prepping for a test I have in an hour. – user2928537 Nov 21 '13 at 22:24
  • @user2928537 Good luck with the test! – Luis Mendo Nov 21 '13 at 22:36

2 Answers2

0

Try:

function [Num1, my_sum] = CalcNum1(B)

n = numel(B);
my_sum =0;
for i = 1:n
   my_sum = my_sum + B(i);
end

Num1 = my_sum/2;
end

Note that I switched the output order, as you call it [Num1,sum] =

function count = PrintTerms( B, Num1 ) % No need for brackets when only single output

B = sort(B);  % Need to save it in a new (or the same variable)
              % sort(B) will only print the sorted vector
my_sum = 0;
ii = 0;       
count = 0; 
while my_sum <= Num1
    ii = ii+1;                  % Use semicolons to supress output
    my_sum = my_sum + B(ii);   
    count = count + 1;          % Use space for better readability 
end  

fprintf(' This many terms necessary %.2f \n', count)
% Include \n to get a line shift
end

Use brackets when you type in B, like this: [1 2 3 4 2 3 4 23 12 32 12 2 3 6]

If you want it simpler, you could also use cumsum and avoid the loops. This is your entire last function (except the print part):

vec_sum = cumsum(sort(B));        % See documentation for explanation
count = find(vec_sum >= Num1,1) ; % the second input 1, is to only include the 
                                  % first element larger than Num1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
0

Your PrintTerms function seems to work well. As possible problem is that, asuming Num1 is an arbitrary value, if it's so large that it's not reached even with all elements of B, you will try to pull a non-existent further element from B. To avoid that you can add a condition with if and a flag result to indicate whether you had success or not:

% [...]
success = 1; % for now
while (sum1 <= Num1)
    ii = ii+1;                  % Use semicolons to supress output
    if ii>length(B)
        success = 0; % we ran out of terms
        break; % exit while loop
    end
    sum1 = sum1 + B(ii);   
    count = count + 1;          % Use space for better readability 
end
if success
    fprintf(' This many terms necessary %.2f', count)
else
    fprintf(' The sum cannot be reached')
end

Also, note that sort(B) does not store the result. Use B = sort(B);

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147