Solved

Trouble with a two dimensional array - one element replaceing another

Posted on 2004-04-22
4
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

623 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