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
What are the advantages of polymorphism?
What is oops?what is its use in software engineering?
How to improve object oriented design skills?
What does I oop mean?
What type of loop is a for loop?
Which is not an object oriented programming language?
What does <> mean pseudocode?
Why do we need oop?
Which is better struts or spring?
What is a class in oop?
What is polymorphism used for?
What is the difference between a constructor and a destructor?
What is polymorphism and types?
What is data binding in oops?
What is abstraction in oops?