ss2180
ss2180

Reputation: 13

c++ "Run-Time Check Failure #2 - Stack around the variable 'pair' was corrupted."

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

Answers (3)

Refresh It Solutions
Refresh It Solutions

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

HadeS
HadeS

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

Cory Kramer
Cory Kramer

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

Related Questions