C Programming Lab: Submitting Assignments, and Requirements For Passing This Lab
C Programming Lab: Submitting Assignments, and Requirements For Passing This Lab
September 3, 2015
http://logicrunch.it.uu.se:4096/~wv/c-lab/
The web service automatically checks whether uploaded code compiles cor-
rectly, and whether it passes a number of testcases.1 If your solution is
accepted you can continue with the next exercise, otherwise you will have
to revise your solution and upload a fixed version.
Whenever you submit a solution, make sure that you have spec-
ified the name and personal number of all authors on the website.
In order to pass the lab, you have to submit correct solutions for at least
4 of the exercises. Submission opens on Thursday September 3rd, 8:00
and closes at Tuesday September 8th, 12:00. The student or group
submitting the highest number of correct solutions earns eternal glory (and
a surprise present).
1
Note: all submitted solutions are stored, and might manually be checked for correct-
ness or signs of cheating at a later point.
1
The Lab Computers
You’ll be working with the assignments in the Unix labs. If you are new to
Unix or working from a terminal, the following table can be used as a quick
reference. There are also many great resources online if you want to learn
more.
2
i n t main ( i n t a r g c , char ∗∗ a r g v ) {
p r i n t f ( ” This t e x t i s p r i n t e d t o t h e s c r e e n \n” ) ;
return 0 ;
}
The flag -Wall tells gcc to warn us about possible errors or design flaws that
it can discover. It is a good habit to use this flag. You may also consider
the -std=c99 flag, which enables some newer additions to the C language,
e.g., declaration of variables in the for loop header. Finally we list the files
to see that some output has been produced and then we run the executable
file.
~/example]$ ls
example.c executable
~/example]$ ./executable
This text is printed to the screen
~/example]$
#include ” p r i n t f u n . h”
i n t main ( i n t a r g c , char ∗∗ a r g v ) {
printfun ( ) ;
return 0 ;
}
3
// p r i n t f u n . c
/∗ ∗
∗ Function i m p l e m e n t a t i o n
∗/
void p r i n t f u n ( ) {
p r i n t f ( ” This t e x t i s p r i n t e d t o t h e s c r e e n \n” ) ;
}
/∗ ∗
∗ Function p r o t o t y p e
∗/
void p r i n t f u n ( ) ;
Compilation now requires three calls to gcc: two to compile the C files to
object files (with option -c), and one for the final linking to an executable:
a l l : example2
example2 . o : example2 . c p r i n t f u n . h
g c c −Wall example2 . c −c −o example2 . o
printfun . o : printfun . c
g c c −Wall p r i n t f u n . c −c −o p r i n t f u n . o
example2 : example2 . o p r i n t f u n . o
g c c −Wall example2 . o p r i n t f u n . o −o example2
4
~/example]$ make
gcc -Wall example2.c -c -o example2.o
gcc -Wall printfun.c -c -o printfun.o
gcc -Wall example2.o printfun.o -o example2
~/example]$ ./example2
This text is printed to the screen
~/example]$
5
Exercises
Now that we know how to create and run a program, we move on to the
exercises. The solutions to (most of) the exercises will be provided after
the end of the lab. Please refer to the slides for more information on C
programming.
Exercise 1 Output
In the introductory program we include the library stdio.h, which con-
tains the function printf. This function is used to produce output in the
form of characters that are printed in the terminal. In its simplest form, the
function is called with a string as argument:
printf("This text is printed to the screen\n");
That is great, but we want our programs to output more than just the fixed
strings that the programmer writes in the program. To print the contents
of variables, we add format specifiers to the string and add the variables we
want to print as arguments:
int number;
char letter;
printf("%d is an integer and %c is a character\n", number, letter);
Different types of variables have different specifiers, all starting with a per-
centage sign. Common specifiers are %d for integers, %f for floats, %c for
characters and %s for strings. To output a percentage sign, we use %%.
Write a function that outputs:
a) The string: One half is 50%
b) two integers and their difference.
c) two floats and the result of dividing one with the other
Write a main function that calls your other functions. The output has to be
as follows:
[.../exercise]$ ./e1
One half is 50%
The difference between 10 and 3 is 7
1.000000 / 3.000000 is 0.333333
[.../exercise]$
Exercise 2 Input
For input we use the function scanf, also from the library stdio.h. The
scanf function takes a format string followed by references to where the
input should be stored. Example that reads an integer to a variable:
6
int number;
scanf("%d", &number);
Notice that the & character in front of the varable name. It means that
the variable is passed as reference to scanf. It allows scanf to update the
value of the variable. If & is not there, the program would likely crash at
that point. When reading a string, the & sign can be omitted:
char my_variable[100];
scanf("%s", my_variable);
Write functions that:
a) asks for two integers and outputs them and their sum.
Exercise 3 Conditionals
If-else statements are used to make a program behave differently depending
on the program state or user input. As an example, one can use if-statements
to make sure that input is sane before performing an operation
int a;
int b;
...
if(b == 0){
printf("Error: Divide by zero!\n");
// Code for error handling.
...
}
else{
printf("Division evaluates to: %d\n", a/b);
}
7
Write functions that:
a) ask for an integer and output whether the entered number is zero or
not.
b) ask for two floats and outputs the largest of the inputs
d) ask for three integers and output whether any of them are equal. Use
only one if-else-statement
Write a main function that calls your other functions. The output has to be
as follows:
[.../exercise]$ ./e3
Give an integer: 12
The number you entered does not equal zero
Give two floats: 13.4 20
20.000000 is the largest
Give an integer: 14
Result is: 7
Give three integers: 1 13 1
Some numbers are equal
[.../exercise]$ ./e3
Give an integer: 0
The number you entered equals zero
Give two floats: 13.2 -150
13.200000 is the largest
Give an integer: 7
Result is: 21
Give three integers: 2 5 13
All are unique
[.../exercise]$
Exercise 4 Loops
Loops are used to execute a statement or a block of code multiple times. A
loop will continue to execute as long as the loop condition is satisfied. These
two example loops will print the numbers 1 to 10 on one line and then 11
to 20 on the next line:
int i,j;
i = 1;
while(i < 11){
8
printf("%d ", i);
i=i+1;
}
printf("\n");
for(j=11;j<=20;j++){
printf("%d ", j);
}
printf("\n");
b) print all the numbers between 1 and 100, with 10 numbers on each
line. Use two for loops. All columns should be aligned.
c) ask for a number than prints the number squared. This repeats until
the 0 is entered.
Write a main function that calls your other functions. The output has to be
as follows:
[.../exercise]$ ./e4
Even numbers between 0 and 40:
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
Numbers 1 to 100:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
Give a number: 2
The square of 2 is 4
Give a number: 5
The square of 5 is 25
Give a number: 9
The square of 9 is 81
Give a number: 0
You entered zero.
[.../exercise]$
9
Exercise 5 Loops II
a) Write a program that asks for a number. Then the program should
print 1 through the given number on separate lines.
[.../exercise]$ ./e5
Give a number: 5
1
2
3
4
5
Run again (y/n)? y
Give a number: 2
1
2
Run again (y/n)? n
Exiting...
[.../exercise]$
Exercise 6 Functions
Functions are a great way to make code reusable, improve the structure of
the code and isolate errors. Write functions that:
b) take four floats as argument and returns the minimum. Make use of
the function defined in a).
Write a main function that asks the user for four floats and then outputs the
minimum, maximum, their sum and mean value. Use the functions from a)
- d) to implement this. The output has to be as follows:
10
[.../exercise]$ ./e6
Give four floats: 10.0 -2.3 13.2 20.4
min: -2.300000
max: 20.400000
sum: 41.299999
mean: 10.325000
[.../exercise]$
Exercise 7 Functions II
b) Write a program that asks the user for numbers a and b, and then use
these numbers as arguments for your functions and print the result on
the screen.
[.../exercise]$ ./e7
Give a: 11
Give b: 5
11 + 5 = 16
11 - 5 = 6
11 * 5 = 55
11 / 5 = 2
[.../exercise]$
Exercise 8 Arrays
In this exercise we look at some basic operations on arrays. Write a C
function that:
b) prints an array of integers. The integers are printed on one line, en-
closed in curly brackets and separated by commas.
All functions take two parameters, a pointer to the array of integers and the
number of elements in the array.
11
Write a main function that asks the user to input 10 integers and stores
them in an array. Use your other functions to print the initial array, the
number of zero-valued elements in the array and the contents of the array
when all elements have been tripled. The output has to be as follows:
[.../exercise]$ ./e8
Input 10 numbers: 1 2 3 0 -3 -2 -1 0 10 11
Initial array: { 1, 2, 3, 0, -3, -2, -1, 0, 10, 11 }
Number of 0’s: 2
Tripled array: { 3, 6, 9, 0, -9, -6, -3, 0, 30, 33 }
[.../exercise]$
that takes as argument two strings str1, str2 and appends str2 to str1.
After calling append, the pointer str1 is supposed to point to the concate-
nation of (the original) str1 and str2. The caller of append has to make
sure that enough memory for the result of concatenation is available at the
memory address that str1 points to.
Example
Your implementation needs to make sure that the output string (pointed
to by str1) remains a well-formed string. Recall that, by definition, a string
in C is an array of characters terminated with zero.
Write a main function that asks the user to input 2 words and stores
them in character arrays. Then use your append function to append the
second word to the first, and output the result. The output has to be as
follows:
[.../exercise]$ ./e9
Enter first word: Hello
Enter second word: World
Result of append: HelloWorld
[.../exercise]$
12
Bonus question: Is it possible that an invocation of append changes the
string that str2 points to? Argue why this is not possible, or give an
example program where this happens. In the latter case, make sure that
your implementation of append behaves in an acceptable manner also in
such situations (e.g, your program is not supposed to end up in an infinite
loop).
2 We loop through the array, comparing the elements next to each other
in the array. If a pair is in the wrong order, we swap their position.
[.../exercise]$ ./e10
Number of strings: 8
Maximum string length: 10
Give string 0: Hello
Give string 1: world!
Give string 2: Here
Give string 3: is
Give string 4: a
Give string 5: big
Give string 6: number:
13
Give string 7: 1234567890
Input when sorted:
1234567890
Hello
Here
a
big
is
number:
world!
[.../exercise]$
Exercise 12 Recursion
The Fibonacci sequence is a sequence of numbers where the first two
numbers are 1 and 1 and the next number in the sequence is the sum of the
14
previous two numbers. The n’th number in the sequence can be calculated
as:
f (1) = 1
f (2) = 1
f (n) = f (n − 1) + f (n − 2)
See the example for the seven first numbers in the sequence.
b) Write a program that asks the user for a number n and then prints
the n first numbers in the Fibonacci sequence.
[.../exercise]$ ./e12
Give n: 7
1
1
2
3
5
8
13
[.../exercise]$
[.../exercise]$ ./e13
Give n: 100
1
1
2
3
5
8
13
[...]
708252800
-798870975
-90618175
15
-889489150
-980107325
[.../exercise]$
16
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
7 14 21 28 35 42
8 16 24 32 40 48
9 18 27 36 45 54
10 20 30 40 50 60
11 22 33 44 55 66
12 24 36 48 60 72
[.../assignment3]$
[.../exercise]$ ./e15
Name (Q to quit): Felix
Birthdate: 20121224
Name (Q to quit): Erica
Birthdate: 19980613
Name (Q to quit): Dawn
Birthdate: 19831004
Name (Q to quit): Charles
Birthdate: 19670225
Name (Q to quit): Benny
Birthdate: 20010810
Name (Q to quit): Astrid
Birthdate: 19901105
Name (Q to quit): Greg
Birthdate: 19940423
Name (Q to quit): Q
Astrid, 19901105
Benny, 20010810
Charles, 19670225
17
Dawn, 19831004
Erica, 19980613
Felix, 20121224
Greg, 19940423
[.../exercise]$
18
The following code illustrates use of the previously defined arithmetic
pointer:
int a = 5;
Thanks to their power, function pointers are used often in C libraries, frame-
works, and APIs.
[.../exercise]$ ./e16
Number of inputs: 5
Give number 0: 10
Give number 1: 9
Give number 2: 8
Give number 3: -1
19
Give number 4: -3
Result of applying op_double: { 20, 18, 16, -2, -6 }
Result of applying op_reset: { 0, 0, 0, 0, 0 }
Result of applying op_invert: { -10, -9, -8, 1, 3 }
[.../exercise]$
20