(20 pt) Recall the Sudoku puzzle that you studied in Assignment l.In this work o
ID: 3914652 • Letter: #
Question
(20 pt) Recall the Sudoku puzzle that you studied in Assignment l.In this work on a similar number placement puzzle known as Magic Square us uestion you will (MS). from the webl A magic square is an arrangement of distinct Here is the description integer numbrow, and in each column, and the numbers in the main (i.e., each nu mber is used once) from 1 to N2 in an NxN square grid, where the numbers in each and secondary diagonals, all add up to the same number, which is N (N + 1) 2. For example, we can place the numbers from 1 to 9 and place the numbers below figures. -3 on a 3x3 MS with the same sum of 15, from 1 to 25-5 on a 5x5 MS with the same sum of 65, as shown in the 7 95 1+15 4 3 8 15 23 6 19 2 15 4 12 25 8 16 10 18 1 14 22 11 24 7 20 3 17 5 13 219 15 15 15 15 15 Suppose we are interested in checking if a given int MS [N] [N] /*initial values /, is a valid Magic Square or not. Also suppose we have the four helper functions in the next page that respectively check if the sum of the numbers in each row, and in each column, and in the main and secondary diagonals are all equal to the same sum (SS)- N°N +1)/2. Then we can simply check if a given MS (N] [N] is a valid Magic Square or not as follows: / suppose all standard C libraries are included here / #define N / the number N can be large in an actual program void main() int MS [N] [N] 12,7,61 (4,3,81)i int Ss(N(N N+1)/2) if(allRowsOK (MS, SS)&&allColumnsOK; (MS, ss)&& mainDiagoK (MS, Ss)&&secondaryDiagoK; (MS, SS) ) printf ("YES, this is a Magic Square! In") else printf("No, this is NOT a Magic Square! In"):Explanation / Answer
Answer:
int allRowsOK(int MS[][N],int SS) /* allRowsOK() definition starts here */
{
int sum,i,j; /* variable declaration */
for(i=0;i<N;i++) /* loop for rows */
{
sum=0; /* For every row the initial value of sum is 0 */
for(j=0;j<N;j++) /* loop for columns */
{
sum=sum+MS[i][j]; /* calculates the sum of elements in ith row */
}
if(sum==SS) /* If a row's sum is equal to the value in SS */
continue; /* goes for next row */
else /* otherwise (If a row's sum is not equal to the value in SS) */
return 0; /* return 0 */
}
return 1; /* if all the rows have the value in SS, then returns 1 */
}
int allColumnsOK(int MS[][N],int SS) /* allColumnsOK() definition starts here */
{
int sum,i,j; /* variable declaration */
for(i=0;i<N;i++) /* loop for columns */
{
sum=0; /* For every column the initial value of sum is 0 */
for(j=0;j<N;j++) /* loop for rows */
{
sum=sum+MS[j][i]; /* calculates the sum of elements in ith column */
}
if(sum==SS) /* If a column's sum is equal to the value in SS */
continue; /* goes for next column */
else /* otherwise (If a column's sum is not equal to the value in SS) */
return 0; /* return 0 */
}
return 1; /* if all the columns have the value in SS, then returns 1 */
}
int secondaryDiagOK(int MS[][N],int SS) /* secondaryDiagOK() definition starts here */
{
int sum,i; /* variable declaration */
sum=0; /* sum is 0 */
for(i=0;i<N;i++) /* loop for getting all te rows */
sum=sum+MS[i][N-i-1]; /* finding the sum of secondary diagonal elements */
if(sum!=SS) /* if sum is not equal to value is SS */
return 0; /* returns 0 */
else /* if sum is equal to value is SS */
return 1; /* returns 1 */
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.