if
statementslab02
by
typing:
mkdir lab02Change to this directory by typing:
cd lab02
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
give cs1511 lab02_negative negative.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 17 June 17:00 to obtain the marks for this lab exercise.
Create and open a new file called icecream.c
for this
exercise.
gedit icecream.c &
Matilda wants to buy some ice-cream, but she only has $10. Write a program so that she can input how many scoops of ice-cream she wants and how much each scoop costs and it will let her know if she has enough money.
Your program should behave as follows:
dcc -o icecream icecream.c ./icecream How many scoops? 5 How many dollars does each scoop cost? 1 You have enough money! ./icecream How many scoops? 5 How many dollars does each scoop cost? 3 Oh no, you don't have enough money :(
You can assume that Matilda will only give you positive integers.
1511 style icecream.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest icecream
give cs1511 lab02_icecream icecream.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 17 June 17:00 to obtain the marks for this lab exercise.
Create a program called addition.c
.
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
.
Make sure to replace the n with the numbers entered in the same order and the sum with the sum of the two numbers.
./addition Please enter two integers: 2 5 2 + 5 = 7
./addition Please enter two integers: 3 5 3 + 5 = 8
./addition Please enter two integers: -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
give cs1511 lab02_addition addition.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 17 June 17:00 to obtain the marks for this lab exercise.
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.
./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.
1511 style numberWords.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest numberWords
give cs1511 lab02_numberWords numberWords.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 17 June 17:00 to obtain the marks for this lab exercise.
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
give cs1511 lab02_dice_range dice_range.cYou must run give before Monday 17 June 17:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.
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.
./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
give cs1511 lab02_wordAddition wordAddition.cYou must run give before Monday 17 June 17:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.
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.
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 easter.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest easter
give cs1511 lab02_easter easter.cYou must run give before Monday 17 June 17:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.
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 Monday 17 June 17: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
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 -sturecThe lab exercises for each week are worth in total 2 marks.
The best 8 of your 9 lab marks for weeks 2-10 will be summed to give you a mark out of 13. If their sum exceeds 13 - your mark will be capped at 13.