need help new to C programming

C

celsius

given below is a program to find magic square of odd order n, but it
is not giving the desired output.could someone please help me

#include<stdlib.h>
#include<stdio.h>

int *m;

int main(void)
{
int i,n;
int val,row,col;
int n_row,n_col;


puts("enter the order of the magic square....");
scanf("%d",&n);

if( (n%2) == 0)
{
puts("order of magic square an odd number:");
exit(1);
}

m = (int*)malloc( (n * n) * sizeof(int));

if(!m)
{
puts("memory allocation failed...:");
exit(1);

}

for(i = 0;i < (n * n);i++)
*(m+i) = 0;

row = 0;
col = n/2;

*(m +(row + col) ) = 1;

for(val=2; val <= (n*n); val++)
{
if(row > 1)
n_row = row-1;
else
n_row = (n-1);

if(col < n-1)
n_col = col+1;
else
n_col = 0;

if( *(m + (n_row *n) + n_col) != 0)
{
n_row = row+1;
n_col = col;
}

*(m + (n_row * n) + n_col) = val;
row = n_row;
col = n_col;

}


for(i = 0;i < (n * n);i++)
{
if( (i%n) == 0 )
puts("");

printf("%d \t",*(m+i));
}


free(m);
return(0);
}
 
J

Joerg Schoen

celsius wrote:

see my comments below
#include<stdlib.h>
#include<stdio.h>

int *m;

why is this global. Local variable would be nicer.
int main(void)
{
int i,n;
int val,row,col;
int n_row,n_col;


puts("enter the order of the magic square....");
scanf("%d",&n);

if( (n%2) == 0)
{
puts("order of magic square an odd number:");
exit(1);
}

m = (int*)malloc( (n * n) * sizeof(int));

if(!m)
{
puts("memory allocation failed...:");
exit(1);

}

for(i = 0;i < (n * n);i++)
*(m+i) = 0;

A better style would be "m = 0"
row = 0;
col = n/2;

*(m +(row + col) ) = 1;

That should actually be "m[row * n + col]= 1;", but luckily, it doesn't
hurt because of row == 0
for(val=2; val <= (n*n); val++)
{
if(row > 1)

First real mistake: Should be "if(row > 0)". Your row/column
notion is 0-based!
n_row = row-1;
else
n_row = (n-1);

The whole thing could be easier written as
n_row = (row > 0) ? row - 1 : n -1;
if(col < n-1)
n_col = col+1;
else
n_col = 0;

if( *(m + (n_row *n) + n_col) != 0)

Again, why not "m[n_row * n + n_col]"?
{
n_row = row+1;

Second real mistake: Missing line is here

if(n_row >= n) n_row = 0;
n_col = col;
}

*(m + (n_row * n) + n_col) = val;

"m[n_row * n + n_col] = val;
row = n_row;
col = n_col;

}


for(i = 0;i < (n * n);i++)
{
if( (i%n) == 0 )
puts("");

printf("%d \t",*(m+i));
}


free(m);
return(0);
}

Joerg
 
M

Michael Fyles

celsius said:
given below is a program to find magic square of odd order n, but it
is not giving the desired output.could someone please help me
[snip]

for(val=2; val <= (n*n); val++)
{
if(row > 1)

I think you mean
if (row >= 1)
n_row = row-1;
else
n_row = (n-1);

putchar('\n');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top