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 is polymorphism in oops with example?

525


What is polymorphism explain its types?

675


How long to learn object oriented programming?

561


What is polymorphism give a real life example?

557


explain sub-type and sub class? atleast u have differ it into 4 points?

1830






What is object in oop?

679


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.

1454


What is encapsulation with real life example?

568


Can private class be inherited?

615


i got a backdoor offer in process global,Bangalore..Can i work with it?

2324


what are the realtime excercises in C++?

2333


What is polymorphism what is it for and how is it used?

572


What are the 5 oop principles?

598


#include #include #include #include void insert(struct btreenode **, int); void inorder(struct btreenode *); struct btreenode { struct btreenode *leftchild; struct btreenode *rightchild; int data; }; main() { struct btreenode *bt; bt=(struct btreenode *)NULL; int req,i=1,num; clrscr(); printf("Enter number of nodes"); scanf("%d",&req); while(i<=req) { printf("Enter element"); scanf("%d",&num); insert(&bt,num); i++; } inorder(bt); } void insert(struct btreenode **sr, int num) { if(*sr==NULL) { *sr=(struct btreenode *)malloc (sizeof(struct btreenode)); (*sr)->leftchild=(struct btreenode *)NULL; (*sr)->rightchild=(struct btreenode *)NULL; (*sr)->data=num; return; } else { if(num < (*sr)->data) insert(&(*sr)->leftchild,num); else insert(&(*sr)->rightchild,num); } return; } void inorder(struct btreenode *sr) { if(sr!=(struct btreenode *)NULL) { inorder(sr->leftchild); printf("\n %d",sr->data); inorder(sr->rightchild); } else return; } please Modify the given program and add two methods for post order and pre order traversals.

3246


Where You Can Use Interface in your Project

1423