Computing.Net > Forums > Programming > Populating 2d array in C++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Populating 2d array in C++

Reply to Message Icon

Name: markwp
Date: April 2, 2006 at 10:31:47 Pacific
OS: WinXP
CPU/Ram: Pentium M 2ghz/ 512mb
Product: inspiron 8600 laptop
Comment:

I am trying to use nested for loops to populate an array. It compiles but gives an error on execution. I am using bloodshed IDE (mingw) compiler. Any idea what I am doing wrong?

Here is the code snippet

char grid [9][9];
for (int i = 0; i <= 9; i++){
for (int ii = 0; ii <= 9; ii++){
grid[i][ii] = '.';
}



Sponsored Link
Ads by Google

Response Number 1
Name: mythoslegend
Date: April 2, 2006 at 12:41:41 Pacific
Reply:

Try using
char grid [9][9];
for (int i = 0; i <= 8; i++){
for (int ii = 0; ii <= 8; ii++){
grid[i][ii] = '.';
}
You have 9 rows and you start with zero, so you want to end at 8.


0

Response Number 2
Name: sspamer
Date: April 12, 2006 at 17:50:45 Pacific
Reply:

Take out the equal signs. Your indeces are from 0 ... 8 grid[9][9] is an array out of bounds exception, which I presume is the error you get. Also, so are grid[9][x], grid[x][9]

You can also initialize (fill up your array) by hard coding it:

grid[9][9]={'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.', '.'
};


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


grand total in mysql VB.NET output from DataGr...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Populating 2d array in C++

How to use string array in C++? www.computing.net/answers/programming/how-to-use-string-array-in-c/9478.html

malloc in C www.computing.net/answers/programming/malloc-in-c/8060.html

vector of arrays in c++ www.computing.net/answers/programming/vector-of-arrays-in-c/12919.html