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
b773f28e
Commit
b773f28e
authored
Oct 24, 2017
by
Florian Gerstmayer
Browse files
Update Task 2.9, more compact solution
parent
6ee03480
Changes
1
Hide whitespace changes
Inline
Side-by-side
ch_2/task_09/main.c
View file @
b773f28e
...
...
@@ -3,121 +3,109 @@
* BASIC EXERCISES - EXAMPLE SOLUTIONS *
* *
* Task_2.09: Magic Square *
* Author:
Dominik Widhalm
*
* Email:
dominik.widhalm
@technikum-wien.at
*
* Date: 2017-
05-08
*
* Author:
Florian Gerstmayer
*
* Email:
florian.gerstmayer
@technikum-wien.at *
* Date: 2017-
10-24
*
* *
******************************************************************************/
/***** INCLUDES ***************************************************************/
#include <stdio.h>
/***** MACROS *****************************************************************/
/***** TYPEDEFS ***************************************************************/
/***** STRUCTURES *************************************************************/
/***** GLOBAL VARIABLES *******************************************************/
/*********DEFINES**************************************************************/
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
#define SIZE 3
/***** FUNCTION PROTOTYPES ****************************************************/
/***** LOCAL FUNCTIONS ********************************************************/
/*********INCLUDES*************************************************************/
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
#include <stdio.h>
/***** MAIN ROUTINE ***********************************************************/
/*********MAIN*****************************************************************/
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcxx//
int
main
(
void
)
{
/*** Local Variables ***/
int
magic
=
1
;
int
sum
=
0
;
int
temp
=
0
;
int
values
[
5
][
5
];
int
quadrat
[
SIZE
][
SIZE
]
=
{
0
};
int
magic
=
1
;
int
row
=
0
;
int
col
=
0
;
int
sum
=
0
;
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
<
5
;
i
++
)
{
/* Ask the user the enter the numbers of the i. row */
printf
(
"Please enter the numbers of the %d. row:
\n
"
,(
i
+
1
));
for
(
int
j
=
0
;
j
<
5
;
j
++
)
{
/* ask the user to enter the numbers */
printf
(
"Enter the %d. number: "
,(
j
+
1
));
/* Read in the user's input */
scanf
(
"%d"
,
&
values
[
i
][
j
]);
for
(
int
i
=
0
;
i
<
SIZE
;
i
++
)
{
for
(
int
j
=
0
;
j
<
SIZE
;
j
++
)
{
printf
(
"Please enter a number for row %d, col %d: "
,
i
+
1
,
j
+
1
);
scanf
(
"%d"
,
&
quadrat
[
i
][
j
]);
}
}
printf
(
"
\n\n\n
"
);
/* Get the sum of the first row */
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
sum
+=
values
[
0
][
i
];
}
/* Check the sums of all rows */
for
(
int
i
=
1
;
i
<
5
;
i
++
)
{
/* Calculate the sum of the i. row (1. already calculated) */
temp
=
0
;
for
(
int
j
=
0
;
j
<
5
;
j
++
)
{
temp
+=
values
[
i
][
j
];
}
/* Compare the sums */
if
(
sum
!=
temp
)
{
/* No magical square */
magic
=
0
;
/* Print square to user */
for
(
int
i
=
0
;
i
<
SIZE
;
i
++
)
{
for
(
int
j
=
0
;
j
<
SIZE
;
j
++
)
{
printf
(
" %d "
,
quadrat
[
i
][
j
]);
}
printf
(
"
\n
"
);
}
/* Check the sums of all columns */
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
/* Calculate the sum of the i. row */
temp
=
0
;
for
(
int
j
=
0
;
j
<
5
;
j
++
)
{
temp
+=
values
[
j
][
i
];
/* Get the sum of the first row */
sum
=
0
;
for
(
int
j
=
0
;
j
<
SIZE
;
j
++
)
{
sum
+=
quadrat
[
0
][
j
];
}
/* Check the sums of all rows, columns and diagonals */
for
(
int
i
=
0
;
i
<
SIZE
;
i
++
)
{
row
=
0
;
col
=
0
;
/* sum 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) */
if
(
i
==
(
SIZE
-
j
-
1
))
{
dia2
+=
quadrat
[
j
][
i
];
// [0][2], [1][1], [2][0],...
}
}
/* Compare the sums */
if
(
sum
!=
temp
)
{
/* No magical square */
/* If any sum (row or col) inequal to reference value */
if
(
sum
!=
row
||
sum
!=
col
)
{
/* no magic square */
magic
=
0
;
break
;
}
}
/* Check the sums of the first diagonal */
temp
=
0
;
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
temp
+=
values
[
i
][
i
];
}
/* Compare the sums */
if
(
sum
!=
temp
)
{
/* No magical square */
magic
=
0
;
}
/* Check the sums of the second diagonal */
temp
=
0
;
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
temp
+=
values
[
i
][
4
-
i
];
}
/* Compare the sums */
if
(
sum
!=
temp
)
{
/* No magical square */
magic
=
0
;
}
/* Check if square is actually a magic square */
if
(
magic
==
1
)
{
/* The square is a magic square */
printf
(
"
\n
The square is a magic square!
\n\n
"
);
/* If also the diagonals match the reference value -> "full" magic */
if
(
dia1
==
sum
&&
dia2
==
sum
)
{
printf
(
"Magic Square!
\n
"
);
}
else
{
/* Otherwise, semi magical */
printf
(
"Semi-magic square!
\n
"
);
}
}
else
{
/* The square is not a magic square */
printf
(
"
\n
The square is NOT a magic square!
\n\n
"
);
printf
(
"Nope, no magic square!
\n
"
);
}
/* Notify the user about the termination of the program */
printf
(
"The program will now be terminated...
\n
"
);
return
0
;
/* Return with Success (0) */
return
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