Write a program that calculates information about sales during a year. This prog
ID: 3548564 • Letter: W
Question
Write a program that calculates information about sales during a
year. This program will have an array that will hold the sales totals
for 4 divisions (North, South, East and West, stored in an array as
strings for the output) and for 4 quarters. This array will also hold
the total for each division and for each quarter. These totals will be
calculated by the program once the values have been inputted by the
user. So this array will be a 2-dimensional array with the quarters as
the columns and the divisions as the rows. The last column will hold the
totals for the rows (division totals) and the last row will hold the
totals for the columns (quarter totals). The program must use/create the
following functions:
in the array except for the column and row with the totals in them.
column that is specified by the second parameter to the function.
highest value in the array. Last two parameters are the index values of
where the highest value is located in the 2D array, these can then be
used to display which division and quarter.
value in the array. Last two parameters are the index values of where
the highest value is located in the 2D array; these can then be used to
display which division and quarter.
The program should output a table in the same format as below which will then be followed by the division and quarter with the highest value and the division and quarter with the lowest value.
Quarter 1
Quarter 2
Quarter 3
Quarter 4
Division Total
North
South
East
West
Quarter Total
Quarter 1
Quarter 2
Quarter 3
Quarter 4
Division Total
North
South
East
West
Quarter Total
Explanation / Answer
please rate - thanks
any questions - ask
#include <iostream>
#define NROW 5
#define NCOLS 5
using namespace std;
int getTotal(int[][NCOLS]);
int getRowTotal(int[][NCOLS],int);
int getColumnTotal(int[][NCOLS],int);
int getAverage(int[][NCOLS]);
int getLowest(int[][NCOLS],int&,int&);
int getHighest(int[][NCOLS],int&,int&);
int main ()
{int i,j,val,row,col;
int sales[NROW][NCOLS]={0};
string division[4]={"North","South","East","West"};
for(i=0;i<4;i++)
{cout<<"for the "<<division[i]<<" division ";
for(j=0;j<4;j++)
{
cout<<"Enter quarter sales ";
cout<<"quarter "<<j+1<<">>";
cin>>sales[i][j];
}
}
for(i=0;i<NROW-1;i++)
sales[i][NCOLS-1]=getRowTotal(sales,i);
for(i=0;i<NCOLS;i++)
sales[NCOLS-1][i]=getColumnTotal(sales,i);
cout<<" Quarter 1 Quarter 2 Quarter 3 Quarter 4 Division Total ";
for(i=0;i<NROW;i++)
{if(i==(NROW-1))
cout<<"Quarter Total ";
else
cout<<division[i]<<" ";
for(j=0;j<NCOLS;j++)
cout<<sales[i][j]<<" ";
cout<<endl;
}
cout<<"Average of all sales: "<<getAverage(sales)<<endl;
val=getHighest(sales,row,col);
cout<<"Highest sale: Division "<<division[row]<<" quarter "<<col+1<<" $"<<val<<endl;
val=getLowest(sales,row,col);
cout<<"Lowest sale: Division "<<division[row]<<" quarter "<<col+1<<" $"<<val<<endl;
system("pause");
return 0;
}
int getTotal(int r[][NCOLS])
{int i,j;
int val=0;
for(i=0;i<NROW-1;i++)
for(j=0;j<NCOLS-1;j++)
val+=r[i][j];
return val;
}
int getHighest(int num[][NCOLS],int& row,int& col)
{int j,i;
int val;
val=num[0][0];
for(i=1;i<NROW-1;i++)
for(j=0;j<NCOLS-1;j++)
if(num[i][j]>val)
{val=num[i][j];
row=i;
col=j;
}
return val;
}
int getAverage(int t[][NCOLS])
{return getTotal(t)/((NROW-1)*(NCOLS-1));
}
int getLowest(int num[][NCOLS],int& row,int& col)
{int j,i;
int val;
val=num[0][0];
for(i=1;i<NROW-1;i++)
for(j=0;j<NCOLS-1;j++)
if(num[i][j]<val)
{val=num[i][j];
row=i;
col=j;
}
return val;
}
int getRowTotal(int num[][NCOLS],int n)
{int i;
int val=0;
for(i=0;i<NCOLS-1;i++)
val+=num[n][i];
return val;
}
int getColumnTotal(int num[][NCOLS],int n)
{int i;
int val=0;
for(i=0;i<NROW;i++)
val+=num[i][n];
return val;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.