473,508 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help new to C programming

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);
}
Nov 14 '05 #1
2 923
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[i] = 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

Nov 14 '05 #2
celsius <at*******@yahoo.co.uk> wrote:
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');
free(m);
return(0);
}


--
Michael
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1840
by: mch2k2 | last post by:
Hello All I have just started working on Pyhton. I need urgent help regarding Python Network Programming. I want the elctronic version of the Book: Foundations of Python Network programming by...
45
2979
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
5
2637
by: mr.iali | last post by:
Hi Everyone I would like to get into software developent using a programming language like c++, java or pl/sql for oracle. I have no idea where to start from. Which language is there more...
2
3854
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
11
2103
by: Larry | last post by:
I will be teaching an eCommerce application development course using ASP.Net after many years of having taught classic ASP. (Course was interrupted by 18 months in the Middle East with my Army...
5
1825
by: Kevin Carne | last post by:
First, Merry Christmas. I have been doing intensive Java programming for two years in school (no choice on the language), but now I need to return to C++ because my resume is filled with it both in...
9
5226
by: Brian Hampson | last post by:
I am trying to determine all the groups which the current user has permissions to add a member. Here's my code: foreach (System.DirectoryServices.SearchResult ADSearchres in...
2
1833
by: _A_C-ker_ | last post by:
Hello geek(s), I'm in desperate need of graphics programming using C language. Does anyone have material or web-links that explain graphics programming in- detail? I would appreciate your help...
13
3475
by: Javad | last post by:
Hello I know that I should get the information of windows internet connections by using "rasapi32.dll" library, and I also have some sample codes, but I can't make them work. My exact need is to...
25
2053
by: vikram Bhuskute | last post by:
I have plans to train some students for C in coming weeks. I am badly looking for C programming assignments fot them. Need 1) lots of them per topiic 2) Should be doable for beginners thanks...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.