Program 2B Given that storeMonthlySales[NUM_STORES][NUM_MONTHS][NUM_DEPTS] is a
ID: 3722821 • Letter: P
Question
Program 2B
Given that storeMonthlySales[NUM_STORES][NUM_MONTHS][NUM_DEPTS] is a three-dimensional array of float-point type. Write a C++ function, printMonthlySales, to calculate and print the total value of sales during a specific month by each department and in each store plus totals by store and department and overall sales. The return type of the function is void and the function should have 2 input parameters: Sales array and the month the user specified (edit for correct value for month!). The constants NUM_STORES, NUM_MONTHS, and NUM_DEPTS must be accessed globally by defining the following global variables:
#define NUM_DEPTS 2
#define NUM_STORES 2
#define NUM_MONTHS 12
The array Sales is initialized by the following statement in your main():
float storeMonthlySales[NUM_STORES][NUM_MONTHS][NUM_DEPTS] =
{ 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2,
2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2,
3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2,
2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2
};
Output: Sales for month of value (where value represents “January”, “March”, etc.)
Dept # Dept # Store Total
Store # value value row value sum
Store # value value row value sum
Dept Total col value sum col value sum total overall sales
User enters a new month as many times as they wish and run printMonthlySales against the new month value.
Be sure to check for valid input (“y” or “n”) when asking if the user wants to enter a new month to print.
Then read in from a data file “inFilePgm2B.dat” additional data scenarios for testing ….. a value of -1 indicates the end of the data for that test.
Be sure to account for all the standard file checks (file doesn’t exist, file empty (should always contain at least a -1)) and handle the issues of not enough or too much data for the array, a complete (right number of data values) but array contains a negative number - still print the array but print an error message.
Explanation / Answer
#include<iostream>
#include<fstream>
#define NUM_DEPTS 2
#define NUM_STORES 2
#define NUM_MONTHS 12
using namespace std;
void printMonthlySales(float[NUM_STORES][NUM_MONTHS][NUM_DEPTS], int);
int main(){
//VARIABLE FOR READING***
int rstore = 0;
int rmonth = 0;
int rdept = 0;
float input = 0;
int i = 0;
int neg = 0;
//STORE
float storeMonthlySales[NUM_STORES][NUM_MONTHS][NUM_DEPTS] =
{ 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2,
2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2,
3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2,
2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2
};
int mon = 0;
char ans = 'y';
while(ans == 'y' || ans == 'Y'){
cout<<"Which month sales do you wish to look at: ";
cin>>mon;
if(mon > 0 && mon <= 12)
printMonthlySales(storeMonthlySales, mon - 1);
else{
cout<<"Invalid Input Entered, Input again ";
continue;
}
cout<<" Do you wish to continue: ";
cin>>ans;
}
ifstream in("inFilePgm2B.dat");
if(!in){
cout<<"The file doesn't exist, or cannot be opened";
return -1;
}
while(input != -1){//Input into departments, if that fills go to the next month and fill the dept
//After that go into thee next store and continue the same
in>>input;
if(input == -1)
continue;
if(input < 0)
neg = 1;
storeMonthlySales[rstore][rmonth%NUM_MONTHS][rdept%NUM_DEPTS] = input;
rdept++;
if(rdept%NUM_DEPTS == 0 )
rmonth++;
if(rmonth%NUM_MONTHS == 0 && rdept%NUM_DEPTS == 0)
rstore++;
}
in.close();
if(neg == 1)
cout<<"There is/are negative(s) number in the input, please check it ";
if(rdept != 48){
cout<<"Wrong amount of data has been provided, please check it";
return -1;
}
for(i = 0; i < 12; i++){
printMonthlySales(storeMonthlySales, i);
cout<<endl;
}
}
void printMonthlySales(float sales[NUM_STORES][NUM_MONTHS][NUM_DEPTS], int month){
char mnames[20][12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
float dtotals[NUM_DEPTS];
float stotals;
float overall;
int i = 0;
int j = 0;
for(i = 0; i < NUM_DEPTS; i++)
dtotals[i] = 0;
stotals = 0;
overall = 0;
cout<<"Sales for month of "<<mnames[month]<<endl;
cout<<" ";
for(i = 0; i < NUM_DEPTS; i++)
cout<<"Dept "<<i+1<<' ';
cout<<"Store Total";
for(i = 0; i < NUM_STORES; i++){
cout<<" Store "<<i+1<<" ";
for(j = 0; j < NUM_DEPTS; j++){
cout<<sales[i][month][j]<<' ';
stotals += sales[i][month][j];
dtotals[j] += sales[i][month][j];
}
cout<<stotals;
stotals = 0;
}
cout<<endl;
cout<<"Dept. Total ";
for(i = 0; i < NUM_DEPTS; i++){
cout<<dtotals[i]<<' ';
overall += dtotals[i];
}
cout<<overall<<endl;
}
//Here is the code as requested
//Hope this helps :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.