noob here.
I'm working on a program to track a "surveillance balloon" for a school project, it's basically glorified battleship. I need to make a 2D array to represent a grid. The grid needs to be a square anywhere between 10x10 and 20x20. I'm using a constant to determine the size of the grid called ARRAY_SIZE*.* My code works all right when ARRAY_SIZE is set to 10, but whenever I go any higher I get issues with the array having missing values, or characters that aren't supposed to be there.
#include <iostream>
using namespace std;
const int ARRAY_SIZE = 10; //The array will consist of this constant squared
//Function to print the map
void printMap(int size, char Bmap[ARRAY_SIZE-1][ARRAY_SIZE-1]) {
cout << size << "x" << size << "map" << endl;
for (int z = 0; z <= size; z++) {
if (z == 0) {
cout << " ";
}
if (z < 10) {
cout << z << " ";
} else {
cout << z << " ";
}
}
cout << endl;
cout << endl;
for (int row = 0; row <= size; row++){
if (row < 10) {
cout << row << " ";
for (int col = 0; col <= size; col++) {
cout << Bmap[row][col] << " ";
}
cout << endl;
}
else {
cout << row << " ";
for (int col2 = 0; col2 <= size; col2++) {
cout << Bmap[row][col2] << " ";
}
cout << endl;
}
}
}
//Main Function
int main() {
bool balloonFound = false;
int endGame = false;
int queryInput = 0;
char map[ARRAY_SIZE-1][ARRAY_SIZE-1] = {'$'};
//VARIABLE DECLARATIONS ^^^
int Ycord = 0;
int Xcord = 0;
for (Xcord = 0; Xcord <= (ARRAY_SIZE-1); Xcord++) {
for (Ycord = 0; Ycord <= (ARRAY_SIZE-1); Ycord++) {
map[Xcord][Ycord] = { '$' };
}
Ycord = 0;
}
Xcord = 0;
while (endGame != true) {
cout << "What would you like to do?:" << endl;
cout << "1. Display Map" << endl;
cout << "2. Guess Location of Balloon" << endl;
cout << "3. Exit" << endl;
cin >> queryInput;
if (queryInput == 1) {
printMap(ARRAY_SIZE-1, map);
} else if (queryInput == 2) {
int userX = 0;
int userY = 0;
cout << "Enter the X coordinate (0 - " << ARRAY_SIZE-1 << ") :" << endl;
cin >> userX;
cout << "Enter the X coordinate (0 - " << ARRAY_SIZE-1 << ") :" << endl;
cin >> userY;
map[userX][userY] = 'G';
} else if (queryInput == 3) {
endGame = 1;
cout << "Thank you for using the Surveillance Balloon Tracker" << endl;
} else {
cout << queryInput << " is not a valid choice" << endl;
}
}
}
This is the part that doesn't appear to be working properly
for (Xcord = 0; Xcord <= (ARRAY_SIZE-1); Xcord++) {
for (Ycord = 0; Ycord <= (ARRAY_SIZE-1); Ycord++) {
map[Xcord][Ycord] = { '$' };
}
Ycord = 0;
}
Xcord = 0;
This works all right, when I enter "1" to print the array my output looks like
0 1 2 3 4 5 6 7 8 9
0 $ $ $ $ $ $ $ $ $ $
1 $ $ $ $ $ $ $ $ $ $
2 $ $ $ $ $ $ $ $ $ $
3 $ $ $ $ $ $ $ $ $ $
4 $ $ $ $ $ $ $ $ $ $
5 $ $ $ $ $ $ $ $ $ $
6 $ $ $ $ $ $ $ $ $ $
7 $ $ $ $ $ $ $ $ $ $
8 $ $ $ $ $ $ $ $ $ $
9 $ $ $ $ $ $ $ $ $ $
But when ARRAY_SIZE is set to 20 the output looks like this
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
1 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
2 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
3 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
4 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
5 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
6 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
7 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
8 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
9 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
10 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
11 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
12 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
13 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
14 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
15 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
16 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
17 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
18 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
19 $ $ $ $ $ $ $
I'm just not quite sure why this is happening, any insight would be appreciated.