Hi, I was solving a question where I had to enter 3 years worth of monthly sales (in terms of number of books sold). I had to use a 2D array to store input for 3 years of monthly sales. Finally, I have to report the total sales each year, and the combined sales over 3 years.
This question was in the book I follow.
Here's my code:
// sales2.cpp -- sales per month for 3 years using 2D array
#include <iostream>
usingnamespace std;
int main()
{
string months[12] =
{
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
int sales[3][12];
int tot_sales = 0;
int sale1 = 0;
int sale2 = 0;
int sale3 = 0;
for (int i = 0; i < 12; ++i) {
cin >> sales[0][i];
sale1 += sales[0][i];
cin >> sales[1][i];
sale2 += sales[1][i];
cin >> sales[2][i];
sale3 += sales[2][i];
}
tot_sales = sale1 + sale2 + sale3;
cout << "Sale on year 1: " << sale1 << endl;
cout << "Sale on year 2: " << sale2 << endl;
cout << "Sale on year 3: " << sale3 << endl;
cout << "Total sales for 3 years: " << tot_sales << endl;
return 0;
}
Unfortunately, when I enter the input I don't get the correct year end values.
Here's an example of the output:
12 34 45 56 12 34 45 23 90 98 87 10
10 20 30 40 50 60 70 80 90 10 12 12
11 22 33 44 55 66 77 88 99 11 10 12
Sale on year 1: 484
Sale on year 2: 493
Sale on year 3: 581
Total sales for 3 years: 1558
It'll be a great help if I'm told where I went wrong.
Thanks :)
Yeah the input is y1 - Jan, y1 - Feb....etc
are you sure? Because i disagree.
In wordy terms your for loop is telling me this: "For January, tell me your sales for year 1, year 2, year 3. Now tell me for February your sales for year 1, year 2, year 3". etc..
That is the point. The data is in one order, but the code expects different order.
Code can change:
1 2 3 4 5
const size_t Years {3};
int yearlytotals[Years] {};
for ( size_t year = 0; year < Years; ++year ) {
// accumulate months of year 'year+1' to yearlytotals[year]
}
@mutexe: I wasn't really sure what I was doing...I thought I was taking inputs for each year separately, instead of each month over three years. Thanks for pointing that out.
Should the 2D sales array be sales[12][3] for the program to work according to what I was thinking?
#include <stdio.h>
#include <string.h>
int main()
{
int i, j, sales[3][12];
unsignedint yearlyTotal[3];
unsignedint total = 0;
memset(yearlyTotal, 0, sizeof yearlyTotal);
for( i=0; i<3; i++ )
{
for( j=0; j<12; j++ )
{
printf("Enter sale %d for month %d: ", i, j);
scanf("%d", &sales[i][j]);
total += sales[i][j];
yearlyTotal[i] += sales[i][j]; // get yearly total for each year
}
printf("Yearly total %d: %d\n", i, yearlyTotal[i]); // print it
yearlyTotal[i] = 0; // reset value to 0
printf("Yearly total %d reset to %d\n", i, yearlyTotal[i]);
}
printf("%d", total);
}
my coding skills suck. apologies. this is the best i could do. :)
The outer loop executes the inner loop 3 times.
The inner loop executes the "read input" 12 times.
The "read input" reads 3 values. 3 * 12 * 3 == 108 However, three years have only 36 months.
Lets demonstrate:
1 2 3 4 5 6 7
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 12; ++j) {
cout << "I want sale of month " << j+1 << " of year " << i+1 << " and I will add it to total for year 1\n";
cout << "I want sale of month " << j+1 << " of year " << i+1 << " and I will add it to total for year 2\n";
cout << "I want sale of month " << j+1 << " of year " << i+1 << " and I will add it to total for year 3\n";
}
}
Run that loop. It does not read any values, but the output should help you to understand what your loop does.
Here's the output:
1 2 3 4 5 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 2 3 4
1 2 3 4 1 2 3 4 1 2 3 4
Sale on year1: 15
Sale on year2: 13
Sale on year3: 30
Total sales for 3 years: 58
Before, I noticed that the output was repeated 3 times for each year.
What I mean is for Sale on year1 it showed 45, for sale on year2 it showed 39, and for sale on year3 it showed 90.
So, I just divided the final answer by 3 in the cout statements.
My question is, is there any way I can get the loop to get the correct value, without having to divide the final values by 3.
Thanks for all the help previously. Any help for the current problem would be appreciated.
It would be good to use code tags. That makes the post more readable.
You have both nested loop and add to each year in the inner loop. Your inner loop is repeated three times for no apparent reason. Your inner loop has separate named variables, so the outer loop is not necessary.
I did give a hint earlier:
1 2 3 4 5
const size_t Years {3};
int yearlytotals[Years] {};
for ( size_t year = 0; year < Years; ++year ) {
// accumulate months of year 'year+1' to yearlytotals[year]
}
Lets complete it:
1 2 3 4 5 6 7 8 9
const size_t Years {3};
int yearlytotals[Years] {};
tot_sales = 0;
for ( size_t year = 0; year < Years; ++year ) {
for size_t month = 0; month < 12; ++month ) {
yearlytotals[year] += sales[year][month];
}
tot_sales += yearlytotals[year];
}
Then, something entirely different:
1 2 3 4 5 6 7 8 9 10 11 12 13
const size_t Years {3};
int tot_sales {0};
for ( size_t year = 0; year < Years; ++year ) {
int yearly {0};
int value;
for size_t month = 0; month < 12; ++month ) {
cin >> value;
yearly += value;
}
cout << "Year " << year+1 << " sales: " << yearly << '\n';
tot_sales += yearly;
}
cout << "Total sales: " << tot_sales << '\n';