COMP1511 19T2
COMP1511 19T2

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.

Getting Started

Create a new directory for this lab called lab02 by typing:
mkdir lab02
Change to this directory by typing:
cd lab02

Exercise: Don't Be So Negative! (pair)

This is a pair exercise to complete with your lab partner.

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.
New! You can run an automated code style checker using the following command:
1511 style negative.c

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest negative

Autotest Results

97% of 542 students who have autotested negative.c so far, passed all autotest tests.
  • 98% passed test negative_0
  • 98% passed test negative_1
  • 98% passed test positive_0
  • 98% passed test positive_1
  • 97% passed test zero
When you are finished 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 17 June 17:00 to obtain the marks for this lab exercise.

Exercise: Icecream Scoops (pair)

This is a pair exercise to complete with your lab partner.

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.

New! You can run an automated code style checker using the following command:
1511 style icecream.c

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest icecream

Autotest Results

99% of 533 students who have autotested icecream.c so far, passed all autotest tests.
  • 99% passed test icecream_0
  • 99% passed test icecream_1
  • 99% passed test icecream_2
  • 99% passed test icecream_3
  • 99% passed test icecream_4
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab02_icecream icecream.c
Note, 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.

Exercise: Addition (pair)

This is a pair exercise to complete with your lab partner.

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.

Some Examples

./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
New! You can run an automated code style checker using the following command:
1511 style addition.c

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest addition

Autotest Results

99% of 522 students who have autotested addition.c so far, passed all autotest tests.
  • 99% passed test addition_1
  • 99% passed test addition_2
  • 99% passed test addition_3
  • 99% passed test addition_4
When you are finished 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 17 June 17:00 to obtain the marks for this lab exercise.

Exercise: Numbers to Words (pair)

This is a pair exercise to complete with your lab partner.

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.
New! You can run an automated code style checker using the following command:
1511 style numberWords.c

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest numberWords

Autotest Results

100% of 512 students who have autotested numberWords.c so far, passed all autotest tests.
  • 100% passed test greater_1
  • 100% passed test greater_2
  • 100% passed test less_1
  • 100% passed test less_2
  • 100% passed test num_1
  • 100% passed test num_2
  • 100% passed test num_3
When you are finished 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 17 June 17:00 to obtain the marks for this lab exercise.

Challenge Exercise: Dice Range (individual)

This is an individual exercise to complete by yourself.
We will often roll multiple dice at the same time.

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.000000
You'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.
New! You can run an automated code style checker using the following command:
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

Autotest Results

93% of 395 students who have autotested dice_range.c so far, passed all autotest tests.
  • 94% passed test dice_range_0
  • 94% passed test dice_range_1
  • 95% passed test dice_range_3
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 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.

Challenge Exercise: Word Addition (individual)

This is an individual exercise to complete by yourself.

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
New! You can run an automated code style checker using the following command:
1511 style wordAddition.c

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest wordAddition

Autotest Results

96% of 277 students who have autotested wordAddition.c so far, passed all autotest tests.
  • 97% passed test all_words_1
  • 97% passed test all_words_2
  • 97% passed test all_words_3
  • 96% passed test greater_1
  • 97% passed test greater_2
  • 96% passed test less_1
  • 96% passed test less_2
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 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.

Challenge Exercise: Easter (individual)

This is an individual exercise to complete by yourself.
Write a program 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!

New! You can run an automated code style checker using the following command:
1511 style easter.c

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest easter

Autotest Results

98% of 296 students who have autotested easter.c so far, passed all autotest tests.
  • 99% passed test easter_0
  • 99% passed test easter_1
  • 99% passed test easter_2
  • 99% passed test easter_3
  • 98% passed test easter_4
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 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.

Submission

When you are finished each exercises make sure you submit your work by running 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 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

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
The 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.

  • You can obtain full marks for the labs without completing challenge exercises
  • You can miss 1 lab without affecting your mark.