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- oriented to convert the input dollar(s) into its equivalent peso. Assume that one dollar is equivalent to 51.60

1 Answers  


What output does the following code generate? Why? What output does it generate if you make A::Foo() a pure virtual function? class A { A() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; class B : public A { B() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; int main(int, char**) { A objectA; B objectB; return 0; }

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,


A suduco given & u hv 2 check if it is incomplete(blanks left),or correct or incorrect

0 Answers  


. Write a program using two-dimensional arrays that computes the sum of data in tows and the sum of data in columns of the 3x3 (three by three) array variable n[3][3].

2 Answers   IBM,






Question 1: Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description (for example, “see the dentist”) and a date and time. Write a virtual function occurs_on(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then fill a vector of Appointment* with a mixture of appointments. Have the user enter a date and print out all appointments that happen on that date. *This Should Be Done IN C++

0 Answers  


Write a program using one dimensional array that searches a number and display the number of times it occurs on the list of 12 input values. Sample input/output dialogue: Enter 12 values: 13 15 20 13 30 35 40 16 18 20 18 20 Enter number to search: 20 Occurences: 3

2 Answers  


Find the maximum product of three numbers in an array? Eg. 9,5,1,2,3 Max product= 9*5*3= 135 The array can hav negative numbers also..

7 Answers   CTS,


write a program that accepts a number and outputs its equivalent in words. take note that the maximum input is 3000

1 Answers   Alvin,


i really need help about this.. write a program to display the set of odd and even numbers separately. find the highest and lowest value of the given numbers.

0 Answers  


what mean void creat_object?in public class in this code class A{ public: int x; A(){ cout << endl<< "Constructor A";} ~A(){ cout << endl<< "Destructor A, x is\t"<< x;} }; void create_object(); void main() { A a; a.x=10; { A c; c.x=20; } create_object(); } void create_object() { A b; b.x=30; }

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  


Categories