Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
master-embedded-systems
c-exercise-solutions public
Commits
9e27c52d
Commit
9e27c52d
authored
Oct 25, 2017
by
Dominik Widhalm
Browse files
Some beautifications and adaptions to fit coding style for task 2.09
parent
4e7a8178
Changes
1
Hide whitespace changes
Inline
Side-by-side
ch_2/task_09/main.c
View file @
9e27c52d
...
...
@@ -3,27 +3,39 @@
* BASIC EXERCISES - EXAMPLE SOLUTIONS *
* *
* Task_2.09: Magic Square *
* Author: Florian Gerstmayer
*
* Author: Florian Gerstmayer
/ Dominik Widhalm
*
* Email: florian.gerstmayer@technikum-wien.at *
* Email: dominik.widhalm@technikum-wien.at *
* 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 *
* *
******************************************************************************/
/*********DEFINES**************************************************************/
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
#define SIZE 3
/***** INCLUDES ***************************************************************/
#include <stdio.h>
/*********INCLUDES*************************************************************/
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
#include <stdio.h>
/***** MACROS *****************************************************************/
// Size of the square (SIZE x SIZE)
#define SIZE 4
// (Optional) Enable additional debug output
#define DBG 0
/*********MAIN*****************************************************************/
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcxx//
/***** MAIN ROUTINE ***********************************************************/
int
main
(
void
)
{
/*** Local Variables ***/
int
quadrat
[
SIZE
][
SIZE
]
=
{
0
};
int
magic
=
1
;
int
row
=
0
;
...
...
@@ -32,83 +44,88 @@ int main (void) {
int
dia1
=
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 */
for
(
int
i
=
0
;
i
<
SIZE
;
i
++
)
{
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
);
/* Read in the user's input and check for validity */
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
;
}
}
}
printf
(
"
\n\n\n
"
);
/* Print square to user */
/* Print the user entered square */
printf
(
"
\n
Entered Square:
\n
"
);
for
(
int
i
=
0
;
i
<
SIZE
;
i
++
)
{
for
(
int
j
=
0
;
j
<
SIZE
;
j
++
)
{
printf
(
"
%d "
,
quadrat
[
i
][
j
]);
printf
(
"%
3
d "
,
quadrat
[
i
][
j
]);
}
printf
(
"
\n
"
);
}
/* Get the sum of the first row */
sum
=
0
;
/* First get the reference sum (from the first row) */
for
(
int
j
=
0
;
j
<
SIZE
;
j
++
)
{
/* Sum up the numbers */
sum
+=
quadrat
[
0
][
j
];
}
/* (Optional) Output the reference sum */
if
(
DBG
)
printf
(
"
\n
The reference sum is %d
\n
"
,
sum
);
/* Check the sums of all rows, columns and diagonals */
for
(
int
i
=
0
;
i
<
SIZE
;
i
++
)
{
row
=
0
;
col
=
0
;
/*
s
um up */
/*
S
um up
...
*/
for
(
int
j
=
0
;
j
<
SIZE
;
j
++
)
{
/* all rows */
row
+=
quadrat
[
i
][
j
];
// [0][0], [0][1], [0][2],...
/* all columns */
/* Notice the switch of indices! */
col
+=
quadrat
[
j
][
i
];
// [0][0], [1][0], [2][0],...
/* diagonale 1, top left to bottom right (same indices) */
if
(
i
==
j
)
dia1
+=
quadrat
[
j
][
i
];
// [0][0], [1][1], [2][2],...
/* diagonale 2, top right to bottom left (inverted indices for 2) */
/* ... all rows: [0][0], [0][1], [0][2], ... */
row
+=
quadrat
[
i
][
j
];
/* ... all columns: [0][0], [1][0], [2][0], ... */
col
+=
quadrat
[
j
][
i
];
// Notice the switch of indices!
/* ... diagonale 1: [0][0], [1][1], [2][2], ... */
if
(
i
==
j
)
{
dia1
+=
quadrat
[
j
][
i
];
// Same indices!
}
/* ... diagonale 2: [0][2], [1][1], [2][0], ... */
if
(
i
==
(
SIZE
-
j
-
1
))
{
dia2
+=
quadrat
[
j
][
i
];
//
[0][2], [1][1], [2][0],...
dia2
+=
quadrat
[
j
][
i
];
//
Inverted indices!
}
}
/* If any sum (row or col) inequal to reference value */
if
(
sum
!=
row
||
sum
!=
col
)
{
/* no magic square */
/* If any of the row or column sum differs ... */
if
((
sum
!=
row
)
||
(
sum
!=
col
))
{
/* ... it is not a magic square */
magic
=
0
;
/* No need to continue checking */
break
;
}
}
/* Check if sums of rows and columns were equal */
if
(
magic
==
1
)
{
/* If also the diagonals match the reference value -> "full" magic */
if
(
dia1
==
sum
&&
dia2
==
sum
)
{
printf
(
"Magic Square!
\n
"
);
if
((
dia1
==
sum
)
&&
(
dia2
==
sum
))
{
/* The square is a full magic square */
printf
(
"
\n
The square is a FULL magic square!
\n\n
"
);
}
else
{
/* Otherwise, s
emi magic
al
*/
printf
(
"
Semi-
magic square!
\n
"
);
/* The square is a a
emi magic
square
*/
printf
(
"
\n
The square is a SEMI
magic square!
\n
\n
"
);
}
}
else
{
printf
(
"Nope, no magic square!
\n
"
);
/* The square is not a magic square */
printf
(
"
\n
The 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) */
}
/******************************************************************************/
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment