program to find the magic square using array

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

develop a program to calculate and print body mass index for 200 employees

2309


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

2230


Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)

3395


Write a program that print in screen a tree with its height taken from user by entering number of 4 digits and find the odd numbers then calculate the sum of odd numbers so he get the height of tree?

3001


Create a program to read two random data set in two files named data1.txt and data2.txt manifold contains integer numbers, whereas data2.txt file contains the float type numbers. Simpanlahmasing each into 2 pieces of data that is an array of type integer array and an array of type float, then calculate the average numbers in the second array.

2249






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

3051


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

4649


write a program to calculate the amount of investment after a period n years if the principal investors was p and interest is calculated using compound interest,formular=a=p(1+r)^n

2474


Definition of priority queue was given. We have to implement the priority queue using array of pointers with the priorities given in the range 1..n. The array could be accessed using the variable top. The list corresponding to the array elements contains the items having the priority as the array index. Adding an item would require changing the value of top if it has higher priority than top. Extracting an item would require deleting the first element from the corresponding queue. The following class was given: class PriorityQueue { int *Data[100]; int top; public: void put(int item, int priority); // inserts the item with the given priority. int get(int priority); // extract the element with the given priority. int count(); // returns the total elements in the priority queue. int isEmpty(); // check whether the priority queue is empty or not. }; We had to implement all these class functions.

4486


How to swap two ASCII numbers?

2553


how to write a program that opens a file and display in reverse order?

2667


write a program to convert temperature from fa height into celcius and vise versa,use modular programming

2549


1+1/2!+1/3!+...+1/n!

2051


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++

663


Given a table of the form: Product Sold on A 1/1/1980 B 1/1/1980 C 1/1/1980 A 1/1/1980 B 1/1/1980 C 2/1/1980 A 2/1/1980 There are 30 products and 10,000 records of such type. Also the month period during which sales happened is given to u. Write the program to display the result as: Product Month No. of copies A January 12 A February 15 A March 27 B January 54 B February 15 B March 10 C January 37

2327