Reputation: 13
Hi i'm very new to programming and i started messing around with two dimensional arrays. I'm getting a this runtime error "Run-Time Check Failure #2 - Stack around the variable 'pair' was corrupted.". If someone could help me that would be great thanks.
#include<stdio.h>
#include<iostream>
int main(void)
{
int pair[2][2];
pair[0][0] = 1;
pair[0][1] = 1;
pair[0][2] = 1;
pair[1][0] = 0;
pair[1][1] = 0;
pair[1][2] = 0;
pair[2][0] = 0;
pair[2][1] = 0;
pair[2][2] = 0;
if((pair[0][0] == 1 && pair[0][1] == 1 && pair[0][2] == 1) || (pair[0][0] == 2 && pair[0][1] == 2 && pair[0][2] == 2))
{
printf("Match!\n");
system("PAUSE");
}
else
{
if((pair[1][0] == 1 && pair[1][1] == 1 && pair[1][2] == 1) || (pair[1][0] == 2 && pair[1][1] == 2 && pair[1][2] == 2))
{
printf("Match!\n");
system("PAUSE");
}
else
{
if((pair[2][0] == 1 && pair[2][1] == 1 && pair[2][2] == 1) || (pair[2][0] == 2 && pair[2][1] == 2 && pair[2][2] == 2))
{
printf("Match!\n");
system("PAUSE");
}
else
{
printf("Nope!\n");
system("PAUSE");
}
}
}
} `
Upvotes: 1
Views: 578
Reputation: 101
you declared the foloowing two dimentionnal array:
int pair[2][2];
From my point of view you can't do:
pair[2][1] = 0;
pair[2][2] = 0;
unless you declare a larger array : int pair[3][3];
Upvotes: 1
Reputation: 2038
You have defined 2d array with 2 rows and 2 column and you are trying to access third..remove all the below references from your code (including in if conditions)... hope that will help..
pair[1][2] = 0;
pair[2][0] = 0;
pair[2][1] = 0;
pair[2][2] = 0;
Upvotes: 0
Reputation: 117856
Your array is only 2 by 2.
int pair[2][2]
So the only legal indexes are [0] and [1]. You want:
int pair[3][3]
Which will allow [0] [1] and [2]
Upvotes: 2