Labeling an Array

So i need to create an array of square values and I need to be able to also label the rows and columns with numbers 0-9. The calculated values need to be 10x10 which they are but i'm struggling with how to figure out how to label the rows and columns without screwing up the formatting of the rest of the array. I listed how the rows should be labeled and made it a 11x11 array to just include those value into the array.
Thanks for the help.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include <iostream>
using namespace std;

int main ()
{
		int column=0;
		int row=0;
		int array [11][11];
		int value=0;

		array[0][1]=0;
		array[0][2]=1;
		array[0][3]=2;
		array[0][4]=3;
		array[0][5]=4;
		array[0][6]=5;
		array[0][7]=6;
		array[0][8]=7;
		array[0][9]=8;
		array[0][10]=9;


		array[1][0]=0;
		array[2][0]=1;
		array[3][0]=2;
		array[4][0]=3;
		array[5][0]=4;
		array[6][0]=5;
		array[7][0]=6;
		array[8][0]=7;
		array[9][0]=8;
		array[10][0]=9;

	cout << endl;

	for (row=1; row<11; row++)
	{
		for (column=1; column<11; column++)
		{
			array [row][column] = value * value;
			value++;
		}// end for (column=0; column<10; column++)
	} //end for (row=0; row<10; row++)

	for (row=1; row<11; row++)
	{

		for (column=1; column<11; column++)
		{
			cout << array[row][column] << "\t";
		}//end for (column=0; column<10; column++)
		cout << endl;
	} //end for (row=0; row<10; row++)
	return 0;
The loops on lines 45 and 48 should start from 0, not 1; otherwise your program is fine except for array[0][0] which is handled in an implementation dependent way:
http://coliru.stacked-crooked.com/a/807b820c784fb090
clang sets [0][0] to 0 but gcc reads some garbage value
You could also take the row and column headers out of your container, have a 10X10 array and print around the edges:
http://coliru.stacked-crooked.com/a/d37913ed473d56b5
Last edited on
with named headers for rows and columns as well:
http://cpp.sh/7pvan
Topic archived. No new replies allowed.