For Loops Nooob

Hi Guys,

I'm trying to teach myself C++ but its proving difficult not having anyone to ask! Videos only explain so much, Basically the example is asking me "Your Program should calculate and output the following information:
Total Sales by division
Average Sales By division
Highest Sales recorded in any one month and the division responsible
Lowest Sales recorded in any one month and the division responsible"

I'm guessing Div A is 59, 44, 21. So on and so fourth.

A B C D
Jan 59 32 7 6
Feb 44 16 17 33
March 21 6 9 56

Here's what I have attempted so far but I just feel lost, and unsure of what I'm doing, if someone could explain I would be super grateful.

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
  #include <iostream>
using namespace std;
 
int main ()
{
    int sales [][4] = { //multidimensional array
    {59, 32, 7, 6},
    {44, 16, 17, 33},
    {21, 6, 9, 56}
 };
 
    //cout << "The total sales for division A is " << sales[0][0] + sales[1][0] + sales[2][0] / 3 << endl; // test data
    double total = 0.0;
    for (int j=0;j<3;j++){
        for (int i=0;i<3;i++){
            total=total+sales[i][0];
        }
    }
 
    total=total/3; //finds average
 
    cout << total << endl;
 
    system("pause");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int sales[3][4] = { //multidimensional array
		{ 59, 32, 7, 6 },
		{ 44, 16, 17, 33 },
		{ 21, 6, 9, 56 }
	};

	float total[3] = {}; // Since its 3 divisions you need to store 3 different totals.

	for (int j = 0; j<3; j++) // this loops through the 3 divisions
	{
		for (int i = 0; i<4; i++) // this loops through every sale in each division
		{
			total[j] = total[j] + sales[j][i];
			
		}
	}

	for (int i = 0; i < 3; i++)
	{
		total[i] = total[i] / 3; //finds average
		cout << total[i] << endl; // prints each divisions average sales
	}
Last edited on
Thankyou for your swift reply Tarik. So in order to do the other things asked, such as highest sale etc, would I just create more loops? Forgive me if that sounds/is stupid.
Well yes, a nested for-loop is required. I'll try and explain it, and you'll try to put it in code okay? :)

You're gonna need this loop -

1
2
3
4
5
6
7
8
9
for (int j = 0; j<3; j++) 
{
         for (int i = 0; i<4; i++)
	{
			
		
	}

}


1. Create a float variable just above this for-loop and call it highestSale;
1.1. Create a integer variable and call it highestDivision;

2. Set highestSale to the first value of the multidimensional array.
2.2. Set highestDivision to 0.

3. now inside the for-loop. ask, if the value of the array Im currently in (sales[j][i]) is bigger than highestSale. Then set highestSale equal to the current value of the array.
3.3. in the same for-loop. Set highestDivision to equal j.

And there you go. If you print out highestSale you should get 59. If you print out highestDivision you should get 0, because its the first division. Try doing this and post your code here after :)

P.s. To find the lowest value, you do the same but just switch the if-statement.

Thankyou!! I am trying to get the first bit working and I will reply with my efforts! However I cant seem to get the initial part to run and I can't for the life of me see why :(

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
#include <iostream>
using namespace std;

int main ()

int sales[3][4] = { //multidimensional array

		{ 59, 32, 7, 6 },
		{ 44, 16, 17, 33 },
		{ 21, 6, 9, 56 },

	};

    float total[3] = {}; // Since its 3 divisions you need to store 3 different totals.

	for (int j = 0; j<3; j++) // this loops through the 3 divisions
	{
		for (int i = 0; i<4; i++) // this loops through every sale in each division
		{
			total[j] = total[j] + sales[j][i];

		}
	}

	for (int i = 0; i < 3; i++)
	{
		total[i] = total[i] / 3; //finds average
		cout << total[i] << endl; // prints each divisions average sales
	}


Any thought?
haha :D

You're missing the opening bracket for the main function.

1
2
3
int main ()

int sales[3][4] =


You see what I mean? You need a {.

1
2
3
4
5
6
int main()
{

// all the code

}
The code seems to be adding 59, 32, 7, 6 and then dividing these numbers by 3, however I believe it should be adding 59, 44, 21 (Div A) and then dividing by 3, if that makes sense aha! As it is supposed to do by Division; ie Div A, Div B etc, however it seems to be calculating it by month if that makes sense, so the average of Div A would be 41.3333. How would I amend the code to do this? I deeply apologise for how frustrating this may be for you, but for some reason I get lost when looking at for loops :/
The divisions being 59,44,21 - 32,16,6 etc makes no sense to me. If that is the case, then the array is initially built the wrong way.

Make it int sales[4][3] and switch the numbers. Becuase, the first part of the array [4] is the "rows" and in this case should be the divisions, while the other part [3] is columns in each row. Which in this case are the sales. Thats how multidimensional arrays work.
So there are actually 4 Divisions, A, B, C, D
1
2
3
4
5
6
7
8
int sales[4][3] = { //multidimensional array

{ 59, 44, 21}, // A
{ 32, 16, 6}, // B 
{ 7, 17, 9}, // C 
{6,33,56}, // D
};
		

Last edited on
Thankyou Tarik for your continued support! I have got it working, outputting the average of each Division, thankyou!

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
#include <iostream>
using namespace std;

int main ()
{

int sales[4][3] = { //multidimensional array

{ 59, 44, 21}, // A
{ 32, 16, 6}, // B
{ 7, 17, 9}, // C
{6,33,56}, // D
};

    float total[4] = {}; // Since its 3 divisions you need to store 3 different totals.

	for (int j = 0; j<4; j++) // this loops through the 3 divisions
	{
		for (int i = 0; i<3; i++) // this loops through every sale in each division
		{
			total[j] = total[j] + sales[j][i];

		}
	}

	for (int i = 0; i < 4; i++)
	{
		total[i] = total[i] / 3; //finds average
		cout << "The average sales for Divisions A "  << total[i] << endl; // prints each divisions average sales
	}
}


How could I make it so the output said-
Div A Average = Answer
Div B Average = Answer
etc?
You could create a character array, with all 4 divisions -

char divisions[4] = { 'A', 'B', 'C', 'D' };

Then make sure the for loop prints them out with everything else -

1
2
3
4
5
6
char divisions[4] = { 'A', 'B', 'C', 'D' };
	for (int i = 0; i < 4; i++)
	{
		total[i] = total[i] / 3; 
		cout << "The average sales for Divisions  "<< divisions[i] << ": " << total[i] << endl; 
	}
Topic archived. No new replies allowed.