Array Question

This is probably a very dumb question, I feel like I must be looking at it wrong or something. I'm learning about arrays and while writing my own program I came across something that stumped me. Here is a snippet of my code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Global
#define MaxColumns 10
#define MaxRows 20

//Array
	char Map[MaxColumns][MaxRows] = 
	{
		"###################",
		"#                 #",
		"#                 #",
		"#                 #",
		"#                 #",
		"#                 #",
		"#                 #",
		"#                 #",
		"#                 #",
		"###################", 
	};

So why are there 10 columns (as there is supposed to be) but only 19 rows? My code works 100% fine by the way, I'm just a little curious as to why the rows don't add up. Whenever I try to add another initializer to make it 20 rows I get an error.
Last edited on
When you have char str[] = "hello"; the first 5 chars in str will have the hello and 6th will have a null character to show end of the string.
I believe it's because of the null character (\0), terminating every string literal. It is functioning as your 20th character.
I think character arrays have a terminator/null character at the end, '\0'. This could be using the 20th character space in each case. Changing Maxrows to 21 should work.

Hope this helps.
Last edited on
That's very weird, I didn't know that. Changing the array to char Map[MaxColumns][MaxRows + 1] fixed the problem. I knew that would fix the problem I just wasn't sure why. Thanks for the info!
Last edited on
I'm moving on now but I figured in case anyone else came to this thread because they were confused about arrays I would post the code I made to practice arrays. This is a pretty straight forward and simple code so I didn't comment it but hopefully it helps someone out there. ;)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#define MaxColumns 10
#define MaxRows 20

using namespace std;

void DrawMap();
void ArrayTest();

int main()
{
	int myChoice;
	enum Choice { MAP = 1, ARRAY };

	cout << "Enter your choice (1 for DrawMap | 2 for ArrayTest): ";
	cin >> myChoice;

	switch(myChoice)
	{
	case MAP:
		DrawMap();
		break;
	case ARRAY:
		ArrayTest();
		break;
	default:
		cout << "ERROR! Invalid choice..." << endl;
		break;
	}

	return 0;
}

void ArrayTest()
{
	int Yams[3] = { 7, 8, 6 };
	int YamCosts[3] = { 20, 30, 5};

	int Total = ( Yams[0] * YamCosts[0] ) + ( Yams[1] * YamCosts[1] ) + 
		( Yams[2] * YamCosts[2] );
	
	cout << "Total yams = " << 
		Yams[0] + Yams[1] + Yams[2] << endl;

	cout << "The package with " << Yams[1] <<
		" yams costs " << YamCosts[1] << " cents per yam" << endl;

	int YamsDollars = Total / 100;
	int YamsCents = Total % 100;

	cout << "Total yams expense: $" << YamsDollars << "." << YamsCents << endl;
	cout << "Size of yams array: " << sizeof(Yams) << endl;
	cout << "Size of one elements" << sizeof(Yams[0]) << endl;
}

void DrawMap()
{
	int X, Y;

	cout << "Entering DrawMap()..." << endl;
	cout << "Enter an X, Y Coord: ";

	cin >> X;
	cin >> Y;

	cout << "Drawing Map[" << MaxColumns << "][" << MaxRows << "]" << endl;
	char Map[MaxColumns][MaxRows+1] = 
	{
		"####################",
		"#                  #",
		"#                  #",
		"#                  #",
		"#                  #",
		"#                  #",
		"#                  #",
		"#                  #",
		"#                  #",
		"####################", 
	};

	//Place a '*' character at set coordinate
	//as long as it's a valid number
	if (X > MaxColumns || X < 0 || Y > MaxRows || Y < 0)
	{
		cout << "Invalid Coordinate..." << endl;
	} else
	{ 
		Map[X-1][Y-1] = '*'; 
	}

	//Draw the Map[][] array
    for(unsigned short rows=0; rows < MaxColumns ; rows++)
                cout << Map[rows] << endl;
}


Reading further into my book I see...

Of course, you should make sure the array is large enough to hold all the characters of the
string, including the null character. Initializing a character array with a string constant is one
case where it may be safer to let the compiler count the number of elements for you. There is
no harm, other than wasted space, in making an array larger than the string. That’s because
functions that work with strings are guided by the location of the null character, not by the
size of the array. C++ imposes no limits on the length of a string.

I should have just kept reading the chapter before asking for help anyway. ;)
Last edited on
Topic archived. No new replies allowed.