Write c++ program that prompts the user to enter numaric values for a 4X 4 array.
the program will display the fllowing menu.
1-Is magic?
2-Sorter
3-diagonal summation
4-Exit
-Is magic wil determine if arry is magic or not.
-Sorter will sort the array values in ascending order
-Diagonal summation will determine the some of two diagonal in the array.
-Exit: to exit from program.
and done my best to do this:
# include<stdio.h>
#include<iostream>
#include<conio.h>
using namespace std;
#define NR_ROW 4
#define NR_COL 4
int main()
{
int ary_elmnt, i, j, choose, colsum[4],rowsum[4];
int diagsum[2]={0};
float mat[NR_ROW][NR_COL];
for (i = 0; i < NR_ROW; i++)
{
for (j = 0; j < NR_COL; j++)
{
cout<<"\n Please Enter The Array Elements (16 Numbers): ", i, j;
cin>> mat[i][j];
}
}
case 4 : exit(1);
default: cout << "\n SORRY,This Number ( " << choose << " ) Does Not Exist On The Menu, Please Try Again.\n\n";
}
}while (1);
system ("pause");
return 0;
}
int isMagic( int n, int mat[NR_ROW][NR_COL]) {
int colsum[NR_ROW];
int rowsum[NR_COL];
int diagsum[2]={0};
// calculate row, column, and diagonal sums
for (int i=0; i<NR_ROW; ++i) {
colsum[i]=0;
rowsum[i]=0;
for (int j=0; j<NR_COL; ++j) {
rowsum[i]+=mat[i][j];
colsum[i]+=mat[i][j];
}
diagsum[0]+=mat[i][i];
diagsum[1]+=mat[NR_COL-i-1][i];
}
int testSum=(1+NR_COL*NR_COL/2)*NR_COL;
// check to see if all sums are equal to testSum
int zeroIfNotMagic=1;
for (int i=0; i<n; ++i) {
// test row sum
if ( rowsum[i]!=testSum ) {
zeroIfNotMagic=0;
break; }
// test column sum
if ( rowsum[i]!=testSum ) {
zeroIfNotMagic=0;
break;
}
}
// test diagonal sums
for (int i=0; i<2; ++i) {
if ( diagsum[i]!=testSum ) {
zeroIfNotMagic=0;
break;
}
}
// return 0 if the square is not "magical", 1 if it is.
return zeroIfNotMagic;
}