noobiee
noobiee

Reputation: 27

Display the sum of both sides in 2-D array

I'm having difficulties in displaying the sum of each row and column in the magic square. My algorithm of the magic square is fine but when I'm trying to display the sum of each side it kinds of messing up my magic square and it gives me large and negative values. I have a hunch that it was something to do with my indexing and I am spending quite some time figuring out why I can't display it properly. any help would be appreciated ty.

A magic square is an arrangement of the numbers from 1 to n^2 (n-squared) in an nxn matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same. It is not hard to show that this sum must be n(n^2+1)/2.

to solve this place 1 at the first row middle of the square then for the next number move up then left, if the index is occupied place it 1 down row.

sorry i am not that good in explaining it in English. thanks

#include <iostream>

using namespace std;

int main(){

    int n,x,y;

    cout<<"Enter size of the magic square " <<endl;
    cout<<"Must be odd number starting from 3: ";
    cin>>n;
    while(n%2==0){              //condtion if the entered value is even
        cout<<"Please Enter Odd number: ";
        cin>>n;
    }
    cout<<"\n\n";

    int array[n][n];            //create 2d matrix

    for( x=0;x<n;x++){              //initialize the value of your 2d matrix as 0
        for( y=0;y<n;y++){
            array[x][y] = 0;        
        }   
    }



    int row = 0;            //your row starting position
    int col = n/2;          //your colum starting position

    array[row][col] =1;     //position of your first counting number 

    for (int i = 2; i <= n*n; ++i)      //this is working   algorith is up left
    {
        if((i-1)%n == 0)            //if it is occupied go down
        {
            row++;
        }
        else  
        {
            row--;               
            row = (row+n)%n;

            col -=1;
            col%=n-1;
        }
        if (col<0){
            col = col+n;
        }
        array[row][col] = i;


    }                               //up to here


    for(x=0;x<n;x++){               //display sum in the side
        for(y=0;y<n;y++){
            array[x][n]+=array[x][y];
        }
    }

    for(x=0;x<n;x++){               //display sum at the bottom
        for(y=0;y<n;y++){
            array[n][x]+=array[x][y];
        }
    }

    for(x=0;x<=n;x++){              //display your matrix.
        for(y=0;y<=n;y++){
            cout<<"\t" <<array[x][y]  <<"  ";   
        }
        cout<<"\n\n";
    }

}

Upvotes: 0

Views: 155

Answers (1)

R Sahu
R Sahu

Reputation: 206607

Use of

int array[n][n];            //create 2d matrix

is supported only on some platforms as an extension. I strongly suggest changing it to use std::vector.

std::vector<std::vector<int>> array;

Also, you use:

        array[n][x]+=array[x][y];

and

        array[x][n]+=array[x][y];

Those lines cause undefined behavior since n is not a valid index.

Also, in both cases, you are accumulating the values from the row. The second line needs to accumulate the values from the column. You need to use array[y][x].

You would like to display the sum of the rows and sum of the columns as the final output along with the elements of the 2D array. For that, it will be easier to create a n+1 X n+1 matrix. You can do that by using:

std::vector<std::vector<int>> array(n+1, std::vector<int>(n+1));

The logic for computing the sum of the rows and storing them in the n+1-th column needs to be:

for(x=0;x<n;x++){
   array[x][n] = 0;
   for(y=0;y<n;y++){
     array[x][n] += array[x][y];
   }
}

The logic for computing the sum of the columns and storing them in the n+1-th row needs to be:

for(x=0;x<n;x++){
  array[n][x] = 0;
  for(y=0;y<n;y++){
     array[n][x] += array[y][x];
  }
}

See it working at https://ideone.com/XBzUr0.

Upvotes: 2

Related Questions