displaying data in arrays

If asked to display data column by column and row by row using a while statement in the outer loops and a for statement in the nested loops, would this be correct? Any suggestions?
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
38
  #include <iostream>
using namespace std;

int main()
{
	int nums[2][4] = { { 17, 24, 86, 35 },
	{ 23, 36, 10, 12 } };

	int row = 0;
	int column = 0;

	
	//display column by column
	while (column < 4)
	{
		for (int row = 0; row < 2; row++)
		{
			cout << "Numbers (column): " << nums[row][column]  << endl;
		}

		column++;
	}
	cout << endl;

	//display row by row
	while (row < 2)
	{
		for (column = 0; column < 4; column++)
		{
			cout << "Numbers (row): " << nums[row][column] << endl;
		}
		row++;
	}
	cout << endl;
	

	return 0;
}	//end of main function  
Have you tested? Does it work?
It displays the correct data but I would like to display it as:

Column 1: 17, 23
Column 2: 24,36
Column. 3: 86,10
Column 4: 35,12

Row 1: 17,24, 86, 35
Row 2: 23,36,10,12

Think about it. What is this Column n? What represents n in that place?
Also, is a while loop the most appropriate loop in this case? All is does is increment over a range of values. Will not be a for better?
This should work. Figure out how to do the commas yourself.
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
38
39
  #include <iostream>
using namespace std;
int main()
{
	int nums[2][4] = { { 17, 24, 86, 35 },
	{ 23, 36, 10, 12 } };

	int row = 0;
	int column = 0;

	
	//display column by column
	while (column < 4)
	{
		cout << "\nColumn " << column + 1 << " : ";
		for (int row = 0; row < 2; row++)
		{
			cout  << nums[row][column]  << " ";
		}

		column++;
	}
	cout << endl;

	//display row by row
	while (row < 2)
	{
		cout << "\nRow " << row + 1 << " : ";
		for (column = 0; column < 4; column++)
		{
			cout << nums[row][column] << " ";
		}
		row++;
	}
	cout << endl;
	

	return 0;
}	//end of main function   
Last edited on
Thanks ephraimr!

I tried cout << "\nColumn " << column + 1 << " : "; last night but had it in the wrong line and since column//row are initialized to 0 I couldn't figure out how to get my counter from starting at 0 (column 0: 17 23 )...Thanks!

now how would one get commas between the numbers EXCLUDING a comma after the last element? This is what I have :

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
38
39
40

#include <iostream>
using namespace std;
int main()
{
	int nums[2][4] = { { 17, 24, 86, 35 },
	{ 23, 36, 10, 12 } };

	int row = 0;
	int column = 0;


	//display column by column
	while (column < 4)
	{
		cout << "\nColumn " << column + 1 << " : ";
		for (int row = 0; row < 2; row++)
		{
			cout << nums[row][column] << ", ";
		}

		column++;
	}
	cout << endl;

	//display row by row
	while (row < 2)
	{
		cout << "\nRow " << row + 1 << " : ";
		for (column = 0; column < 4; column++)
		{
			cout << nums[row][column] << ", ";
		}
		row++;
	}
	cout << endl;

	system("pause");
	return 0;
}	//end of main function    


Last edited on
now how would one get commas between the numbers EXCLUDING a comma after the last element?

That implies an if statement.

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

const int ROWS = 2;
const int COLS = 4;

int main()
{   int nums[ROWS][COLS] = { { 17, 24, 86, 35 },
	{ 23, 36, 10, 12 } };

	int row = 0;
	int column = 0;

	//display column by column
	while (column < COLS)
	{   cout << "\nColumn " << column + 1 << " : ";
		for (int row = 0; row < ROWS; row++)
		{   cout << nums[row][column];
		    if (row != ROWS-1)
		        cout << ", ";
		}
		column++;
	}
	cout << endl;

	//display row by row
	while (row < ROWS)
	{   cout << "\nRow " << row + 1 << " : ";
		for (column = 0; column < COLS; column++)
		{   cout << nums[row][column];
		    if (column != COLS-1)
		        cout << ", ";
		}
		row++;
	}
	cout << endl;
	system("pause");
	return 0;
}	//end of main function   

Column 1 : 17, 23
Column 2 : 24, 36
Column 3 : 86, 10
Column 4 : 35, 12

Row 1 : 17, 24, 86, 35
Row 2 : 23, 36, 10, 12
Press any key to continue . . .


Note the use of const statements at lines 4-5 to define the dimensions of your array. This makes it easy to change the size of the array. i.e. You only need to change the dimensions in one place instead of having 2 and 4 scattered all through your code.

You're inconsistent with the use of your loop variables.
Line 17: You introduce a new loop variable called row.
Line 30: You're using the column variable defined at line 10.
This doesn't hurt anything currently, but if you were to expand your program this inconsistency might bite you later.
Last edited on
This makes it easy to change the size of the array. i.e. You only need to change the dimensions in one place instead of having 2 and 4 scattered all through your code


Thanks AbstractionAnon! I've been told this in a prior post but no explanation to accompany it. That makes sense (albeit not how my text has taught thus far) but real world > college text.
Say for all intensive purposes, the array name need stay nums [2] [4], and I forgo the constants, how would I access just the row like you did in your code on line 19 if (row != ROWS-1)?

Edit: I fixed my code with:

1
2
if (row != 2 - 1)
				cout << ", ";


but is this appropriate?
Last edited on
and I forgo the constants, how would I access just the row like you did in your code on line 19 if (row != ROWS-1)?

Well - you would substitute 2 for ROWS, giving
 
    if (row != 1)

The problem here is that the meaning is lost. What is the significance of the number 1. Does it mean the second row counting from the start, or the last row counting from the end? Or something else?

That is a reason why using such constants is better - it enables the human reader to understand the intention of the code. Remember we are not writing code only for the compiler to convert into an executable program, we also (and perhaps more importantly) write code for the benefit of the human reader.
Last edited on
Chervil

I understand the necessity of constants in this example.

The problem is we were given a source code template to complete with the array being nums [2][4], I don't want to change the code already given. I was only asked to display the elements column by column and then row by row.

I may be trying to do too much with this example eh?

I just didn't want my output to be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
column:17
column:23
column:24
column:36
column: 86
column: 10
column: 35
column: 12


row:17 
row:24
row:86
row:35
row:23
row:36
row: 10
row: 12


Last edited on
Topic archived. No new replies allowed.