My program is supposed to output like this Month 1 Month 2 Month 3 District 1 (d
ID: 3572130 • Letter: M
Question
My program is supposed to output like this
Month 1 Month 2 Month 3
District 1 (data) (data) (data)
District 2
District 3
District 4
I need to pass the array to a print() function that prints out the data and a totals() function that sums the total sales of each districts and then the column totals for each month of the districts. here's my source code.
#include <iostream>
#include <iomanip>
using namespace std;
void in(int arr[4][3]);
void print(int arr[4][3]);
void totals(int arr[4][3]);
main()
{
int sales[4][3];
in(sales);
print(sales);
}
void in(int arr[4][3])
{
int number;
for(int i = 0; i < 4; i++)
{
cout << "District number : " << " " << i+1 << endl;
for(int k = 0; k < 3; k++)
{
cout << "What were the sales for month #" << " " << k+1 << endl;
cin >> arr[i][k];
}
}
}
void print(int arr[4][3])
{
for(int k = 0; k < 4; k++)
{
cout << "Month" << k+1 << " ";
}
for(int i = 0; i < 3; i++)
{
cout << "District : " << i+1 << endl;
}
}
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
void in(int arr[4][3]);
void print(int arr[4][3]);
void totals(int arr[4][3]);
main()
{
int sales[4][3];
in(sales);
print(sales);
}
void in(int arr[4][3])
{
int number;
for(int i = 0; i < 4; i++)
{
cout << "District number : " << " " << i+1 << endl;
for(int k = 0; k < 3; k++)
{
cout << "What were the sales for month #" << " " << k+1 << endl;
cin >> arr[i][k];
}
}
}
// to print the month of the each district
void print(int arr[4][3])
{
for(int k = 0; k < 3; k++)
{
cout <<setw(20)<<right<< "Month" << k+1 << " ";
}
cout<<endl;
for(int i = 0; i < 4; i++)
{
cout <<"District : " ;
cout <<setw(10)<<left<< i+1;
cout<<setw(20)<<left<<arr[i][0]<<setw(20)<<left<<arr[i][1]<<setw(20)<<left<<arr[i][2];
cout<<endl;
}
totals(arr);
}
//totals to print the totals of each district
void totals(int arr[4][3]){
cout <<setw(25)<<right<< "Total"<<endl;
for(int i = 0; i < 4; i++)
{
cout <<"District : " ;
cout <<setw(10)<<left<< i+1;
int sum=arr[i][0]+arr[i][1]+arr[i][2];
cout<<setw(20)<<left<<sum<<endl;
}
}
----------------output------------
District number : 1
What were the sales for month # 1
1
What were the sales for month # 2
2
What were the sales for month # 3
3
District number : 2
What were the sales for month # 1
4
What were the sales for month # 2
5
What were the sales for month # 3
6
District number : 3
What were the sales for month # 1
7
What were the sales for month # 2
8
What were the sales for month # 3
9
District number : 4
What were the sales for month # 1
0
What were the sales for month # 2
1
What were the sales for month # 3
2
Month1 Month2 Month3
District : 1 1 2 3
District : 2 4 5 6
District : 3 7 8 9
District : 4 0 1 2
Total
District : 1 6
District : 2 15
District : 3 24
District : 4 3
-------------output-----------
Note: feel free to ask question/doubts. God bless you!!
----------------output------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.