Solved

Trouble with a two dimensional array - one element replaceing another

Posted on 2004-04-22
4
342 Views
Last Modified: 2010-04-01
I have been trying to write a simple program of connect4 using a 4x4 array. It certiantly sounds simple but for some reason I am having problems with the array itself

#include <iostream>
using namespace std;

void main()
{
      int grid[3][3];
      for(int m=0;m<4;m++)
      {
            for(int n=0;n<4;n++)
            {
                  grid[m][n]=0;
            }
      }
      cout<<grid[0][0]<<" "<<grid[0][1]<<" "<<grid[0][2]<<" "<<grid[0][3]<<endl;
      cout<<grid[1][0]<<" "<<grid[1][1]<<" "<<grid[1][2]<<" "<<grid[1][3]<<endl;
      cout<<grid[2][0]<<" "<<grid[2][1]<<" "<<grid[2][2]<<" "<<grid[2][3]<<endl;
      cout<<grid[3][0]<<" "<<grid[3][1]<<" "<<grid[3][2]<<" "<<grid[3][3]<<endl;
      cout<<endl;
      grid[1][0]=1;
      grid[2][3]=1;
      cout<<grid[1][0]<<" "<<grid[0][3]<<" "<<grid[2][3]<<" "<<grid[3][0]<<endl;
      grid[0][3]=2;
      grid[3][0]=2;
      cout<<grid[1][0]<<" "<<grid[0][3]<<" "<<grid[2][3]<<" "<<grid[3][0]<<endl;
}

After a significant amount of frusteration I scrapped the entire code and tried this code.  The only problem is, after they are all initalized to 0, it prints out the grid perfectly.  When I go to assign values however, any value I put in grid[1][0] ends up in grid[0][3] and vice versa.  Also anything in grid[2][3] ends up in grid[3][0].  My Dev enviroment is Microsoft Visual C++, I installed it on my other computer just to make sure it wasn't a memory problem, but got the exact same result.

This is my first question here so I am still a bit fuzzy on how this works.
0
Comment
Question by:SpaceMonkey101
  • 2
4 Comments
 
LVL 11

Expert Comment

by:avizit
Comment Utility
int x[10];
means you have 10 integers  x[0] to x[9];


similarly for int grid[3][3];

grid[3][3] is out of bounds.


/abhijit/
0
 
LVL 11

Accepted Solution

by:
avizit earned 125 total points
Comment Utility
changing

 int grid[3][3];  to  int grid[4][4];  made the program work fine .. (atleast on my machine )

/abhijit/
0
 

Author Comment

by:SpaceMonkey101
Comment Utility
Wow, thank you... I knew it was something stupid... heh
0
 
LVL 5

Expert Comment

by:rendaduiyan
Comment Utility
the arry declared is int grid[3][3]
and initialized with 4x4, which is Array boudary write(ABR/ABW).
that is the reason you print such result.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now