C++ Programming: I need help writing a void function that prints sales per product per period using 3 for loops. Please see my code below ... not sure how to write the function. But this is what I've got so far. Please leave main alone. What the output should look like is posted below the code. Thanks :) #include #include #include using namespace std; int const LOCATION_DIM_SZ = 2; int const TIME_DIM_SZ = 2; int const PRODUCT_DIM_SIZE = 3; string Location[] = { "FL", "TX" }; string Period[] = { "Jan", "FEB" }; string Product[] = { "iPhone","iPad","MacBookPro" }; /*1*/ void printSalesPerProductPerPeriod(double sales[LOCATION_DIM_SZ][TIME_DIM_SZ][PRODUCT_DIM_SIZE]) { int i(0), j(0), k(0); double salesPerProductPerPeriod = 0; cout << "Total Sales per Product per Period" << endl; cout << "====================================" << endl; for (i = 0; i < 3; i++) { for (j = 0; j < 2; j++) { salesPerProductPerPeriod = 0; for (k = 0; k < 2; k++) { salesPerProductPerPeriod += sales[j][k][i]; } cout << "Total sales for " << Product[i] << " in " << Period[j] << " = $" << salesPerProductPerPeriod << " millions" << endl; } } cout << endl; cout << "===================================================" << endl; } int main(int argc, const char* argv[]) { double sales[LOCATION_DIM_SZ][TIME_DIM_SZ][PRODUCT_DIM_SIZE] = { 0 }; sales[0][0][0] = 45; sales[0][0][1] = 8; sales[0][0][2] = 4; sales[0][1][0] = 35; sales[0][1][1] = 10; sales[0][1][2] = 5; sales[1][0][0] = 75; sales[1][0][1] = 15; sales[1][0][2] = 13; sales[1][1][0] = 45; sales[1][1][1] = 17; sales[1][1][2] = 23.1; printSalesPerProductPerPeriod(sales); return 0; } DESIRED OUTPUT: Total Sales per Product per Period ==================================== Total Sales for iPhone in Jan =$ 120.00 millions Total Sales for iPhone in FEB =$ 80.00 millions Total Sales for iPad in Jan =$ 23.00 millions Total Sales for iPad in FEB =$ 27.00 millions Total Sales for MacBookPro in Jan =$ 17.00 millions Total Sales for MacBookPro in FEB =$ 28.10 millions ===================================================
C++ Programming:
I need help writing a void function that prints sales per product per period using 3 for loops. Please see my code below ... not sure how to write the function. But this is what I've got so far. Please leave main alone. What the output should look like is posted below the code. Thanks :)
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int const LOCATION_DIM_SZ = 2;
int const TIME_DIM_SZ = 2;
int const PRODUCT_DIM_SIZE = 3;
string Location[] = { "FL", "TX" };
string Period[] = { "Jan", "FEB" };
string Product[] = { "iPhone","iPad","MacBookPro" };
/*1*/
void printSalesPerProductPerPeriod(double sales[LOCATION_DIM_SZ][TIME_DIM_SZ][PRODUCT_DIM_SIZE]) {
int i(0), j(0), k(0);
double salesPerProductPerPeriod = 0;
cout << "Total Sales per Product per Period" << endl;
cout << "====================================" << endl;
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
salesPerProductPerPeriod = 0;
for (k = 0; k < 2; k++) {
salesPerProductPerPeriod += sales[j][k][i];
}
cout << "Total sales for " << Product[i] << " in " << Period[j] << " = $" << salesPerProductPerPeriod << " millions" << endl;
}
}
cout << endl;
cout << "===================================================" << endl;
}
int main(int argc, const char* argv[]) {
double sales[LOCATION_DIM_SZ][TIME_DIM_SZ][PRODUCT_DIM_SIZE] = { 0 };
sales[0][0][0] = 45;
sales[0][0][1] = 8;
sales[0][0][2] = 4;
sales[0][1][0] = 35;
sales[0][1][1] = 10;
sales[0][1][2] = 5;
sales[1][0][0] = 75;
sales[1][0][1] = 15;
sales[1][0][2] = 13;
sales[1][1][0] = 45;
sales[1][1][1] = 17;
sales[1][1][2] = 23.1;
printSalesPerProductPerPeriod(sales);
return 0;
}
DESIRED OUTPUT:
Total Sales per Product per Period
====================================
Total Sales for iPhone in Jan =$ 120.00 millions
Total Sales for iPhone in FEB =$ 80.00 millions
Total Sales for iPad in Jan =$ 23.00 millions
Total Sales for iPad in FEB =$ 27.00 millions
Total Sales for MacBookPro in Jan =$ 17.00 millions
Total Sales for MacBookPro in FEB =$ 28.10 millions
===================================================
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images