Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

Answers were Sorted based on User's Feedback



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

Answer / ganesh kundapur

Detail about the magic square
http://en.wikipedia.org/wiki/Magic_square.

To design a matrix sqaure, consider a 3X3 matrix.

n = 3;

* Fill the matrix with 0 or X. Start from the middle of the
first row ( i = 0, j = n/2)
+--+--+--+
|X_|1_|X_|
|X_|X_|X_|
|X_|X_|X_|

*. From 1, Go up rt corner(no row, and last column)(-1, 2),
and come down. and fill the next number at (2, 2)

X 1 X
X X X
X X 2

*. From 2, go right up corner( No column, 2nd row)(1, -1)
and if not any column, take a left and fill the next number
at (1, 0)

X 1 X
3 X X
X X 2

* From 3, go right up(0,1), It's already accupied, so come
down(2, 0) and fill the next number at (2,0)
X 1 X
3 X X
4 X 2.

* From node 4(2,0), up right up (1, 1), Its not accupied,
fill the next number at (1, 1)

X 1 X
3 5 X
4 X 2.

* From 5(1,1), Go right up(0,2) and its not accupied, fill
the next number at (0, 2)
X 1 6
3 5 X
4 X 2

* From x(0,2), go right up(No row, no column), so come down
from 6(1, 2), Its not accupied fill with the next number 7
at (1, 2)

X 1 6
3 5 7
4 X 2

* From 7(1, 2), Go up rt(1st row, no column), so take a left
and fill the unfilled node (0, 0)

8 1 6
3 5 7
4 X 2

* From 8(0,0), take up rt( No row, 1st column), come down
and fill the unfilled node at ( 2, 1)

8 1 6 sum = 15
3 5 7 sum = 15
4 9 2 sum = 15

sum 15 15 15

Diagonal sum = 15

Fallow the same procedure for 5X5, 7X7 square.

Is This Answer Correct ?    8 Yes 3 No

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

Answer / 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

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

Answer / anu

I don't understand above answer.... can anyone explain in
detail...?? plz.....

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More OOPS Interview Questions

What is the difference between Home and $Home?

2 Answers   TCS,


What is an advantage of polymorphism?

0 Answers  


difference between abstraction and encapsulation with progarammatic eg. hi,just recently i went for an interview .The interviewer asked what is the difference between abstraction and encapsulation with programmatic eg. I gave the answer as encapsulation mean hiding the relevant data which is not useful for the user, eg a electric fan .hiding the information how the electricity is converted into machanical energy. abtraction showing only the relevant data to the user eg electric fan. it look ,its color ,it design etc only relevant data. Then the interviewer asked me, give me some programmic eg .I Said Let assume a web form having control like textbox,button etc. The user can view textbox,button etc this is the eg of abstraction and when the user click on the button how he is redirected is not known by the user is the eg of the encapsulation. Am I Correct .was the answer given by me is perfect .now i am planing to go for an another interview should i give the same answer.IF not please suggest me a better answer.with some good eg Please help

1 Answers  


what is difference b/w object based and object oriented programming language?

18 Answers   Chaitanya, College School Exams Tests, Educomp, IBM, Infosys, Telko,


What is Difeerence between List obj=new ArrayList(); and ArrayList obj=new ArrayList()?

0 Answers   NIIT, SRA,


Why do we need polymorphism in c#?

0 Answers  


What is conditional Compilation?

3 Answers   emc2, HCL,


How can you overcome the diamond problem in inheritance?

0 Answers   NIIT,


How oops is better than procedural?

0 Answers  


What is oops in simple words?

0 Answers  


me get an assignent n its question is this 1.creat a set as in math i.ea={1,2} 2.insert element in it3. delete element don,t repeat any element 4.union 5. intersection of two sets plz help me i always pray for u n send me at ayeshawzd@hotmail.com f u have c++ how to program 5th addition then it is the 10.9 question in 10th chapter exercise

1 Answers  


which feature are not hold visual basic of oop?

0 Answers   Ignou,


Categories