Unstructured programming
Objectives
Preamble
Anyone who wishes to learn scuba diving starts by practicing the basics of this technique in a swimming pool! Similarly, before we dive into “Assembly Programming”, something that scares many students, the following exercises teach you how to write programs in the C language, albeit with a twist: Technically, the required coding is still to be done in the C language, but the “structure” of your codes will be much like assembly programming.
Begin by downloading the starter file for this lab and unzip its content to a folder of your choice. In this lab work, you are asked to write programs in the C language to the tasks given below with the following constraints: No for
, while
, do
, switch
or else
keyword can be used in your codes.
To check whether your implementations are correct, configure then compile the programs with the following commands (under UN*X environment):
$ cd path/to/the/directory/lab3
$ mkdir build # three configuration
$ cd build # steps to do the
$ cmake .. # first time only
$ make # <- compile the programs - run this command every time you modify your ".c" files
If your OS (i.e. the VM) gives you a warning about “cmake” unknown/not installed, you need in this case to install “cmake” before continuing with the lab. Please type:
$ sudo apt install cmake
When prompted for the password, just enter: “user” (without the quotes). The typed password will not show on the screen. Then redo the last two steps mentioned above (i.e. executing cmake ..
then make
).
If the compilation process goes without errors, you can run the validation tests for the exercises using the command
$ ctest --output-on-failure # <- test your programs - run this command after every "successful" make
This command will display on your screen the results of some tests which will allow you to check if you have correctly implemented the assigned tasks.
Hint: Write your solution in a standard way (i.e. with the keywords for
, while
, …), then you can convert it into an unstructured code (without these keywords) using the examples and indications given in the document “ Structured and Unstructured Programming”.
Exercise 1: (Sum of squares)
In mathematics, the sum of the squares of N integers is described with the following equation:
\[sum = \sum_{i=0}^{N-1} n_i^2\]where \(n_0, n_1, ..., n_{N-1}\) are integers.
Write the body of the function SumOfSquares(int count, const int *tab)
in the file src/SumOfSquares.c
.
This function returns the sum of the squares of count
integers stored in the array referenced by the pointer tab
.
Constraints on the code:
- prohibited keywords: for, while, do, switch, else
-
# vvv constraint checking test vvv lab3/src$ python3 test.py --filename=SumOfSquares.c --for --while --switch --else --ternary
Exercise 2 : (StrCmp)
In the standard C language library, the strcmp()
function (see man 3 strcmp
) compares, character by character, two strings in memory to establish which character string comes first in standard lexicographic order, i.e. based on the ASCII values of the characters. Here are some examples :
- “a” < “b”
- “abc” < “abcd”
- “A” < “a”
The character strings to compare are represented by contiguous bytes in memory (each byte is an ASCII character) followed by the NUL character (0x00).
Write the body of the function StrCmp(char *s1, char *s2)
in the file src/StrCmp.c
. This function compares two character strings.
The arguments s1
and s2
hold the addresses in memory of the two strings to compare. The memory address of the beginning of the first string is in s1
, and the memory address of the start of the second string is in s2
. If the first string is greater than the second, return a positive number. If the second string is larger, return a negative number. If the strings are equal, return the value 0.
Hint: code for every possible case, even when NULL strings are supplied!
Constraints on the code:
- prohibited keywords: for, while, do, switch, else
-
# vvv constraint checking test vvv lab3/src$ python3 test.py --filename=StrCmp.c --for --while --switch --else --ternary
Exercise 3: (Calculator)
Write the body of the function calculator(float nbr1, float nbr2, char op)
in the file src/calc.c
.
This function returns the result of the operation nbr1 op nbr2
, where nbr1
and nbr2
are two numbers (of type float
), and op
is a character indicating the type of operation:
’+’: addition ‘-‘: subtraction ‘*’: multiplication ‘/’: division
if op
is different from the four operations indicated above then the function must return the value NAN
(cf. man 3 nan
).
Constraints on the code:
- prohibited keywords: for, while, do, switch, else
-
# vvv constraint checking test vvv lab3/src$ python3 test.py --filename=calc.c --for --while --switch --else --ternary
Exercise 4: (AtoI)
In the C language standard library, the atoi(char *ptr)
function converts the character string pointed to by ptr
into an integer value. For example, executing the function atoi()
on the following character strings:
char str1[] = "1234";
char str2[] = "-1234";
will return the integer values 1234 and -1234, respectively.
Write your version of the AtoI(char *str)
function body in the src/AtoI.c
file. This function returns the integer represented by the character string char* str
.
Unlike the standard atoi()
function, you can limit yourself to character strings of the form [-]14207...
. Note that the square brackets []
indicate the possibility of having an optional character for the minus sign character. They are not part of the character string!
Constraints on the code:
- prohibited keywords: for, while, do, switch, else
-
# vvv constraint checking test vvv lab3/src$ python3 test.py --filename=AtoI.c --for --while --switch --else --ternary