Solutions to test 2
The sine function
Here is the source code of the 3 functions of mysin.
https://afnog.iotworkshop.africa/pub/Embedded_Systems/SolutionsToTest2/mysin.tar.gz
The mySin function calculated the sin function but this time takes the angles in degrees instead of taking it it radians as the sin function in the math library does. It converts degrees into radians and calls the math library sin function whose result it returns to the caller (callMySin)
The main routine simply calls mySin with an angle of 30°
This is the result when running callMySin:
My way of compiling did not work because I did not link the math library into the program (-lm is missing)
The Makefile is part of the mysin.tar.gz package
Command Line Parameters
- argc will be 5
- argv[0] will be ./myProg, the name of the command
- argv[5] is undefined. We have 5 parameters but we start counting from zero!
- (argv[3])[1] is the second character of the string "ike" and therefore 'k'. Remember that we start counting from zero!
If we need integer parameters we will have to convert the strings "65" and "35" to integers. The functions
atoi or (better)
strtol will do this.
To check it the strings are "uli" and "ike", use the string compare function strcmp:
if (strcmp("uli",argv[1]==0) then we found the right string. Alternatively we can say: if (strcmp("uli",argv[1]) as the integer 0 is interpreted as the boolean "false".
Makefiles
CC, CFLAGS, RM, $@ and $^ are all macros
hello and clean are targets
hello.c is a dependency
$(RM) translates into
rm -f which removes the listed files but does not print a warning message if one of the listed files does not exists.
$@ is the target and expands into hello
@^ is the dependency and expands into hello.c
--
Uli Raich - 2017-10-24
Comments