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 encapsulation selenium?
What does it mean when someone says I oop?
What are different oops concepts?
What is object in oop?
any one please tell me the purpose of operator overloading
Why it is called runtime polymorphism?
What is an advantage of polymorphism?
What is Difeerence between List obj=new ArrayList(); and ArrayList obj=new ArrayList()?
what type of questions
They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?
i am getting an of the type can not convert int to int *. to overcome this problem what we should do?
What is object-oriented programming? Webopedia definition
Can private class be inherited?
What is the point of polymorphism?