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