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.
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/21T1/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 Monday 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/21T1/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 Monday 15 March 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Scan in indicies and replace corresponding elements
For this exercise, make a program called painterbot.c
which
will scan in indices until EOF and then print out a one dimensional array
which has 36 '0's, except at the given indices it has a '1'.
You can assume that every number you are given will be between 0 and 35. You can assume you will never get the same number twice.
./painterbot 5 2 4 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
./painterbot 0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
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 (since there is only one %d
,
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 a different value.
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 painterbot.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest painterbot
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab04_painterbot painterbot.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Monday 15 March 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Create a Simple Calculator, Reading Different Numbers of Integers
For this exercise, make a program called cs_calculator.c
which
will scan in instructions until EOF and prints the output as specified below. An instruction is a sequence of positive integers. The first integer identifies what
type the instruction is.
- If the first number in the instruction is
1
, then your program should print out the square of the next number in the instruction. - If the first number in the instruction is
2
, then your program should print out the value of the next number raised to the power of the number after next.
You can assume that the first number in the instruction is only either
1
or 2
You can assume that for each instruction, the correct number of successive positive integers will be given.
./cs_calculator Enter instruction: 1 2 4 Enter instruction: 2 5 3 125 Enter instruction: 1 4 16 Enter instruction: 2 3 4 81 Enter instruction:
./cs_calculator Enter instruction: 2 3 3 27 Enter instruction: 1 10 100 Enter instruction:
Note: The autotest for this exercise expects your program to end WITHOUT a new line character when the user presses Ctrl+D. This means that the command prompt for the next command should be on the same line as the end of your program.
One major challenge of this exercise is figuring out how to use scanf
effectively.
The lessons you learn in this exercise regarding scanf
will be useful in the first assignment.
Hint: You can scan in the first number initially, and based on the value of that first number then scan in the correct number of additional numbers.
Hint: scanf
will jump over and ignore any
whitespace characters including newline characters. Be careful not to overrun
or underrun your input.
There is no need to print a newline character after the user presses Ctrl+D
1511 style cs_calculator.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest cs_calculator
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab04_cs_calculator cs_calculator.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Monday 15 March 20:00
to obtain the marks for this lab exercise.
Challenge Exercise — individual:
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
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 must
submit your work by running give
:
give cs1511 lab04_reverse_array reverse_array.c
You must run give
before Monday 15 March 20:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
Challenge Exercise — individual:
Help Route an Electric Car Along a Long Road
This challenge does not provide comprehensive test cases
You have been provided some autotests to check your output is sane, and that it is in a correct format. Passing the autotests does not guarantee that you have solved this challenge -- your own testing will be required.
You are in charge of planning a route for an electric car across a long road.
Your electric car takes exactly one unit of charge to travel one kilometer. It has infinite battery capacity, but starts off empty.
Conveniently, every kilometer along this road, there is a charging station, where you can stop to charge your car. These charging stations may have a limited of supply of charge you can use to charge your car. A charging station may have no charge available.
Your job is to determine if it is possible to drive your car to the last charging station on the road, and if so, what the minimum number of stops is to get your car there.
Input Format
You should write a C program,going_electric.c
.
This program will be provided a series of numbers. Each number represents
the charge available from a given charging station. The first number given
is the charge of the first station (where your car begins its journey).
The second number given is the charge at the second station, and so on.
Output Format
Your program should print a single integer, the minimum number of charging stops required to cross the road. This should include the initial charging stop. If it is not possible to drive all the way along the road, you should print the integer 0.Examples
dcc going_electric.c -o going_electric ./going_electric 2 0 0 1
In the above example, the car had to charge at the first station. It then had 2 units of charge, which let it reach the final station. Note that even though that final charging station had no charge to give, the car successfully reached it (just), so this journey is possible.
./going_electric 2 0 0 3 0 0
In the above example, the car could not make it to the last charging station -- the two units of charge at the first station aren't enough to drive to the next charging station with more charge.
./going_electric 1 1 1 1 0 4
In the above example, the car charges at each of the four stations it can. In this way, it just makes it to the final charging station.
./going_electric 3 3 2 1 0 2
In the above example, the car must charge twice, but there are three possible ways it could do so -- it must charge at the first station, but then it could charge at any of the others (excluding the last).
Assumptions, Restrictions & Clarifications
- The road will have no more than 10000 charging stations.
- The road is longer than 1 kilometer (that is, it has at least two charging stations).
- A charging station always has a non-negative amount of charge (that is, either a charging station has a positive amount of charge, or no charge at all).
An Extra Challenge From The Author
As backstory, this challenge was written in a single night, after I nerd-sniped myself.
I'm pretty sure the solution I have to this question is correct, but I invite anyone who is really interested in this problem to submit a mathematical proof of their code being correct. If doing so interests you, you may find the courses COMP3121, COMP4161, COMP2111 and COMP6721 interesting.
Submit your proof via the cs1511.challenge@cse.unsw.edu.au email (be sure to include your zID). Extra Marcs (which aren't worth Marks, but are worth street cred) are available!
1511 style going_electric.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest going_electric
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab04_going_electric going_electric.c
You must run give
before Monday 15 March 20:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
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 5 Monday 20:00 to submit your work.
You cannot obtain marks by e-mailing your code 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,
using test cases different to those autotest
runs for you.
(Hint: do your own testing as well as running autotest
.)
After automarking is run by the lecturer you can view your results here. The resulting mark will also be available 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