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

C++ Program 1. Prompt the user to enter the size of the square such as N. N must

ID: 3744274 • Letter: C

Question

C++ Program

1. Prompt the user to enter the size of the square such as N. N must be greater than or equal to 2 and less than or equal to 10.

2. create an array of size N x N.

3. use the fucntion to popuate the array with distinct random numbers that are between 1 - 100.

4. use a function to determine whether the numbers in N x N are magic square numbers.

5. repeat steps 1 - 4 until the user termiates the program.

Magic Number : n(n2 + 1) / 2

the program must have rows and colums that all equate to the sum of 15 for example

8 1 6 = 15

3 5 7 = 15

4 9 2 = 15

ask the user if he would like to enter another magic square and compute the same way. (Y/N)

this is a sample report of the output

Welcome to my magic sequence program The function of the program is to 1, Allow the user to enter the size of the magic square such as N . N>= 2 and

Explanation / Answer

#include<stdio.h>

int main() {

int size,N;

char ans;

printf("enter size of the magic square");

scanf("%d",&N);

if((N<2)&&(N>10))

{

printf(" Please enter a range between 2 to 10");

}

size = N;

int matrix[N][N]; // = {{4,9,2},{3,5,7},{8,1,6}};

int r, c = 0;

int sum, sum1, sum2;

int flag = 0;

printf(" Enter Random numbers ");

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

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

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

}

printf(" matrix that is created for test is : ");

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

printf(" ");

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

printf(" %d", matrix[r][c]);

}

}

//For diagonal elements

sum = 0;

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

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

if (r == c)

sum = sum + matrix[r][c];

  

}

}

//For Rows sums to be compared

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

sum1 = 0;

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

sum1 = sum1 + matrix[r][c];

}

if (sum == sum1)

{

flag = 1;

printf(" sum of %d th row %d",r+1,sum1);

}

else {

flag = 0;

break;

}

}

//For Columns sums to be compared

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

sum2 = 0;

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

sum2 = sum2 + matrix[c][r];

}

if (sum == sum2)

{

flag = 1;

printf(" sum of %d th column %d",c+1,sum2);

}

else {

flag = 0;

break;

}

}

if (flag == 1)

printf(" Magic square");

else

printf(" Not a Magic square");

  

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote