Magic Square

Magic Square
-Write a program will accept 9 input values.
-The program will accept 1-9 values.
-The program will not accept duplicate entry.
-The program will check if the summation of input for horizontal, vertical, and ---diagonal are 15.
-The program will check if it’s a magic square or not.
-The program uses arrays and flow controls.


And I only know this

#include <iostream>
using namespace std;
int a[3][3];
//int b[3][3];
int main()
{
cout << "Enter number:" << endl;

for(int i = 0; i <3; i++){
for (int j=0 ; j<3 ; ++j) {
cout << "Num " << (j+1)+3*i << ": ";
cin >> a[i][j];
}
}

for(int i = 0; i <3; i++){
for (int j=0 ; j<3 ; ++j) {
a[i][j] = a[i][j];

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

cout << a[i][j] << " ";
}
cout << endl;
}

}

Pls help
First of all, try using code tags next time you post code, it improves the readability a lot (and your chance of others answering your question).

I'll try to help you step by step. The first step would be to remove your second loop, since it does absolutely nothing (it assigns the same value to itself every time, which isn't what we want. Your code would then look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int main()
{
    int a[3][3];
    for(int i = 0; i <3; i++)
    {
        for (int j=0 ; j<3 ; ++j)
        {
            cout << "Num " << (j+1)+3*i << ": ";
            cin >> a[i][j];
        }
    }

    for(int i = 0; i <3; i++)
    {
        for (int j=0 ; j<3 ; ++j)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}


Your first task was to check whether your input numbers are between 1 and 9. I'll combine this with the duplicate entries part of the assignment.

To check for duplicates, we should somehow store if we already encountered that number already. We could do this using a small bool array with nine elements, since we only want to check valid numbers.

 
bool encountered[9] = {}; //Initialize an array of 9 bool's to false 


Next in the input loop, we should check whether the input is between 1 and 9, and if so, check whether it is a duplicate. If so, we'll ask again:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int a[3][3];
for(int i = 0; i <3; i++)
{
    for (int j=0 ; j<3 ; ++j)
    {
        cout << "Num " << (j+1)+3*i << ": ";
        cin >> a[i][j];
        if(cin.fail()) //Check if input failed, for example if you enter a string
        {
            std::cerr << "Invalid input" << std::endl;
            cin.clear(); //Restore the error bit, to allow us to use cin again
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //Skip to the next line
            --j; //See below
            continue; //Try again
        }
        if(a[i][j] > 9 || a[i][j] < 1)
        {
            std::cerr << "Invalid number, should be between 1 and 9" << std::endl;
            --j; //Decrease j by one, so we will repeat this iteration again
        }
        else if(encountered[a[i][j]]-1) //Check whether we already had this number
        {
            std::cerr << "You already entered that number" << std::endl;
            --j; //Same as above
        }
        else //Valid input
        {
            encountered[a[i][j]-1] = true; //Set this number as encountered, so it may not be entered again
        }
    }
}


I hope the code is clear enough, if there are any question, just ask. In case you want help with checking whether the input is a "magic square", you should post the definition of a magic square. It might be that I'm the only one, but I have absolutely no idea what that is.
Last edited on
@Shadowwolf

The definition of a 'Magic Square' was given in the original post
-The program will check if the summation of input for horizontal, vertical, and ---diagonal are 15.


So...
8 1 6
3 5 7
4 9 2

is a magic square, since each row, column and diagonal, equals 15
Is that the definition of magic square? I thought it was just another requirement of the program that wasn't related to the others. Sorry, my bad then.
Topic archived. No new replies allowed.