Week 04 Laboratory Sample Solutions
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.
print_pi.c
//Prints the first n digits of pi, where n is specified
// by the user
// Liz Willer
// 2020-03-07
#include <stdio.h>
#define MAX_DIGITS 10
int main(void) {
int pi[MAX_DIGITS] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
//From here
printf("How many digits of pi would you like to print? ");
int digits;
scanf("%d", &digits);
int i = 0;
while (i < digits) {
printf("%d", array[i]);
if (i == 0){
printf(".");
}
i++;
}
printf("\n");
//To here
return 0;
}
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.
reverse_array.c
// A program to print a list of integers in reverse
// Written by Tom Kunc (t.kunc@unsw.edu.au)
// Created in 2019-06-23
#include <stdio.h>
#define MAX_NUMBERS 100
int main(void) {
int i = 0;
int did_scan_something = 0;
int scanned_value;
int scanned_numbers[MAX_NUMBERS];
printf("Enter numbers forwards:\n");
while (scanf("%d", &scanned_value) == 1) {
scanned_numbers[i] = scanned_value;
did_scan_something = 1;
i++;
}
printf("Reversed:\n");
while (i > 0 && did_scan_something) {
i--;
printf("%d\n", scanned_numbers[i]);
}
return 0;
}
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.
circle_facts.c
// Alex Linker 2017-08-13
// This program prints out facts about a circle given its radius,
// using functions.
#include <stdio.h>
#include <math.h>
double area(double radius);
double circumference(double radius);
double diameter(double radius);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
double radius;
printf("Enter circle radius: ");
scanf("%lf", &radius);
printf("Area = %lf\n", area(radius));
printf("Circumference = %lf\n", circumference(radius));
printf("Diameter = %lf\n", diameter(radius));
return 0;
}
// Calculate the area of a circle, given its radius.
double area(double radius) {
return M_PI * radius * radius;
}
// Calculate the circumference of a circle, given its radius.
double circumference(double radius) {
return M_PI * 2 * radius;
}
// Calculate the diameter of a circle, given its radius.
double diameter(double radius) {
return 2 * radius;
}
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.
array_maximum.c
// COMP1511 Week 5 Lab: Array Maximum
// A sample solution by Andrew Bennett, 2019-03-16.
//
// This program calculates the maximum value in an array of integers.
#include <stdio.h>
// Returns the maximum value from an array of `size` integers.
int array_maximum(int size, int array[size]) {
int max = array[0];
int i = 1;
while (i < size) {
if (array[i] > max) {
max = array[i];
}
i = i + 1;
}
return max;
}
// This is a simple main function that you can use to test your array_maximum
// function.
// It will not be marked - only your array_maximum function will be marked.
//
// Note: the autotest does not call this main function!
// It calls your array_maximum function directly.
// Any changes that you make to this function will not affect the autotests.
#define MAX_SIZE 100
int main(void) {
int size1 = 7;
int array1[MAX_SIZE] = {3, 14, 15, 9, 2, 6, 5};
int result1 = array_maximum(size1, array1);
printf("The largest value from array1 is: %d\n", result1);
int size2 = 10;
int array2[MAX_SIZE] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
int result2 = array_maximum(size2, array2);
printf("The largest value from array2 is: %d\n", result2);
// Note: you can add more tests here (but they won't be marked).
return 0;
}
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.
print_2D_array.c
// Print a 2D array
// Liz Willer
// 2020-03-07
#include <stdio.h>
#define SIZE 4
void print_2D_array(int array[SIZE][SIZE]);
int main(void) {
int array[SIZE][SIZE] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}};
print_2D_array(array);
return 0;
}
void print_2D_array(int array[SIZE][SIZE]) {
int i = 0;
while (i < SIZE) {
int j = 0;
while (j < SIZE) {
printf("%02d", array[i][j]);
if (j < SIZE - 1){
printf(" ");
}
j++;
}
printf("\n");
i++;
}
}
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