Thread: Code help

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    73

    Code help

    The following code should determine whether or not a square of numbers is a magic square or not. The code debugs and run just fine, but the command prompt opens, asks me how many squares I would like to calculate for, and after I hit enter with a number, it just sits there and does nothing.

    Can anyone tell me why it does that? Have I done anything wrong?

    Code:
    #include <iostream>#include <fstream>
    #include <iomanip>
    using namespace std;
    
    
    const int size = 5;
    void square (int x[size][size]);
    void heading();
    
    
    ifstream infile;
    ofstream outfile;
    
    
    int main()
    {
        infile.open("input.txt");
        outfile.open("output.txt");
    
    
        int c;
    
    
        cout << "How many squares would you like to determine?  ";
        cin >> c;
        cout << endl;
    
    
        heading;
        outfile << "The user of this program has chosen to determine whether " << c << " squares are magic squares." << endl << endl;
    
    
        int x[size][size];
    
    
        for (int c = 1; c > 0; c++)
        {
            for (int d=0; d<size; d++)
                for (int e=0; e<size; e++)
                    infile >> x[d][e];
    
    
            square(x);
        }
    
    
        system("pause");
        return 0;
    }
    
    
    // This function writes the heading in the outfile.
    void heading()
    {
        outfile << "Trey Brumley" << endl;
        outfile << "CMPS 1063 - Johnson" << endl;
        outfile << "January 29, 2013" << endl;
        outfile << "Program 1 - Magic Squares" << endl;
        outfile << "=========================" << endl << endl;
    
    
        outfile << "This program will take a certain number of number squares as determined by the user, and it will compare the sums of each row, column, and diagonal." << endl;
        outfile << "A magic square is a square of numbers with a constant sum of each of its rows, columns, and diagonals." << endl << endl;
    
    
        return;
    }
    
    
    // This function will test a square one row at a time
    void square (int x[size][size])
    {
        infile.open("input.txt");
        outfile.open("output.txt");
    
    
        int sum = 0, sum2 = 0;
        int y=1, z=1;
    
    
        //This and the following loop will determine a sum, which will remain constant. 
        //A second sum of the next row will be determined, which will repeatedly be tested against the first sum.
        //If the sums are equal, the loop will continue to run.  If not, the function will end.
        for (y; y <= 5; y++)
            sum += x[y][z];
            while (z<5)
            {
                sum2 = 0;
    
    
                for (y=1; y<=5; y++)
                    sum2 += x[y][z];
    
    
                if (sum2 != sum)
                {
                    outfile << "This square is not a magic square." << endl;
                    return;
                }
    
    
                else
                    z++;
            }
    
    
        for (z=1; z<=5; z++)
            while (y<=5)
            {
            sum2 = 0;
            sum2 += x[y][z];
            if (sum2 != sum)
            {
                outfile << "This square is not a magic square." << endl;
                return;
            }
            else
                y++;
            }
    
    
        sum2 = 0;
    
    
        //The following loops will test diagonals.
        for (z=1; z<=5; z++)
        {
            y=z;
            sum2 += x[y][z];
            if (sum2 != sum)
            {
                outfile << "This square is not a magic square." << endl;
                return;
            }
        }
    
    
        sum2 = 0;
    
    
        for (z=1; z<=5; z++)
            {
                y=6-z;
                sum2 += x[y][z];
                if (sum2 != sum)
                {
                    outfile << "This square is not a magic square." << endl;
                    return;
                }
            }
    
    
        outfile << "This square is a magic square.  The constant sum is " << sum << "." << endl;
    
    
        return;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The cause of your problem is the for loop
    Code:
    for (int c = 1; c > 0; c++)
    c starts with a positive value, is only ever incremented, so is always positive - so c > 0 is always true. Hence an infinite loop. (Strictly speaking, the program will eventually exhibit undefined behaviour when c overflows, so the loop isn't necessarily infinite, but will iterate at least 32766 times before that happens so your program will appear to hang).


    Unrelated to your question, but the line
    Code:
    heading;
    does nothing (except evaluate the address of the function named heading). If you want the function to be called, it is necessary to do
    Code:
    heading();
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    First of all this is wrong:
    Code:
    void square (int x[size][size])
    What you can write is
    Code:
    void square (int x[][size], int size)
    or
    Code:
    void square (int x[][], int size)
    The compiler ignores the first parameter. Why you didn't an error? Because, int size is a global variable (not good technique, I can explain why if you wish after you resolve your infinite loop!).

    Ok, so you have an infinite loop, I guess inside square function.

    Also, the index of the arrays, start at zero, not at one! (learn this one and for all! You are not fortran programmer!).

    Then I would check if the files really open. On line 42 I would check if the x array (x is not really a good name a suppose, but ok), is really filled up as you expect it is. Then I would fill square function with cout's on lines that are prone to infinite loops and I would run the code with a small amount of data. That way you debug yourself the code and you feel good

    EDIT: Well obviously, grumpy found the mistake while I was typing, but I think you will be benefited a bit by reading my post
    Last edited by std10093; 02-06-2013 at 06:46 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>int x[][]
    You can't do that. You must specify the outer dimension, so it must be something like
    int x[][size]
    Where size is some constant expression (eg, a number).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Correct, so you use my first suggestion
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    Okay, so I edited the code. The offending code now looks like this:

    for (c; c > 0; c--)

    which should work, but now nothing at all is printing. Not even "The user has chosen to determine whether X squares are magic squares," which was printing just fine earlier.
    Last edited by Trey Brumley; 02-07-2013 at 09:53 AM.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hmm.. Can you post a bit more code?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    Quote Originally Posted by std10093 View Post
    Hmm.. Can you post a bit more code?
    Code:
    #include <iostream>#include <fstream>#include <iomanip>
    using namespace std;
    
    
    const int size = 5;
    void square (int x[size][size]);
    void heading();
    
    
    ifstream infile;
    ofstream outfile;
    
    
    int main()
    {
        infile.open("input.txt");
        outfile.open("output.txt");
    
    
        int c;
    
    
        cout << "How many squares would you like to determine?  ";
        cin >> c;
        cout << endl;
    
    
        heading();
        outfile << "The user of this program has chosen to determine whether " << c << " squares are magic squares." << endl << endl;
    
    
        int x[size][size];
    
    
        for(c; c > 0; c--)
        {
            for (int d=0; d<size; d++)
                for (int e=0; e<size; e++)
                    infile >> x[d][e];
    
    
            square(x);
        }
    
    
        system("pause");
        return 0;
    }
    
    
    // This function writes the heading in the outfile.
    void heading()
    {
        outfile << "Trey Brumley" << endl;
        outfile << "CMPS 1063 - Johnson" << endl;
        outfile << "January 29, 2013" << endl;
        outfile << "Program 1 - Magic Squares" << endl;
        outfile << "=========================" << endl << endl;
    
    
        outfile << "This program will take a certain number of number squares as determined by the user, and it will compare the sums of each row, column, and diagonal." << endl;
        outfile << "A magic square is a square of numbers with a constant sum of each of its rows, columns, and diagonals." << endl << endl;
    
    
        return;
    }
    
    
    // This function will test a square one row at a time
    void square (int x[size][size])
    {
        infile.open("input.txt");
        outfile.open("output.txt");
    
    
        int sum = 0, sum2 = 0;
        int y=1, z=1;
    
    
        //This and the following loop will determine a sum, which will remain constant. 
        //A second sum of the next row will be determined, which will repeatedly be tested against the first sum.
        //If the sums are equal, the loop will continue to run.  If not, the function will end.
        for (y; y <= 5; y++)
            sum += x[y][z];
            while (z<5)
            {
                sum2 = 0;
    
    
                for (y=1; y<=5; y++)
                    sum2 += x[y][z];
    
    
                if (sum2 != sum)
                {
                    outfile << "This square is not a magic square." << endl;
                    return;
                }
    
    
                else
                    z++;
            }
    
    
        for (z=1; z<=5; z++)
            while (y<=5)
            {
            sum2 = 0;
            sum2 += x[y][z];
            if (sum2 != sum)
            {
                outfile << "This square is not a magic square." << endl;
                return;
            }
            else
                y++;
            }
    
    
        sum2 = 0;
    
    
        //The following loops will test diagonals.
        for (z=1; z<=5; z++)
        {
            y=z;
            sum2 += x[y][z];
            if (sum2 != sum)
            {
                outfile << "This square is not a magic square." << endl;
                return;
            }
        }
    
    
        sum2 = 0;
    
    
        for (z=1; z<=5; z++)
            {
                y=6-z;
                sum2 += x[y][z];
                if (sum2 != sum)
                {
                    outfile << "This square is not a magic square." << endl;
                    return;
                }
            }
    
    
        outfile << "This square is a magic square.  The constant sum is " << sum << "." << endl;
    
    
        return;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM