how can we design a magic square in c++?or suggest me the
basic idea of it.

Answer Posted / ganesh kundapur

void check(int *i,int *j,int n)
{
if(*i<0 && *j>=n) {
*i=*i+2;
*j=*j-1;
}
if(*i<=0 && *j<n)
*i=n-1;
if(*i>=0 && *j>=n)
*j=0;
}

/* n is the order of matrix 1, 3, 5... */
void DrawMagicTriangle(int n)
{
int **m;
int i, j, t, k = 0;

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

for(i=0; i<n; i++)
for(j=0; j<n; j++)
m[i][j]=0;
i=0;
j=n/2;
for(t=0; t<n*n; t++) {
m[i][j]=++k;
i--;
j++;

if(i>=0 && j<n && m[i][j]!=0) {
i+=2;
j--;
}
else if(i>=0 && j<n && m[i][j]==0) {
i=i;
j=j;
}
else check(&i,&j,n);
}
for(i=0; i<n; i++) {
for(j=0; j<n; j++)
printf(" %d",m[i][j]);
printf("\n");
}
}

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the advantages of polymorphism?

670


What is oops?what is its use in software engineering?

631


How to improve object oriented design skills?

657


What does I oop mean?

710


What type of loop is a for loop?

777






Which is not an object oriented programming language?

625


What does <> mean pseudocode?

714


Why do we need oop?

782


Which is better struts or spring?

699


What is a class in oop?

678


What is polymorphism used for?

656


What is the difference between a constructor and a destructor?

707


What is polymorphism and types?

680


What is data binding in oops?

698


What is abstraction in oops?

676