In: Computer Science
PLEASE ANSWER IN MATLAB
Problem 2
Continuation of HW2, Problem 2: Total Solar Eclipse (TSE) budgeting.
This problem will help your program be more usable and flexible. Apply the following improvements to your code so it does the following:
Test your code using these parameters:
How many eclipse events can you choose to attend out of the next 6 upcoming eclipses?
Self check: the output should look like this:
With a required budget of $4000, you can attend the following total solar eclipse(s):
A total solar eclipse that occurs at 10xx days with a savings balance of $4x1x
A total solar eclipse that occurs at 14xx days with a savings balance of $5xx6
A total solar eclipse that occurs at 1xxx days with a savings balance of $7xx8
A total solar eclipse that occurs at 2xxx days with a savings balance of $9xx0
A total solar eclipse that occurs at 3x4x days with a savings balance of $12xxx
ANSWER:
I have provided the properly commented
and indented code so you can easily copy the code as well as check
for correct indentation.
I have provided the output image of the code so you can easily
cross-check for the correct output of the code.
Have a nice and healthy day!!
CODE
% a. Ask the user for the required budget. using input function,
budget = input("Please enter your own budget: ");
% b. Ask the user to input their daily savings rate starting from 1-1-2018.
saving = input("Please enter your daily saavings rate(starting from 1-1-2018): ");
% c. Ask the user to input their initial savings
ini_saving = input("Please enter your initial savings(on 1-1-2018): ");
% d. Future total solar eclipse events that you will be afford to go to
% As not provided, defining own solar eclipse events with required budgets
% defining vector of days after solor eclipse event will fall from 1-1-2018
days_after = [100,200,300,400,500,600];
% required budget to travel
req_budget = [400,900,1700,1800,2300,2500];
% looping for each event and checking if user can attend the event or not
% defining counter to record no. of events user can attend
events = 0;
for i=1:6
day = days_after(i);
required_bud = req_budget(i);
% calculating saving of user till date
saving_till_event = ini_saving + day*saving;
% checking if saving fullfill required_bud
if saving_till_event>= required_bud
% user can attend the event
% incrementing even t counter by 1
events = events+1;
end
end
% displaying result
fprintf("With a initial saving of $%d and per day saving of $%d, user can attend %d events.\n",ini_saving,saving,events);
OUTPUT IMAGE