Hey guys, first post. I am at the end of the semester and running ito some trouble. I know this question has been askd a few months ago but I looked at the code that was supplied and it didnt work. I cant get it started, I think i can get the second part to work, checking if the values add to equal eachother. Any help is appreciated.

Array Sums

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 validates that each number is input exactly once (you did this in lab 10).

When all the values are read into the two-dimensional array, test to see if the following condition holds:

• 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).

Have you written any code at all? Show us what you have working.

include <iostream>
include <stdio.h>

using namespace std;
/* 5x5.c */
int main(void) 
{ int array[5][5];
int i, j; int sum;
for (i = 0; i < 5; ++i) 
for (j = 0; j < 5; ++j) 
scanf("%d", &(array[i][j]));
for (i = 0; i < 5; ++i) 
{
//checks for rows and coloums adding to same number
sum = 0; for (j = 0; j < 5; ++j)
sum += array[i][j]; printf("row sum [%d][] = %d\n", i, sum); }
for (j = 0; j < 5; ++j) { sum = 0;
for (i = 0; i < 5; ++i) sum += array[i][j];
printf("column sum [][%d] = %d\n", j, sum); }
return 0; 
}

This is what I have so far but it isnt getting the right numbers for the array. thanks again for the reply

What kind of output are you getting?
Are you at least getting the numbers input to the array correctly?

After I enter 25 digits, even if they are all the same, it just stops and does the "press any key to close" option.

Have you tried stepping through the program one line at a time?

You can do a couple things:
1) step through the program, and after each array element is input, view it in the debugger.
2) insert an output statement right after the input statement; then you can see if all the array elements are being input correctly.

Once you've confirmed all the array elements are being entered correctly, the rest is pretty easy.

How do I get it to allow me to input into the array rather then having the array auto generate numbers?

#include <iostream>
#include <iomanip>
//notice the addition of iomanip this gives us access to setw
// 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;
    }
}
int main(void)
{
//where it all come to gether
long seed=0;

do {
seed = GetNumber();
cout << "You've entered: " << seed << "\n";
if(seed > 0) PrintTable(seed);
cout << "\n\n";

} while(seed != 0); //if 0 is entered we quit

return 0;
}

That should work but it generates an error that i dont know,
4\PRS2\r\main.cpp|8|error: expected unqualified-id before 'for' 4\PRS2\r\main.cpp|8|error: expected constructor, destructor, or type conversion before '<' token|
4\PRS2\r\main.cpp|8|error: expected constructor, destructor, or type conversion before '++' token|
||=== Build finished: 3 errors, 0 warnings ===|

//notice the addition of iomanip this gives us access to setw
// 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 will not compile. It's not in a function, nor is it in main.. How do you expect for it to compile? This is why you're getting that error.

so do I need to put it inside the Main()?

@batman189 - I guess so. Give it a try =)

tried that, it now says that getInput() cannot be used as a function.

Did you implement getInput() outside main()? Its not a built-in function and you need to provide its implementation. Also, try to wrap the above code in a separate function (rather than copying everything inside main) and try using that function from inside main. This way, you'll have more modularity and you'll better understand your code
(You didn't write the above code snippet yourself, did you?)

In short, the break-up would be something like this (roughly) :

// #define SIZE as 5
#define SIZE 5

// define the various modules:
void takeInput (int arr[][SIZE], sizeOfArr) 
{ 
    // the above snippet... 
}

int isEqualSum (int arr[][SIZE], sizeOfArr) 
{
    // check for the 2nd condition here and return 0 or 1 accordingly... 
}

void printArrLine (int arr[][SIZE], sizeOfArr) 
{ 
    // print as per your requirements...
}


// now, integrate all the modules from within main:
int main (void) 
{
    // declare array

    // call takeInput by passing the array and its size
    takeInput (arr, SIZE);

    // check if 2nd condition is true
    if (isEqualSum (arr, SIZE))
    {
        // call another helper function to print the array
        printArrLine (arr, SIZE);
        std::cout<<"meets second condition";
    }

    return 0;
}

Try that and let us know... Happy coding!

NP-No i didnt write it myself, this question has been asked before so I tried to pick up from there and was just as lost as trying to start it myself. I get the basic idea of how this stuff is supose to work and how the array and loops function, my issue is just getting it in the right order I think. I will try that as soon as I get home, thank you for your reply, all of you guys.

#include <iostream>
#include <stdio.h>
using namespace std;


int main(void)
{
int i,j,l=1, array[5][5];
for(i=0;i<5;i++)
for(j=0;j<5;j++)array[i][i]=l++;
scanf("%d", &(array[i][j]));

for (i = 0; i < 5; ++i) {
int sum = 0;
for (j = 0; j < 5; ++j)
sum += array[i][j];
printf("row sum [%d][] = %d\n", i, sum);
}

for (j = 0; j < 5; ++j) {
int sum = 0;
for (i = 0; i < 5; ++i)
sum += array[i][j];
printf("column sum [][%d] = %d\n", j, sum);

}

return 0;
}

I updated the code alot after lab today, it still doesnt do what I want it to do and I cant figure out why....ugh

the code above is not meeting with your requirements...you're not validating your input, you're not checking the sum condition either!

Why don't you follow the modular approach I discussed in the previous post? It'll be much simpler for you to understand the flow and you can debug yourself then.

Also, try to put curly braces for every for loop. Removing them can gain you one less line but cost you a lot on readability. (you haven't for the loops on line 9, 10, 15 and 22)

But, did you omit them on purpose or did you simply missed them?

Anyway, follow the modular approach and we'll be more than happy to help.

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.