program to find the magic square using array



program to find the magic square using array..

Answer / sneh nagaonkar

/*Program to takes the the data of a 3x3 matrix and check if the given matrix is magic

square or not*/
#include <stdio.h>
#include <conio.h>
int num[3][3];
int sum[8];
int r,c,i;

/*Function to take the value in matrix from user*/
void read_matrix()
{
for(r=0;r<3; r++)
{
for(c=0;c<3; c++)
{
printf("Enter value for %d %d::",r,c);
scanf("%d",&num[r][c]);
}
}
}

/*Function to print matrix and get the sum of rows,cols and diaglons*/
void print_calc_rows_cols()
{
int sum_row,sum_col,ctr=0;

/*Print the Matrix and get the sum of rows*/
for(r=0;r<3; r++)
{
sum_row=0;
for(c=0;c<3; c++)
{
printf("\t%d",num[r][c]);
sum_row=sum_row+num[r][c];
}
sum[ctr]=sum_row;
ctr++;
printf(":: %d",sum_row);
printf("\n");
}
printf("\t::\t::\t::\n");

/*Get the sum of columns*/
for(c=0;c<3;c++)
{
sum_col=0;
for(r=0;r<3;r++)
sum_col=sum_col+num[r][c];

sum[ctr]=sum_col;
ctr++;
printf("\t%d",sum_col);
}
printf("\n");

/*Get the sum of diagnols*/
sum[6]=num[0][0]+num[1][1]+num[2][2];
sum[7]=num[0][2]+num[1][1]+num[2][0];

}

/*Function to Check the sum of rows,cols and diagnols*/
char check_matrix()
{
char c;
for(i=0;i<3; i++)
{
if(sum[i]==sum[i+1])
c='y';
else
{
c='n';
break;
}
}
return c;
}

void main()
{
char c;
clrscr();
read_matrix();
print_calc_rows_cols();
c=check_matrix();


/*Print the result*/
if(c=='y')
printf("\nThis Matrix is a Magic Square");
else
printf("\nThis Matrix is Not a Magic Square");

getch();
}

Is This Answer Correct ?    10 Yes 4 No

Post New Answer

More C++ Code Interview Questions

write a function that allocates memory for a single data type passed as a parameter.the function uses the new operator and return a pointer to the allocated memory.the function must catch and handle any exception during allocation

0 Answers   HCL,


Code for Easily Using Hash Table?

0 Answers  


Write a C/C++ program that connects to a MySQL server and displays the global TIMEZONE.

0 Answers   Facebook, Webyog, Wipro,


how to diplay a external image of output on winxp by using c & c++,

0 Answers  


solve the problem in the programming language C++"if a five digit number is input through the keyboard.Write a program to calculate the sum of its digits(hint: use the modulus operator)

0 Answers  






Code for Method of Handling Factorials of Any Size?

0 Answers  


Write a &#61521;(n) algorithm that sorts n distinct integers, ranging in size between 1 and kn inclusive, where k is a constant positive integer. (Hint: Use a kn-element array.)

0 Answers  


write a program to perform generic sort in arrays?

0 Answers  


A string of charaters were given. Find the highest occurance of a character and display that character. eg.: INPUT: AEGBCNAVNEETGUPTAEDAGPE OUTPUT: E

1 Answers  


How can I Draw an ellipse in 3d space and color it by using graph3d?

0 Answers  


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

0 Answers  


Algorithm in O(2n) Presently we can solve in our hypothetical machine problem instances of size 100 in 1 minute using algorithm A, which is a O(2n). We would like to solve instances of size 200 in 1 minute using algorithm A on a new machine. What is the speed of the new machine should be?

2 Answers   ABC, Qatar University,


Categories