Solved

Trouble with a two dimensional array - one element replaceing another

Posted on 2004-04-22
4
346 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 175
C++ Code Issue 4 48
How to add columns to a RAD Studio FMX grid at design time 1 59
why doesn't assert terminate here? 2 18
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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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…

734 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