In: Computer Science
Matlab question:
By using while loop to solve, Sum up bottles until you are over 75. How many days/loops are needed? Print out cups on the shelf and total until you reach at least 75.
Finish the code below,
least_num_Cups = randi([75, 300], 1,1);
while
totalCups = % Change this code to reflect change in totalCups
fprintf('%0.0f cups of tea on the shelf, total %0.0f\n',);
k = ; % Change code to update k
end
k_complete = ; % Change variable to determine k value when totalCups requirement is met
Make the output looks like:
1 cups of tea on the shelf, total 1
2 cups of tea on the shelf, total 3
3 cups of tea on the shelf, total 6
4 cups of tea on the shelf, total 10
5 cups of tea on the shelf, total 15
6 cups of tea on the shelf, total 21
7 cups of tea on the shelf, total 28
8 cups of tea on the shelf, total 36
9 cups of tea on the shelf, total 45
10 cups of tea on the shelf, total 55
11 cups of tea on the shelf, total 66
12 cups of tea on the shelf, total 78
Total number of cups is: 78 after 12 loops
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!
===========================================================================
least_num_Cups = randi([75, 300], 1,1);
k = 1;
days = 0;
totalCups = 0;
while totalCups<75
totalCups = totalCups+k;% Change this code to reflect change
in totalCups
days+=1;
fprintf('%0.0f cups of tea on the shelf, total
%0.0f\n',days,totalCups);
k = k+1; % Change code to update k
end
k_complete = totalCups; % Change variable to determine k value when
totalCups requirement is met
printf("Total number of cups is: %d, after %d loops\n",k_complete,days);