lundi 27 janvier 2020

Why am I getting a "Run-Time Check Failure #2 - Stack around the variable 'pr' was corrupted" Error?

It happens in void numgeneratorinator(int ar[]) whenever I try to run run the program. The program itself is supposed to generate as many magic squares as the user needs, then checks how many of those are magic squares. I have that down however this number generator is not working with me and I keep getting the "Run-Time Check Failure #2 - Stack around the variable 'pr' was corrupted" Error. It seems to "work" when I change pr[9] to pr[10] but then when I print the matrix as a test it has a zero in it and after 1 run it results in the matrix having a really low number in it (like -83289).

#include <ctime>
#include <cstdlib>
#include <iostream>
#include <iomanip> //Used for printinator

void printinator(int a[][3]) //prints a matrix
{
    using namespace std;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            cout << fixed << setprecision(2) << setw(12) << a[i][j] << "  ";
        cout << endl;
    }
    cout << endl;
}

int Checkinator(int m[][3]) //https://www.codespeedy.com/how-to-check-the-given-matrix-is-magic-square-or-not-cpp/ This function checks to see if the created matrix is M A G I C A L
{
    int s1 = 0, s2 = 0;

    for (int i = 0; i < 3; i++)
        s1 += m[i][i];

    for (int i = 0; i < 3; i++)
        s2 += m[i][3 - 1 - i];

    if (s1 != s2)
        return 0;

    for (int i = 0; i < 3; i++) {

        int rs = 0;
        for (int j = 0; j < 3; j++)
            rs += m[i][j];

        if (rs != s1)
            return 0;
    }

    for (int i = 0; i < 3; i++) {

        int cs = 0;
        for (int j = 0; j < 3; j++)
            cs += m[j][i];

        if (cs != s1)
            return 1;
    }

    return true;
}

void numgeneratorinator(int ar[])
{
    int pr[9] = { 1,2,3,4,5,6,7,8,9 };

    for (int i = 9; i > 1; --i)
    {
        int j = rand() % i;
        int temp = pr[i];
        pr[i] = pr[j];
        pr[j] = temp;
    }
    for (int i = 1; i < 9; ++i)
        ar[i] = pr[i];
}

int main()
{
    int tr;
    srand(time(0));
    char more = 'y';

    using namespace std;
    while (more == 'y' || more == 'Y')
    {
        cout << "\n\t\tHow many randomly generated matrix would you like to test? : ";
        cin >> tr;
        int res = 0;
        int ra = 0;
        int ar[9] = {1,2,3,4,5,6,7,8,9};
        int m[3][3] = { {0,0,0}, {0,0,0}, {0,0,0} };
        numgeneratorinator(ar);

            for (int p = 0; p < tr; p++)
            {
                for (int i = 0; i < 3; i++)
                {
                    for (int k = 0; k < 3; k++)
                    {
                        m[i][k] = ar[ra++];
                        if (ra >= 10)
                            ra = 0;
                    }
                }
                if (Checkinator(m) == true)
                    res++;
            }
            cout << "\n\t\t\tThere are " << res << " magic squares after running " << tr << " times.\n";
            printinator(m); //This was used for testing purposes to ensure the random number generator was working
            cout << "\n\t\tWould you like to do another run? : ";
            cin >> more;
    }
}





Aucun commentaire:

Enregistrer un commentaire