Please help me in Magic square

im just a novice student!! i need help

Page 1 of 1

7 Replies - 2380 Views - Last Post: 19 October 2009 - 05:43 AM Rate Topic: -----

#1 iwanttobecomeanexpert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-October 09

Please help me in Magic square

Posted 16 October 2009 - 05:58 AM

hi everyone..this is my first time to post here...please someone will help me in my project??in C program (yes project not assignment)..i dont have an idea about array...our teacher did not discuss well in 2 dimensional array becoz she was sick (i hope she will die T_T)thats why she give a project..and a sample output of two dimensional array but i cant understand(discussion is better than reading) i dont want to copy the code from my classmate i hate cheating ^_^ (cheating==help) sorry guys but i really need your help im not and IT or CompSci student ..im just a third year high school (im not kidding)..rewrite the code if you want ^_^ your help will greatly much appreciated...thanks...and sorry for my wrong grammar

sample output:

Enter the number for m: 3
Enter the 9 integers: 2 7 6 9 5 1 4 3 8
Table Format:
2 7 6
9 5 1
4 3 8

Remarks: It is a magic square!


the number of integer depends in number you entered on m

i made this code in 3 days T_T


#include <stdio.h>
void input (int m[][]);
void display (int m[][]);
int main()
{
	int m[][],M,n;
	printf("Enter the number for m:");
	scanf("%d",&M);
		n=M*M;
	input(m);
	display(m);
	return 0;
	}
void input (int m[][])
{
	int ctr,no;
	printf("Enter the %d integers:"n);
	for (ctr=1;ctr<=n;ctr++)
	{
	scanf("%d",m[][]);
	}
}

void display (int m[][])
{
	int no;
	printf("I dont know what im going to do please help");
	}
	


This post has been edited by iwanttobecomeanexpert: 16 October 2009 - 06:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Please help me in Magic square

#2 hackterr  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 293
  • Joined: 13-August 09

Re: Please help me in Magic square

Posted 16 October 2009 - 07:01 AM

View Postiwanttobecomeanexpert, on 16 Oct, 2009 - 04:58 AM, said:

hi everyone..this is my first time to post here...please someone will help me in my project??in C program (yes project not assignment)..i dont have an idea about array...our teacher did not discuss well in 2 dimensional array becoz she was sick (i hope she will die T_T)thats why she give a project..and a sample output of two dimensional array but i cant understand(discussion is better than reading) i dont want to copy the code from my classmate i hate cheating ^_^ (cheating==help) sorry guys but i really need your help im not and IT or CompSci student ..im just a third year high school (im not kidding)..rewrite the code if you want ^_^ your help will greatly much appreciated...thanks...and sorry for my wrong grammar

sample output:

Enter the number for m: 3
Enter the 9 integers: 2 7 6 9 5 1 4 3 8
Table Format:
2 7 6
9 5 1
4 3 8

Remarks: It is a magic square!


the number of integer depends in number you entered on m

i made this code in 3 days T_T



hi iwanttobeanexpert its a good problem you have on your hands
your problem is not with understanding of arrays (there are alot of good tutorials for understanding that on the web heres one)
http://www.cplusplus...utorial/arrays/

your problem is to understand the magic square and its properties
if you want to check if the matrix is a magic square you have to do the following
1 check the sum of all its columns
2 check the sum of all its rows
3 check the sum of all its diagnals
if every value from above is the same ... then its a magic square

now if you want to generate a magic square then use de la Loubere's algorithm which can be expressed as :

* Begin by placing a 1 in the middle location of the top row:

* Now put successive integers in an upward-right diagonal path.

*
o If the upward-right movement results in a location outside the boundaries of the square, place the new number at the opposite end of the row or column that would contain the new number, if the rows and columns were not bounded.
o If the upward-right square is already occupied, place the new number directly below the current one.

Convert that to pseudo code first then translate it into a C program.

This post has been edited by hackterr: 16 October 2009 - 07:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 iwanttobecomeanexpert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-October 09

Re: Please help me in Magic square

Posted 16 October 2009 - 10:54 PM

i dont understand c++ >.<
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • Joined: 19-February 09

Re: Please help me in Magic square

Posted 17 October 2009 - 05:52 PM

One possible way :
I've just written this off the top of my head,
so errors are likely.

Create a constant for maximum size of side of square.

#include <stdio.h>

int MAXSIDE = 20;




Allow working size of array to be passed to functions.

void input (int m[][], int size);
void display (int m[][], int size);



Declare array.

int main()
{
	int magic[MAXSIDE][MAXSIDE];
		int M;

	printf("Enter the number for m:");
	scanf("%d",&M);

	   if(M > MAXSIDE)
		  return(1);

	input(magic, M);
	display(magic, M);

	return 0;
}



Display function can be something like this.

void display (int m[][], int size)
{
	int row, col;

	for(row=0; row<size; row++) 
	{
	   for(col=0; col<size; col++)
	 printf(" %d", m[row][col]);

	   printf("\n");
	 }
}	
	


Was This Post Helpful? 0
  • +
  • -

#5 iwanttobecomeanexpert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-October 09

Re: Please help me in Magic square

Posted 17 October 2009 - 08:35 PM

^_^ thanks sir..i have already a idea..but not so much..
but it helps ^_^
Was This Post Helpful? 0
  • +
  • -

#6 iwanttobecomeanexpert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-October 09

Re: Please help me in Magic square

Posted 18 October 2009 - 12:55 AM

ive work the code but still...>.< please someone T_T
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • Joined: 19-February 09

Re: Please help me in Magic square

Posted 18 October 2009 - 07:16 AM

View Postiwanttobecomeanexpert, on 18 Oct, 2009 - 08:55 AM, said:

ive work the code but still...>.< please someone T_T


You need to post your latest code.
Was This Post Helpful? 0
  • +
  • -

#8 iwanttobecomeanexpert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-October 09

Re: Please help me in Magic square

Posted 19 October 2009 - 05:43 AM

heres my running code but the big problem is how can i sum all rows,coloums and diagonals??please help me give even only a code fragment!! >.<

#include <stdio.h>
#define MAXSIDE 100
void inputMatrix(int m[MAXSIDE][MAXSIDE],int number);
void displayMatrix(int m[MAXSIDE][MAXSIDE],int number);
int main()
{
	int m[MAXSIDE][MAXSIDE],number;
	printf("Enter an integer for m:");
	scanf("%d",&number);
	printf("Enter the %d integer:\n",number*number);
	inputMatrix(m,number);
	displayMatrix(m,number);
	return 0;
}
void inputMatrix(int m[MAXSIDE][MAXSIDE],int number)
{
	int r,c;
	for(r=0;r<number;r++)
	{
	for(c=0;c<number;c++)
	{
	scanf("%d",&m[r][c]);
	}
	}

}
void displayMatrix(int m[MAXSIDE][MAXSIDE],int number)
{

	int r,c;
	printf("\n\n\nTable Format:\n");
	for(r=0;r<number;r++)
	{
	for(c=0;c<number;c++)
	{
	printf("%5d",m[r][c]);
	}
	printf("\n");
}
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1