Commit 9e27c52d authored by Dominik Widhalm's avatar Dominik Widhalm
Browse files

Some beautifications and adaptions to fit coding style for task 2.09

parent 4e7a8178
...@@ -3,27 +3,39 @@ ...@@ -3,27 +3,39 @@
* BASIC EXERCISES - EXAMPLE SOLUTIONS * * BASIC EXERCISES - EXAMPLE SOLUTIONS *
* * * *
* Task_2.09: Magic Square * * Task_2.09: Magic Square *
* Author: Florian Gerstmayer * * Author: Florian Gerstmayer / Dominik Widhalm *
* Email: florian.gerstmayer@technikum-wien.at * * Email: florian.gerstmayer@technikum-wien.at *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-10-24 * * Date: 2017-10-24 *
* * * *
* Notes: *
* *) Quadrat-Indizes: *
* [0][0] [0][1] [0][2] ... *
* [1][0] [1][1] [1][2] ... *
* [2][0] [2][1] [2][2] ... *
* ... ... ... *
* *) Examples for magic squares: *
* https://en.wikipedia.org/wiki/Magic_square *
* *) Examples for 3x3 semi magic squares: *
* http://mathworld.wolfram.com/SemimagicSquare.html *
* *
******************************************************************************/ ******************************************************************************/
/***** INCLUDES ***************************************************************/
/*********DEFINES**************************************************************/ #include <stdio.h>
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
#define SIZE 3
/*********INCLUDES*************************************************************/ /***** MACROS *****************************************************************/
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx// // Size of the square (SIZE x SIZE)
#include <stdio.h> #define SIZE 4
// (Optional) Enable additional debug output
#define DBG 0
/*********MAIN*****************************************************************/ /***** MAIN ROUTINE ***********************************************************/
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcxx//
int main (void) { int main (void) {
/*** Local Variables ***/
int quadrat[SIZE][SIZE] = {0}; int quadrat[SIZE][SIZE] = {0};
int magic = 1; int magic = 1;
int row = 0; int row = 0;
...@@ -32,83 +44,88 @@ int main (void) { ...@@ -32,83 +44,88 @@ int main (void) {
int dia1 = 0; int dia1 = 0;
int dia2 = 0; int dia2 = 0;
/*
Quadrat-Indizes:
[0][0] [0][1] [0][2] ...
[1][0] [1][1] [1][2] ...
[2][0] [2][1] [2][2] ...
... ... ...
*/
/* Read in the numbers of the square */ /* Read in the numbers of the square */
for (int i = 0; i < SIZE; i++) { for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) { for (int j = 0; j < SIZE; j++) {
/* Ask the user the enter the number of the i. row and j. column */
printf("Please enter a number for row %d, col %d: ", i+1, j+1); printf("Please enter a number for row %d, col %d: ", i+1, j+1);
/* Read in the user's input and check for validity */
if (scanf("%d", &quadrat[i][j]) != 1) { if (scanf("%d", &quadrat[i][j]) != 1) {
printf("Illegal character entered, exiting.\n"); /* The user's input was not a valid number */
printf("Illegal character entered!.\n\n");
printf("The program will now be terminated...\n");
/* Return with "FAIL" */
return 1; return 1;
} }
} }
} }
printf("\n\n\n");
/* Print square to user */ /* Print the user entered square */
printf("\nEntered Square:\n");
for (int i = 0; i < SIZE; i++) { for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) { for (int j = 0; j < SIZE; j++) {
printf(" %d ", quadrat[i][j]); printf("%3d ", quadrat[i][j]);
} }
printf("\n"); printf("\n");
} }
/* Get the sum of the first row */ /* First get the reference sum (from the first row) */
sum = 0;
for (int j = 0; j < SIZE; j++) { for (int j = 0; j < SIZE; j++) {
/* Sum up the numbers */
sum += quadrat[0][j]; sum += quadrat[0][j];
} }
/* (Optional) Output the reference sum */
if (DBG) printf("\nThe reference sum is %d\n",sum);
/* Check the sums of all rows, columns and diagonals */ /* Check the sums of all rows, columns and diagonals */
for (int i = 0; i < SIZE; i++) { for (int i = 0; i < SIZE; i++) {
row = 0; row = 0;
col = 0; col = 0;
/* sum up */ /* Sum up ... */
for (int j = 0; j < SIZE; j++) { for (int j = 0; j < SIZE; j++) {
/* all rows */ /* ... all rows: [0][0], [0][1], [0][2], ... */
row += quadrat[i][j]; // [0][0], [0][1], [0][2],... row += quadrat[i][j];
/* ... all columns: [0][0], [1][0], [2][0], ... */
/* all columns */ col += quadrat[j][i]; // Notice the switch of indices!
/* Notice the switch of indices! */ /* ... diagonale 1: [0][0], [1][1], [2][2], ... */
col += quadrat[j][i]; // [0][0], [1][0], [2][0],... if (i == j) {
dia1 += quadrat[j][i]; // Same indices!
/* diagonale 1, top left to bottom right (same indices) */ }
if (i == j) /* ... diagonale 2: [0][2], [1][1], [2][0], ... */
dia1 += quadrat[j][i]; // [0][0], [1][1], [2][2],...
/* diagonale 2, top right to bottom left (inverted indices for 2) */
if (i == (SIZE-j-1)) { if (i == (SIZE-j-1)) {
dia2 += quadrat[j][i]; // [0][2], [1][1], [2][0],... dia2 += quadrat[j][i]; // Inverted indices!
} }
} }
/* If any of the row or column sum differs ... */
/* If any sum (row or col) inequal to reference value */ if ((sum != row) || (sum != col)) {
if (sum != row || sum != col) { /* ... it is not a magic square */
/* no magic square */
magic = 0; magic = 0;
/* No need to continue checking */
break; break;
} }
} }
/* Check if sums of rows and columns were equal */
if (magic == 1) { if (magic == 1) {
/* If also the diagonals match the reference value -> "full" magic */ /* If also the diagonals match the reference value -> "full" magic */
if (dia1 == sum && dia2 == sum) { if ((dia1 == sum) && (dia2 == sum)) {
printf("Magic Square!\n"); /* The square is a full magic square */
printf("\nThe square is a FULL magic square!\n\n");
} else { } else {
/* Otherwise, semi magical */ /* The square is a aemi magic square */
printf("Semi-magic square!\n"); printf("\nThe square is a SEMI magic square!\n\n");
} }
} else { } else {
printf("Nope, no magic square!\n"); /* The square is not a magic square */
printf("\nThe square is NOT a magic square!\n\n");
} }
return 0;
/* Notify the user about the termination of the program */
printf("The program will now be terminated...\n");
return 0; /* Return with Success (0) */
} }
/******************************************************************************/ /******************************************************************************/
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment