I am fairly new at C++ and Im having a lot of trouble understanding how to do it. This is part of a homework assignment due next week and I have no idea how to even start it (much less finish coding it). Can anyone out there help?
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Create a 5X5 array, that is filled with integers 1, 2, 3, …25. (1 to 25) Each slot in the array must hold a unique value (ie. no number is repeated)

Write a program that reads in 25 integer values from the user and tests whether these two conditions are true.

•does each of the numbers 1 to 25 occur in the user input once each (this condition must be met)
•when the numbers are put into the array, are the sums of the rows, columns and diagonals equal to each other?
Output: a printout of the 5 x 5 array with each row of data on a separate line and a message of whether it meets the second condition (the first condition MUST be met).

Recommended Answers

All 2 Replies

So, this is a 3-part problem. Solve each separately, and then see if you need to make some changes to make them all work together. Example of a possible issue to bring up with your professor is #2, "when the numbers are put into the array, are the sums of the rows, columns and diagonals equal to each other?". My question is, when examining the semantics of the question, is whether he/she wants to simply know if they are equal, or does he/she want you to make them equal? Very different things, for sure.

So. Problem #1 - remember each number input and check if any have already been input. Problem #2 - well, this goes back to my previous query about the professor's intent. Problem #3. And after reading #3 about the output, I have to assume that the intent is not so much that you make every row/column/diagonal meet the conditions, but just to test if they do.

So, go back to problem #1 - what would you do, taking one input at a time from a user, to populate a 5x5 array. Consider that the array could be defined as such:

int array[5][5];

And then, what would you do as each item is input to see if it has not been input yet? One way is as follows:

// Make sure we initialize the array to known values NOT >=1 OR <= 25
// Zero is good for this case.
int array[5][5] = {{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};

// For each row
for (size_t crow = 0; crow < 5; crow++)
{
    // For each column
    for (size_t ccol = 0; ccol < 5; ccol++)
    {
        int i = getInput(); // getInput() will get the user input and return value as int.
        if (i < 1 || i > 25)
        {
            // Input range error!
        }
        else
        {
            // Look for input.
            for (size_t y = 0; y < crow; y++)
            {
                for (size_t x = 0; x < ccol; x++)
                {
                    if (array[y][x] == i)
                    {
                        // Duplicate input error.
                    }
                }
            }
        }
        // If no errors
        array[crow][ccol] = i;
    }
}

This code snippit (except the error handling which I leave to you) will solve problem #1. After all the 25 values are input, you need to determine if you have a solution to problem #2. Problem #3, the output, will display what you have found.

Oh, and you need to implement the getInput() function as well... :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.