My homework ( only C standard library) Minimum TWO functions BUT NOT more than T
ID: 3568174 • Letter: M
Question
My homework( only C standard library)
Minimum TWO functions BUT NOT more than THREE functions to fulfill the requirements of this question. At least ONE function should have return type. Minimum TWO functions with TWO parameters. Either two dimensional arrays or pointers can be used.
Write a C program that prints an n * n magic square(a square arrangement of the numbers 1,2 ...., n2 in which the sums of the rows, colum and diagonals are all the same). The user will specify the value of n: (the maximum size of rows and columns for this question would be 50)
Sample output:
This program creates a magic square of any size.
The size is between 2 and 50.
Enter size of magic square: 5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Continue(Y/N)? N
Exit the program successfully.
Once all conditions are fulfilled, I'll +rep
Explanation / Answer
#include int main() { // Introductory message printf("This program creates a magic sqaure of a specified size. "); printf("The size must be an odd number between 2 and 50. "); // Get the users magic number and allocate to int n int n; printf("Enter size of magic square: "); scanf("%d", &n); // Create the array (not using VLA) int magic[99][99]; int start = (n / 2); // The middle column int max = n * n; // The final number magic[0][start] = 1; // Place the number one in the middle of row 0 // Loop to start placing numbers in the magic square int row; int col; int next_row; int next_col; int i; for (i = 2, row = 0, col = start; i (n - 1)) { // If column will leave the side next_col = 0; // Wrap to first column printf("Column will leave side "); } else { next_col = col + 1; } // Otherwise go over one printf("In col: %d ", col); if (magic[next_row][next_col] > 0) { // If that position is taken if (row > (n - 1)) { // If going to row below leaves bottom next_row = 0; // Go back to the top } else { next_row = row + 1; // Go to the row below next_col = col; // But stay in same column } } row = next_row; col = next_col; printf("About to put %d in row %d, col %d ", i, row, col); magic[row][col] = i; // Put the current value in that position } // Now let's print the array int j; for (i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.