C++ Primer Plus Chapter 5 Exercise 5

c plus plusExercise 5 is a remake of exercise 4, except this time we introduce a two dimensional array into our loops. The purpose of this is to fill it first with years then months. This way, the program will iterate through 12 months for 3 years. See the source below for the solution.

5. Do Programming Exercise 4, but use a two-dimensional array to store input for 3 years
of monthly sales. Report the total sales for each individual year and for the combined
years.

#include <iostream>
#include <string>

using namespace std;

int main()
{
string month[12] = {"January","February ","March ","April ","May","June","July","August","September","October","November","December"};
int sales[3][12];
int yearTotal;
int sum = 0;
int total = 0;
int everything = 0;

cout << "Enter sales for the month: " << endl;

for(int y = 0; y < 3; y++)
{
for(int m = 0; m < 12; m++)
{
cout << month[m] << ": ";
cin >> sales[y][m];
sum=sum+sales[y][m];
total=sum;

}

}

for(int y = 0; y < 3; y++)
{
yearTotal = 0;
for(int m =0; m < 12; m++)
{
cout << month[m] << sales[y][m] << endl;
yearTotal+=sales[y][m];
everything+=sales[y][m];
}

cout << "Sales for this year: " << yearTotal << endl;
}

cout << "The data for all years: " << everything << endl;

return 0;
}

Advertisements