Computer programming - Lab 1
Compiling your program
Compiling and running are two distinct steps.
The compiler converts the source file (.c) to an executable file.
Command: gcc options file.c
You should use these options (in any order)
To run your program, use the command
- -Wall produce all types of warning messages. Your code should compile with no warnings (certainly without any warnings that you are absolutely sure don't matter).
- -O optimize. In the process, will warn about uninitialized variables. If your reason is to optimize, look up different variants.
- -std=c11 Use the latest standard (2011). Useful e.g. for local declarations in for. For older compilers, use -std=c99 .
- -lm Compile with the math library. Needed when you use functions from math.h
- -o execfile Use if you don't want the default name a.out for the generated executable.
where . (dot) means the current directory (where you compiled),
a.out is the executable file name (replace as needed) and /
is the directory separator character under Linux.
When solving the proposed problems:
write a function that does the stated task, returning the appropriate result
in main, call the function and print the result. Test your function with several (combinations of) relevant values.
1. Leap year Write a function that determines if a year is a leap year (returns 1 if true, 0 otherwise). The rules are:
Use conditional expressions. Do not use boolean AND/OR. When you encode the rules, you'll need to take decisions in a different order than stated above.
2. Rounding Write a function myround that takes a real number (double) and returns (also as double) the value rounded to the nearest integer, away from zero. You may use the standard functions floor and/or ceil (look up the man page in the terminal using the command man functionname).
- A year divisible by 4 is a leap year.
- As an exception to 1), years divisible by 100 are not leap years
- As an exception to 2), years divisible by 400 are leap years.
Write a function that tests your implementation by comparing the result with that given by the standard function round. The function should take an argument x and print Test for x passed/failed.
3. Median of three Write a function that returns the median of three numbers (i.e., the
middle one, when ordering the numbers by value).
Use conditional expressions. Avoid long and complex code.
Test your code for all possible orderings of the arguments (how many?)
Option 1: if you first compare two numbers, you are left with a similar
problem but with extra information: the ordering between two of the numbers.
Write a function to solve this subproblem, and call it in the function
that you were asked to write.
Option 2: can you write the function just by composing max and min of pairs of values? Hint: the values should appear symmetrically.
Which version makes the smallest number of comparisons in the worst case?
4. Distinct values Write a function that takes three integers, returns the number of distinct values among these (1, 2 or 3) and prints, depending on the case, "all arguments are equal / distinct", or "arguments 1 and 2 / 2 and 3 / 1 and 3 are equal".
Use as little duplicate code as possible, writing helper functions where useful.
5. Bonus value A local public transit ride in NYC is $2.75. You can load up your MetroCard with any multiple of 5 cents. If you load up with at least the value of two trips, you get a 5% bonus, rounded to a cent. Write a function that computes and returns the amount you need to load for N trips, and also prints the amount that will be left on the card. You can check your results here .
Last modified: Wed Sep 27 13:10:00 EET 2017