a file has the contents below.. Each number represents the amount of money made
ID: 3632202 • Letter: A
Question
a file has the contents below.. Each number represents the amount of money made each month. Each line represents a new year (12 months per line). Right now, the program will read the numbers below and tell the month and year with the highest sales. Modify the program underneath so it will AT THE END tell the month with the highest sales each year (Do not change the current printf at the bottom, just add another one ). Uses arrays.FIle:
791190.00 1405022.44 6028934.99 8325426.23 9311234.99 1735566.22 8325563.84 3645678.75 8564334.53 5634692.99 1470123.53 0720503.12
0958018.89 6793564.47 4233561.65 7938018.43 6423501.83 3242300.58 1245200.85 7183146.26 9823834.26 215647.64 782352.93 5123584.53
3123585.32 534316.44 5123512.73 5801856.53 344644.88 434456.93 1767777.54 6823466.36 2834512.36 4123478.20 724571.99 6723126.53
316789.93 8934621.32 993223.72 2623421.41 852353.01 1234623.06 232434.45 4719994.23 981121.59 834523.43 6466754.88 3674500.20
8372300.08 484723.20 4968340.53 3388003.88 1968303.54 3290405.94 6704901.67 2539292.38 1438357.49 5939394.83 1958030.81 6939829.37
program:
#include<stdio.h>
#include<conio.h>
/*main function*/
main()
{
/*declaring variables*/
double sales[5][12];
double monthlySales;
double highestSales=0;
int hYear=0,hMonth=0;
int m,y;
/*file pointer*/
FILE *fp;
clrscr();
/*opening file*/
fp=fopen("input.txt","r");
if(fp==NULL)
{
printf("File not found");
exit(0);
}
for(y=0;y<5;y++)
{
for(m=0;m<12;m++)
{
fscanf(fp,"%lf",&monthlySales);
sales[y][m]=monthlySales;
if(monthlySales>highestSales)
{
hYear = y;
hMonth = m;
highestSales= monthlySales;
}
}
}
printf("Month of Highest Sales: Year %2d, Month %2d , %.2lf Dollars ",hYear+1,hMonth+1,sales[hYear][hMonth]);
printf("Month Before's Sales: Year %2d, Month %2d , %.2lf Dollars ",hYear+1,hMonth,sales[hYear][hMonth-1]);
printf("Month After's Sales: Year %2d, Month %2d , %.2lf Dollars ",hYear+1,hMonth+2,sales[hYear][hMonth+1]);
printf("Year After the Month of Highest Sales: Year %2d, Month %2d , %.2lf Dollars ",hYear+2,hMonth+1,sales[hYear+1][hMonth]);
}
Explanation / Answer
in your variable declarations add this code: double tempSum = 0.0; int highestMonth = 0; double highestMonthIncome = 0.0; after your for loops add this code: for(m = 0; m < 12; m++) { tempSum = 0.0; for(y = 0; y < 5; y++) { tempSum += sales[y][m]; } if(tempSum > highestMonthIncome) { highestMonthIncome = tempSum; highestMonth = m; } } after your printf statements add this one: printf("The Month with the Highest overall sales: Month %2d, %.2lf Dollars ", highestMonth, highestMonthIncome);Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.