2D Array for QuarterlySales
Apr 26, 2018 at 11:45pm UTC
How do I create a getInput function, a calcTotal function and a displayResults function. When the purpose of getInput is to prompt the user for input and assign values to the 2D array. The purpose of the caclTotal function is to process the 2D Array and accumulate the total sales. The purpose of the displayResults function is to display the entire 2D array for quarterly sales as well as the total sales. The main function should consist of variable declarations and function calls. Do not use global variables. Pass the data through functions parameters or value returning f? Please help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
const int NUM_DIVS = 3;
const int NUM_QTRS = 4;
double sales[NUM_DIVS][NUM_QTRS];
double totalSales = 0;
int div, qtr;
cout << "This program will calculate the total sales of \n" ;
cout << "All the company's division.\n" ;
cout << "Enter the following sales information: \n\n" ;
for (div=0; div < NUM_DIVS; div++)
{
for (qtr=0; qtr < NUM_QTRS; qtr++)
{
cout << "Division" << (div + 1);
cout << ",Quarter" << (qtr + 1) << ": $" ;
cin >> sales[div][qtr];
}
for (div=0; div < NUM_ DIVS; div++)
{
for (qtr=0; qtr < NUM_QTRS; qtr++)
totalSales += sales[div][qtr];
}
cout << fixed << showpoint << setprecision (2);
cout << "The total sales for the company are: $" ;
cout << totalSales << endl;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
const int NUM_DIVS = 3;
const int NUM_QTRS = 4;
double sales[NUM_DIVS][NUM_QTRS];
double totalSales = 0;
int div, qtr;
cout << "This program will calculate the total sales of \n" ;
cout << "All the company's division.\n" ;
cout << "Enter the following sales information: \n\n" ;
for (div=0; div < NUM_DIVS; div++)
{
for (qtr=0; qtr < NUM_QTRS; qtr++)
{
cout << "Division " << (div + 1);
cout << ", Quarter " << (qtr + 3) << ": $" ;
cin >> sales [div][qtr];
}
cout << endl;
}
getInput (double sales [][qtr], int div, int qtr)
{
for (int i= 0; i<div; i++)
{
for (int j= 0; j<qtr; j++)
{
cin>>sales[i][j];
}
}
return ;
}
double calcTotal (double sales[][qtr], int div, int qtr)
{
double sumQtr[4]= 0, sumTotal= 0;
for (int i= 0; i<qtr; i++)
{
for (int j= 0; j<div; j++)
{
sumQtr[i]+= sales[i][j];
}
}
for (int i= 0; i<qtr; i++)
sumTotal+= sumQtr[i];
displayResult(sumQtr, sumTotal); //Display result should be called from here
}
void displayResult(double *quarter, double total)
{
cout << "Quarterly sales total: " ;
for (int i= 0; i<4; i++)
{
cout <<quarter[i]<<endl;
}
for (div=0; div<Num_DIVS; div++)
{
for (qtr=0; qtr < Num_QTRS; qtr++)
totalSales += sales [div][qtr];
}
cout << fixed << showpoint << setprecision(2);
cout << "The total sales for the company are: $" ;
cout << totalSales << endl;
cout << "Total sales is: " <<total;
}
return ;
}
Here is how I ended up doing it but I get an error on my getInput function. Why is that?
Last edited on Apr 30, 2018 at 1:53am UTC
Topic archived. No new replies allowed.