Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code in C. Skeleton Code: https://ideone.com/1DmY5K Task Web Source: http://www.

ID: 3596801 • Letter: C

Question

Code in C.

Skeleton Code: https://ideone.com/1DmY5K

Task Web Source: http://www.comp.nus.edu.sg/~cs1010/labs/2017s1/lab5/2D_arrays.html#section1

Scroll down to 1 Exercise 1: Semi-Magic Square.

Please try to write in some thought processes to allow me to understand arrays better. Arrays are so tough! Thank you! :)

The square contains at least 1 row and at most 9 rows.

All values in the given sqaure are positive integers.

Your program must contain the function scanSquare() to read data into the array and return the size of the square. You are to determine the parameters.

Your program must contain the function isSemiMagic() to check whether the square is a semi-magic square. You are to determine the parameters.

Explanation / Answer

#include <stdio.h>

#define MAXSIZE 9

int scanSquare(int size,int [][MAXSIZE]);

int isSemiMagic(int size, int [][MAXSIZE]);

int main(void) {

//We would need the size of the array, so we would take the user input here itself.

int size;

printf("Enter size of square: ");

scanf("%d", &size);

if (size > MAXSIZE){

//We are checking whether the size input by user is greater than MAXSIZE, if yes, return from here.

return 0;

}

//We have initialized an array of the size , size * size.

int inputArray[size][size];

//Calling the function scanSquare which would take user input and

//initialize corresponding element.

scanSquare(size,inputArray);

//This would check whether the given square fulfills the conditions

//of Semi Magic square and prints the correspinding output.

if (isSemiMagic(size,inputArray) == 1){

printf("It is a semi-magic square. ");

}else{

printf("It is not a semi-magic square. ");

}

return 0;

}

// Read in size of square and values in the square.

// Return the size of square.

int scanSquare(int size,int square[][size]) {

int r, c;

printf("Enter values in the square: ");

for (r=0; r<size; r++) {

for (c=0; c<size; c++) {

scanf("%d", square[r][c]);

}

}

return size;

}

int isSemiMagic(int size,int square[][size]) {

int r, c;

//This variable would keep the track of the sum of the universal

//sum we need for all the columns and rows.

int magicSum = 0;

//This array would keep of track of all the elements from 1 to n^2.

int allElementsCount = size * size;

int allNoArray[allElementsCount];

//We would initialize all elements 0, meaning we have not found any element yet.

for (int i=0; i<allElementsCount; i++) {

allNoArray[i] = 0;

}

for (r=0; r<size; r++) {

int currentRowMagicSum = 0;

int currentColumnMagicSum = 0;

for (c=0; c<size; c++) {

currentRowMagicSum += square[r][c];

currentColumnMagicSum += square[c][r];

//We would check if the current element is in range of the index,

//if yes, we would update the value to 1.

//We are comparing with -1 because we are starting from the index 0.

if ((square[r][c] - 1) < allElementsCount){

allNoArray[square[r][c]] = 1;

}

}

//This is used to initialize the magicSum.

//We would use only sum of first row as the magicSum base.

//All further sum would be compared with that.

if (r == 0){

magicSum = currentRowMagicSum;

}

//We are checking whether the currentRowMagicSum is not equal to magicSum.

//and returning if not.

if (magicSum != currentRowMagicSum){

return 0;

}

//Same is being done with currentColumnMagicSum

if (magicSum != currentColumnMagicSum){

return 0;

}

}

//This would check whether we have all the elements from 1 to n^2

for (int i=0; i<allElementsCount; i++) {

if (allNoArray[i] == 0){

return 0;

}

}

//If all conditions passes, at last we would return 1, meaning the given square is semi magic square.

return 1;

}