Week 02 Laboratory Exercises
Objectives
- using C input/output (I/O) facilities
- creating simple arithmetic expressions
- programming with
if
statements - creating relational expressions
- displaying varying strings
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Exercise — in pairs:
Don't Be So Negative!
Now create and open a new file called negative.c
for this exercise.
gedit negative.c &
Write a program that uses scanf
to get a number from a user and prints
"Don't be so negative!" if they entered a negative number.
If the number is positive, the program should print "You have entered a positive number."
If the user enters the number 0, the program should print
"You have entered zero."
Note: you can assume that the number will always be a whole number (i.e. an integer)
Your program should behave as follows:
dcc -o negative negative.c ./negative 3 You have entered a positive number. ./negative -3 Don't be so negative! ./negative 0 You have entered zero.
1511 style negative.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest negative
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab02_negative negative.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Monday 01 March 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Addition
Create a program called addition.c
, which will add together the number of students and tutors in a class.
This program should ask for two integers
using the message
Please enter the number of students and tutors:
and then display the sum of the integers as
students + tutors = sum
.
Make sure to replace students and tutors with the numbers entered in the same order and the sum with the sum of the two numbers.
Some Examples
./addition Please enter the number of students and tutors: 2 5 2 + 5 = 7
./addition Please enter the number of students and tutors: 3 5 3 + 5 = 8
./addition Please enter the number of students and tutors: -1 5 -1 + 5 = 4
1511 style addition.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest addition
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab02_addition addition.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Monday 01 March 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Numbers to Words
Make a program called numberWords.c
.
This program will ask for a number
with the message
Please enter an integer:
.
For numbers between 1 and 5,
display the number as a word in the message
You entered number
.
For numbers less than 1,
display the message
You entered a number less than one.
For numbers greater than 5,
display the message
You entered a number greater than five.
Some Examples
./numberWords Please enter an integer: 2 You entered two.
./numberWords Please enter an integer: 5 You entered five.
./numberWords Please enter an integer: 0 You entered a number less than one.
./numberWords Please enter an integer: 1000 You entered a number greater than five.
This exercise is designed you give you practice using if statements. There are more efficient ways to solve this exercise which we will learn about in later weeks. Please only use content which we have covered in the course so far for this exericse.
1511 style numberWords.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest numberWords
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab02_numberWords numberWords.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Monday 01 March 20:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Leap Year Calculator
Write a C program is_leap_year.c
that reads a year
and then
prints whether that year is a leap year.
Match the examples below exactly
Hint: you only need use the int type, modulus (%) and if statement(s).
For example:
dcc -o is_leap_year is_leap_year.c ./is_leap_year Enter year: 2017 2017 is not a leap year. ./is_leap_year Enter year: 2016 2016 is a leap year. ./is_leap_year Enter year: 2000 2000 is a leap year. ./is_leap_year Enter year: 3000 3000 is not a leap year.
1511 style is_leap_year.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest is_leap_year
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab02_is_leap_year is_leap_year.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Monday 01 March 20:00
to obtain the marks for this lab exercise.
Challenge Exercise — individual:
Dice Range
Write a C program dice_range.c
that reads the number of sides on a set of dice and how many of them are being rolled.
It then outputs the range of possible totals that these dice can produce as well as the average value.
Hint: use the examples below to clarify the expected behaviour of your program.
For example:
dcc -o dice_range dice_range.c ./dice_range Enter the number of sides on your dice: 6 Enter the number of dice being rolled: 2 Your dice range is 2 to 12. The average value is 7.000000 ./dice_range Enter the number of sides on your dice: 8 Enter the number of dice being rolled: 3 Your dice range is 3 to 24. The average value is 13.500000 ./dice_range Enter the number of sides on your dice: 20 Enter the number of dice being rolled: 4 Your dice range is 4 to 80. The average value is 42.000000You'll also need to check for invalid dice or situations where the range is empty. Those situations should produce these results:
./dice_range Enter the number of sides on your dice: -5 Enter the number of dice being rolled: 4 These dice will not produce a range. ./dice_range Enter the number of sides on your dice: 6 Enter the number of dice being rolled: 0 These dice will not produce a range.
1511 style dice_range.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest dice_range
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_dice_range dice_range.c
You must run give
before Monday 01 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:
Word Addition
Create a program called wordAddition.c
.
For this challenge exercise, you can only use the topics that we've covered in the course thus far (if/elseif/else, printf, scanf, ints).
This program should ask for two integers
using the message
Please enter two integers:
and then display the sum of the integers as
n + n = sum
.
Any numbers that are between zero and ten should appear as words. This also applies to negative numbers between negative ten and zero. All other numbers should appear as decimal integers.
Make sure to replace the n with the numbers entered in the same order and the sum with the sum of the two numbers.
Some Examples
./wordAddition Please enter two integers: 2 5 two + five = seven
./wordAddition Please enter two integers: 3 0 three + zero = three
./wordAddition Please enter two integers: -1 5 negative one + five = four
./wordAddition Please enter two integers: 10 5 ten + five = 15
1511 style wordAddition.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest wordAddition
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_wordAddition wordAddition.c
You must run give
before Monday 01 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:
Easter
easter.c
which allows the user to enter a year,
then calculates the date of Easter Sunday for that year.
Use the formula developed in 1876 by
Samuel Butcher, Bishop of Meath,.
Follow the output format in in the example below exactly:
dcc easter.c -o easter ./easter Enter year: 2017 Easter is April 16 in 2017. ./easter Enter year: 2018 Easter is April 1 in 2018. ./easter Enter year: 2019 Easter is April 21 in 2019.
Hints
Cut-and-paste the formula from the above Web site and then fill in the C program around it.Make sure every variable is declared.
Make sure every statement ends with a semicolon.
Note that the original proposal of this formula had only single letter variable names, and no explanation of how it works.
Because of this, even though we know that it works, no-one knows how it works.
Make sure to always comment your code and have sensible variable names so that people can understand how your code works!
1511 style
. That's okay, but only for this exericse...
1511 style easter.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest easter
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_easter easter.c
You must run give
before Monday 01 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 3 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