0

I have made a programm that determines if your integers make a magic square. There is a 2D array, which i would like to fill with numbers, but as you may know the square consists of values from 1 to (N*N) at the moment the loop checks if the integer is greater than 0 and if its not out of boundries. What I wanted to do is to check if input integer is unique, that is, if it is not used in array before. I tried to do something with another array which is filled with ones, and a do loop , but i had some problems, to execute loop as long as i get error message, that made me belive that it was just a crappy idea to try that, or it was poorly executed. Maybe anyone of you has an idea of how to execute this loop and restart it. I'd like to know it it was the right way with another array and a do loop Here is the original loop:

for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
{
    cin >> arr[i][j];              ///Cikls ar kura palidzibu tiek aizpildits magiskais kvadrats
    if(arr[i][j] <= 0 || arr[i][j]>(N*N))
        {
            i=0;
            j=0;
            cout << error<< endl;
            cin >> arr[i][j];

        }
}

As for the full code it is a long and structured in a weird manner and the comments are not in english aswell, but im willing to edit it in by request if it will do any good. I'm using no libraries.

1
  • Where is your code that tries to check for uniqueness? Why does your loop set i and j to zero when it sees a value out of bounds? Commented Dec 9, 2014 at 16:42

2 Answers 2

1

First of all, the logic that you've implemented is flawed, as it cannot support more than one input mistake (inserting a number outside those boundaries).

Supposing that the client is expected to insert an integer of the square one by one, putting this inside your loop should work.

arr[i][j] = -1;
do {
    cin >> arr[i][j];
    if (arr[i][j] <= 0 || arr[i][j]>(N*N)) {
        cout << error << endl;
    } else {
        break;
    }
} while (true);

And as for the verification of uniqueness, I would recommend inserting the user-input numbers in a set, a practical solution to achieve a matrix of distinct numbers (just put the numbers already in the matrix into the set, then check whether new input numbers are in the set). Modifying the whole code:

std::set<int> numbers;
for (int i=0; i<N; i++) {
    for (int j=0; j<N; j++) {
        arr[i][j] = -1;
        do {
            cin >> arr[i][j];
            if (arr[i][j] <= 0
                    || arr[i][j]>(N*N)
                    || numbers.find(arr[i][j]) != numbers.end()) {
                cout << error << endl;
            } else {
                numbers.insert(arr[i][j]);
                break;
            }
        } while (true);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

As per your problem one solution might be like:

You can create one Buffer Array that will have the value as 1 at particular index which value is already inserted in your 2-D array.

i.e.

if 2-D array is having value 3 then in buffer array at index 3 put 1. so you can check by this buffer array no need to go through to the all elements in 2-D array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.