Week 04 Laboratory Exercises
Objectives
- using arrays
- creating functions
- using while loops for repetition
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Getting Started
Exercise — in pairs:
Print out pi to a certain number of digits
Write a C program, print_pi.c
, which
prints out the first n digits of pi, where n is specified by the user.
Download print_pi.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/20T1/activities/print_pi/print_pi.c .
You will never be given more than 10
integers to print out.
The output from your program should look exactly like this:
dcc print_pi.c -o print_pi ./print_pi How many digits of pi would you like to print? 3 3.14 ./print_pi How many digits of pi would you like to print? 1 3 ./print_pi How many digits of pi would you like to print? 0
Need a Hint?
The digits of pi are stored in the array called pi
.
While loops are very handy for traversing arrays.
1511 style print_pi.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest print_pi
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab04_print_pi print_pi.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Friday 15 March 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Reverse an Array
Write a C program, reverse_array.c
, which reads integers line by line,
and when it reaches the end of input, prints those integers in reverse order, line by line.
You will never be given more than 100
integers to print out.
The output from your program should look exactly like this:
dcc reverse_array.c -o reverse_array ./reverse_array Enter numbers forwards: 10 50 20 40 Reversed: 40 20 50 10 ./reverse_array Enter numbers forwards: -5 -4 -3 -2 -1 Reversed: -1 -2 -3 -4 -5
Need a Hint?
The result from calling scanf
can be assigned to an integer, like so:
int scanned_in_value; int result = scanf("%d", &scanned_in_value);
result
will be equal to the number of variables that scanf
successfully scanned in.
Explanation
In the code sample above, if the user does enter a number, scanf will be able to
read this in to the variable scanned_in_value
, and thus it will
have successfully scanned in one value, so the variable result
will
contain the value 1.
If the user had not entered a valid number (for example if they typed in a word
instead), there would be no integer there for scanf to read into
scanned_in_value
, so scanf would not successfully scan in any
values, and so the variable result
would contain the value 0.
Detecting the end of input
The variable result
can be used to determine whether your program
has reached the end of the input (hint: if there aren't any values left for
scanf to scan in, the result
variable won't contain the
number 1
).
If you have a while
loop condition that checks the result of
scanf
to determine whether scanf was able to successfully scan the
expected number of variables, you can use this information to keep scanning
numbers until the user tells you to stop.
Note: on Mac/Linux, you can signal "end of input" on the command line by pressing Control + D
1511 style reverse_array.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest reverse_array
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab04_reverse_array reverse_array.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Friday 15 March 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Fun facts about Circles
Download circle_facts.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/20T1/activities/circle_facts/circle_facts.c .
Your task is to add code to these functions in circle_facts.c:
// Calculate the area of a circle, given its radius.
double area(double radius) {
// TODO: complete this function.
return M_PI; // TODO: change this to the correct return value.
}
// Calculate the circumference of a circle, given its radius.
double circumference(double radius) {
// TODO: complete this function.
return M_PI; // TODO: change this to the correct return value.
}
// Calculate the diameter of a circle, given its radius.
double diameter(double radius) {
// TODO: complete this function.
return M_PI; // TODO: change this to the correct return value.
}
Its main function is complete. Do not change the main function. Complete these three functions:
double area(double radius); double circumference(double radius); double diameter(double radius);
Hint use the constant M_PI
defined in math.h
dcc -o circle_facts circle_facts.c ./circle_facts Enter circle radius: 1 Area = 3.141593 Circumference = 6.283185 Diameter = 2.000000 ./circle_facts Enter circle radius: 17 Area = 907.920277 Circumference = 106.814150 Diameter = 34.000000 ./circle_facts Enter circle radius: 0.0125 Area = 0.000491 Circumference = 0.078540 Diameter = 0.025000
1511 style circle_facts.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest circle_facts
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab04_circle_facts circle_facts.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Friday 15 March 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Find the Maximum Value in an Array of Integers
Download array_maximum.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/20T1/activities/array_maximum/array_maximum.c .
Your task is to add code to this function in array_maximum.c:
// Returns the maximum value from an array of `size` integers.
// Note: this function must not modify the array.
int array_maximum(int size, int array[size]) {
int max = 42; // TODO: change this line!
// TODO: complete this function.
return max;
}
The above file array_maximum.c contains a function array_maximum, which should find the maximum value in the array.
Unfortunately, the provided function doesn't actually work. For this lab exercise, your task is to complete this function.
Note: you must not modify the array within the array_maximum function. You should only read the values in the array, not change them.
The file also contains a main function which you can use to help test your array_maximum function. It has two simple test cases.
This main function will not be marked -- you must write all of your code in the array_maximum function. You may modify the main function if you wish (e.g. to add further tests), but only the array_maximum function will be marked.
Once your program is working, the output from the two provided tests in the main function should be:
dcc -o array_maximum array_maximum.c ./array_maximum The largest value from array1 is: 15 The largest value from array2 is: 512
1511 style array_maximum.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest array_maximum
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab04_array_maximum array_maximum.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Friday 15 March 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Print out a 2D Array
Download print_2D_array.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/20T1/activities/print_2D_array/print_2D_array.c .
Your task is to add code to these functions in print_2D_array.c:
void print_2D_array(int array[SIZE][SIZE]) {
//TODO: Fill in this function
}
Its main function is complete. Do not change the main function. Complete the function:
void print_2D_array(int array[SIZE][SIZE])
You can assume that the size of the array will always be of
the #defined value SIZE
.
The output should be in the format:
dcc -o print_2D_array print_2D_array.c ./print_2D_array 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
1511 style print_2D_array.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest print_2D_array
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab04_print_2D_array print_2D_array.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Friday 15 March 20:00
to obtain the marks for this lab exercise.
Submission
give
.
You can run give
multiple times.
Only your last submission will be marked.
Don't submit any exercises you haven't attempted.
If you are working at home, you may find it more convenient to upload your work via give's web interface.
Remember you have until Week 4 Sunday 20:00 to submit your work.
You cannot obtain marks by e-mailing lab work to tutors or lecturers.
You check the files you have submitted here
Automarking will be run by the lecturer several days after the submission deadline
for the test, using test cases that you haven't seen:
different to the test cases autotest
runs for
you.
(Hint: do your own testing as well as running
autotest
)
After automarking is run by the lecturer you can view it here the resulting mark will also be available via via give's web interface
Lab Marks
When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:
1511 classrun -sturec