2d arrays

ok so I was able to initialize my array but not sure how to add the rows and display sum per row and display the total of all the rows could use help or explanation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
using namespace std;

int main ()
{
  // an array with 2 rows and 3 columns.
  double a[2][3] = { 7.2,0.4,5.6,1.2,9.0,3.5};

  // output each array element's value
  for ( int i = 0; i < 2; i++ )
    for ( int j = 0; j < 3; j++ )
      {
        cout << "a[" << i << "][" << j << "]: ";
        cout << a[i][j]<< endl;
      }
  return 0;
}
Last edited on
You're close, now you can treat each member of the array as an individual variable.

1
2
3
double Total = 0;
Total += a[i][j];
j++;
Last edited on
hey newbieg did I display the sum per row right?? and I did how you told me but Im getting errors?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main ()
{
  // an array with 2 rows and 3 columns.
  double a[2][3] = { 7.2,0.4,5.6,1.2,9.0,3.5};
  double total=0;
  // output each array element's value
  for ( int i = 0; i < 2; i++ )
    for ( int j = 0; j < 3; j++ )
      {
        cout << "a[" << i << "][" << j << "]: ";
        cout << a[i][j]<< endl;
        total+=a[i][j];
        j++;
     }
  return 0;
}
Last edited on
also im only getting numbers 7.2,5.6,1.2,3.5 when I run it
the way you initialized the array is not good practice, actually (I'm a beginner as well) i didn't know your initialization would even compile, but it does.
1
2
3
4
5
6
7
8
9
     //initialize in the following two formats, to make code more readable
      	double a[2][3] = { { 7.2, 0.4, 5.6 },{ 1.2, 9.0, 3.5 } };
       
      	// or
      	double a[2][3] = 
      	{ 
      	     { 7.2, 0.4, 5.6 },
      	     { 1.2, 9.0, 3.5 } 
      	};
Last edited on
i tried the second way you have it but it didn't work for some reason but now im stuck on adding the rows and displaying the sum per row and displaying the total for all rows
a[0][0]: 7.2
a[0][2]: 5.6
a[1][0]: 1.2
a[1][2]: 3.5
these are the only numbers it shows when i run it and i don't know why
Last edited on
closed account (D80DSL3A)
Line 16. You don't need it. The j++ thing happens in the for loop.
here ive prepared a solution with comments, let me know if you dont understand anything
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
	// initialize in a readable format
	double a[2][3] = { { 7.2, 0.4, 5.6 }, { 1.2, 9.0, 3.5 } };
	//get a variable ready to store total
	double total = 0;
	//make a for loop for the outermost dimension
	for (int i = 0; i < 2; i++) {
		//here we'll get a variable ready to display total of the row
		double row = 0;
		// now make a for loop for the 2nd outer most 
		// dimension, your case: only 2D so this is the final loop
		for (int j = 0; j < 3; j++){
			// now output to console and add this to the row total
			cout << "a [" << i << "][" << j << "] : ";
			//here well use \n instead of endl which ive also heard is
			//bad practice
			cout << a[i][j] << "\n";
			row += a[i][j];
		}
		// here we'll output the row total when the inner loop finishes

		cout << "Row total is: " << row << "\n";
		// also well add the row total to the grand total
		total += row;

	}
	cout << "The Grand total is: " << total;
wow seriously thanks mxood i see what i was missing really helpful and thanks to everyone else as well appreciate it
Topic archived. No new replies allowed.