Solved

Trouble with a two dimensional array - one element replaceing another

Posted on 2004-04-22
4
344 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
ID: 10895349
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
ID: 10895355
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
ID: 10895367
Wow, thank you... I knew it was something stupid... heh
0
 
LVL 5

Expert Comment

by:rendaduiyan
ID: 10895372
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

809 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