problem in code

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:

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
// sales2.cpp -- sales per month for 3 years using 2D array
#include <iostream>

using namespace 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 :)
oops ignore me.
Last edited on
You do read: y1-Jan, y2-Jan, y3-Jan, y1-Feb, ...
Is your input: y1-Jan, y1-Feb, y1-Mar, y1-Apr, ...
Yeah the input is y1 - Jan, y1 - Feb....etc
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..
Last edited on
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?
store them directly in the sales array, it might work
No. &(sales[0][0]) + row*Cols + col == &( sales[row][col] )

1
2
std::cout << "Enter sales for " << months[i] << " of year " << year+1 << '\n';
std::cin >> sales[year][i];
This short code in C(not C++) but you can convert it into C++ might help you understand how to get the right input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main()
{
	int i, j, sales[3][12];
	unsigned int total = 0;
	
	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];
		}
	}
	
	printf("%d", total);
}


In this case, total is the amount of sales at the end of 3 years.
Last edited on
How to get the sales total per year?
this is a slight workaround to the solution. kinda hacky but it does the trick.

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
#include <stdio.h>
#include <string.h>

int main()
{
	int i, j, sales[3][12];
	unsigned int yearlyTotal[3];
	unsigned int 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. :)
You don't actually need any arrays in that last form.
if that's directed to me, I was showing the OP that you can create a loop within a loop with fewer variables hence saving space and code.
Here's my loop, please tell me what I am doing wrong:

int sales[3][12];
int tot_sales = 0;
int year1 = 0;
int year2 = 0;
int year3 = 0;

for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 12; ++j) {
cin >> sales[i][j];

year1 += sales[i][j];

cin >> sales[i][j];
year2 += sales[i][j];

cin >> sales[i][j];
year3 += sales[i][j];

tot_sales = year1 + year2 + year3;
}
}

I don't seem to be getting the yearly total.
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.
@keskiverto: Thanks for pointing that out!

I finally solved my problem. But, I have a problem. Here's my final code:

// sales2.cpp -- sales per month for 3 years using 2D array
#include <iostream>

using namespace 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 year1 = 0;
int year2 = 0;
int year3 = 0;


int i, j;
for (i = 0; i < 3; ++i) {
for (j = 0; j < 12; ++j) {
cin >> sales[i][j];
}
}

for (i = 0; i < 3; ++i) {
//year1 += sales[0][j];
//year2 += sales[1][j];
//year3 += sales[2][j];
for (j = 0; j < 12; ++j) {
year1 += sales[0][j];
year2 += sales[1][j];
year3 += sales[2][j];

tot_sales += sales[i][j];
}
}

//cout << "Sale on year 1: " << year_tot << endl;


cout << "Sale on year1: " << year1 / 3 << endl;
cout << "Sale on year2: " << year2 / 3 << endl;
cout << "Sale on year3: " << year3 / 3 << endl;

cout << "Total sales for 3 years: " << tot_sales << endl;

return 0;
}

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';

Topic archived. No new replies allowed.