Is this the proper way to print out a multi-dimensional array?

Here's what I wrote :

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

	// Created a multi-dimensional array that holds 3 index's and 5 element's
	int elements2D [3] [5] = {
		{10, 15, 20, 25, 30}, // Row 1
		{35, 40, 45, 50, 55}, // Row 2
		{60, 65, 70, 75, 80} // Row 3
	};


	// Loop through the index's of elements2D and print the index number
	for (int index = 0; index < 3; ++index) {

		// Print current index
		cout << "Index[" << index << "]" << endl;
		
		// Loop through the elements in the index's and print the elements values
		for (int elements = 0; elements < 5; ++elements) {

			// Print element index and value
			cout << "Element[" << elements << "] = " << elements2D [index] [elements] << endl;
		}

		// Finished with first row
		cout << "Finished with Row[" << index << "]" << endl;
	}

	// End of program
    return 0;
}


And do my comments even clarify what i'm doing? When I comment i'm just really commenting what i'm adding lol.
It might be clearer to use row and column instead of index and element.
Comments like "finished with first row" are confusing, as the cout behind it will print after every row, not just the first.

Whether or not your printing itself is correct depends on what you want. Personally, I'd relace the endl on line 28 with "\t" (tab). That way, your output will look like a matrix, which makes it easy to compare to lines 12-14.

Also, look up the proper rules for plural & possessive nouns in the English language... Very embarassing if you hand in code with such blatant mistakes in grammar.
This should be better :

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

	// Created a multi-dimensional array that holds 3 rows and 5 columns
	int elements2D [3] [5] = {
		{10, 15, 20, 25, 30}, // Row 1
		{35, 40, 45, 50, 55}, // Row 2
		{60, 65, 70, 75, 80} // Row 3
	};


	// Loop through the rows of elements2D and print the row's value
	for (int row = 0; row < 3; ++row) {

		// Print current index
		cout << "Row[" << row << "] \n";
		
		// Loop through the columns in the array and print the column's values
		for (int column = 0; column < 5; ++column) {

			// Print the column's index and value
			cout << "Column[" << column << "] = " << elements2D [row] [column] << "\t";
		}

		// Space out the rows and columns
		cout << "\n";
	}

	// End of program
    return 0;
}


Yeah, sorry I failed English class terribly. I should probably study some more on it for sure :/
Last edited on
I would like this ..
1
2
3
4
5
6
7
8
9
10
11
12
for (int row = 0; row < 3; ++row) 
{
		// Loop through the columns in the array and print the column's values
		for (int column = 0; column < 5; ++column) 
               {

			// Print the column's index and value
			cout <<  elements2D [row] [column] << "\t";
		}
		// Space out the rows and columns
		cout << "\n";
}


This is much simpler
Topic archived. No new replies allowed.