2D array

Started by
7 comments, last by Aggronium 19 years, 10 months ago
Can you help me debug this code, it has a problem that if it writes to the last cell in the array, it writes also to the first cell in the next line: #include <iostream.h> #include <stdlib.h> #include <time.h> int const MAP_SIZE = 8; int map[MAP_SIZE][MAP_SIZE]; void reset_map(); void generate_bombs(); void print_map(); void main() { srand((unsigned)time( NULL )); reset_map(); print_map(); generate_bombs(); print_map(); } void reset_map() { for (int i=0; i <= MAP_SIZE; i++) { for (int j=0; j <= MAP_SIZE; j++) { map[j] = 0; } } } void generate_bombs() { int x, y; for (int i=0; i <= 9; i++) { x = rand()%9; //choose the bomb''s x and y y = rand()%9; if (x > 8) cout << "\nx>8\n"; if (y > 8) cout << "\ny>8\n"; cout << "\ni: "<< i << endl; if (map[x][y] == 1) { i--; cout << "equals to 1, x: "<< x << endl << "y: " << y << endl; print_map(); } else { map[x][y] = 1; cout << "x: "<< x << endl << "y: " << y << endl; print_map(); } } } void print_map() { for (int i=0; i <= MAP_SIZE; i++) { for (int j=0; j <= MAP_SIZE; j++) { cout << map[j]; } cout << endl; } } </i>
Advertisement
I'm pretty sure everywhere you put '<=' you actually meant '<'.

0----1----2----3----4----5----6----7----8---- <- oh noes, 9 elements, not 8.


[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on June 6, 2004 1:50:53 AM]
[size=2]
void reset_map(){    for (int i=0; i <= MAP_SIZE; i++)    {        for (int j=0; j <= MAP_SIZE; j++)        {            map[j] = 0;        }    }}   


change that to this:

void reset_map(){    for (int i=0; i < MAP_SIZE; i++)    {        for (int j=0; j < MAP_SIZE; j++)        {            map{i}[j] = 0;        }    }}   


replace those curly braces around the i with brackets (the post gets screwed up if you put brackets around the i...I'm new to the forums so I don't know how to get around this)


as a side note....I had no idea you could declare const variables like:

int const MAP_SIZE = 8;

I always put the const first.

Surprises around every corner with C++.


[edited by - clearsnake on June 6, 2004 2:01:42 AM]
I''m connecting from another computer and I don''t remember the password so I can''t login.
But clearsnake I wrote map{i}[j] but it made all the text after the ''i'' to Italic.
And to wild_pointer I wanted a 9x9 array and not 8x8.
The problem is that what it writes to the last cell in the row it writes also to the first cell in the next row.
quote:Original post by Anonymous Poster
I''m connecting from another computer and I don''t remember the password so I can''t login.
But clearsnake I wrote map{i}[j] but it made all the text after the ''i'' to Italic.
And to wild_pointer I wanted a 9x9 array and not 8x8.
The problem is that what it writes to the last cell in the row it writes also to the first cell in the next row.


your problem is that your arrays aren''t big enough then, they are 1 cell to small for all rows and columns.

if you want an int array of size 9 you have to do:

int array[9]; 


that will give you an array of size 9, but the indexes go from 0 to 8

so basically when you set up your for loops it would be like:

for (int i = 0; i < 9; ++i) {...}

It''s really unfortunate that C was built like this, but it''s a small price to pay. I personally think they should''ve made it like some other languages where array indexing begins at 1, which is much more intuitive intellectually (although 0-based indexing might be more intuitive for the computer).

Some languages are even better at indexing and let YOU decide which index is the starting index. Now that''s flexibility!
It''s not the slightest bit unfortunate. Even if you don''t directly do pointer arithmetic (which is where the idea of starting at 0 comes from - you can just directly add the value (times whatever sizeof(foo)) to the pointer), eventually you''ll find yourself using the result of a calculation to determine an array index - and almost every time, you''ll find the calculation is cleaner with 0-based indices. Unless my experience is highly atypical (which I doubt).

What *is* unfortunate is how much difficulty it seems to cause for some, when there are such simple heuristics for getting it right the first time:

- use strict inequality in loop conditions (< rather than <=; some advocate !=, which works with STL iterators as well as just numerical indices)
- translate between 1-based "numbers intended to be seen by the user" and 0-based "numbers for internal use" only at boundaries of code, the same way that you should (ideally) separate out I/O. In fact, in many cases you''ll want to do the translation in the same breath as the input parsing/output formatting.

It''s actually quite uncommon, at least IME, that expressions with an odd-looking "+1" or "-1" tacked on are correct and that there would be an obiwan error without it. Much much more likely the other way around. As long as you''re following the rules.
Thanks clearsnake it works now
quote:Original post by Zahlman
It''s not the slightest bit unfortunate. Even if you don''t directly do pointer arithmetic (which is where the idea of starting at 0 comes from - you can just directly add the value (times whatever sizeof(foo)) to the pointer)


Pointer arithmetic is unnecessary and error prone (IMO, of course), and furthermore, C and C++ are two of the few languages that even allow it.

Take a look at this.

quote:
eventually you''ll find yourself using the result of a calculation to determine an array index - and almost every time, you''ll find the calculation is cleaner with 0-based indices. Unless my experience is highly atypical (which I doubt).


What the world?! Have you ever taken a course on algorithms that isn''t language-biased towards C? Most of them use 1-based indexing. The most popular book on algorithms uses 1-based indexing.

Introduction to Algorithms, 2nd Edition by Thomas H. Cormen, et al
quote:Original post by clearsnake
Pointer arithmetic is unnecessary and error prone (IMO, of course), and furthermore, C and C++ are two of the few languages that even allow it.

Take a look at this.


What does that link have to do with anything?

quote:Original post by clearsnake
What the world?! Have you ever taken a course on algorithms that isn't language-biased towards C? Most of them use 1-based indexing. The most popular book on algorithms uses 1-based indexing.


a[x] = &a + x * sizeof(x)

a[x] = &a + (x - 1) * sizeof(x)

Zero based arrays save us some arithmetic!

Really though, array indexing should never change, they are a thin abstraction for dealing with a chunk of memory. Personally, i'm unable to think in any other way. Besides, who wants 1-based arrays when dealing with coordinate planes?

Collections are another matter. I don't think anyone would feel like changing gears depending on whether they're working with a vector or array, but this is a place 1-based indexing would be more intuitive.

Dijkstra's argument


[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on June 7, 2004 4:23:31 PM]
[size=2]

This topic is closed to new replies.

Advertisement