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 is polymorphism in oops with example?
What is polymorphism explain its types?
How long to learn object oriented programming?
What is polymorphism give a real life example?
explain sub-type and sub class? atleast u have differ it into 4 points?
What is object in oop?
There are two base class B1,B2 and there is one class D which is derived from both classes, Explain the flow of calling constructors and destructors when an object of derived class is instantiated.
What is encapsulation with real life example?
Can private class be inherited?
i got a backdoor offer in process global,Bangalore..Can i work with it?
what are the realtime excercises in C++?
What is polymorphism what is it for and how is it used?
What are the 5 oop principles?
#include
Where You Can Use Interface in your Project