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

Activities To Be Completed

The following is a list of all the activities available to complete this week...

Worth one mark in total:

  • negative
  • addition
  • get_letter
  • is_leap_year

Worth half a mark in total:

  • calculator
  • dice_range

Worth half a mark in total:

  • easter
  • word_addition

For your interest, but not for marks:

  • text_game

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

Exercise
(●◌◌)
:

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

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 27 September 20:00 to obtain the marks for this lab exercise.

Exercise
(●◌◌)
:

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

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 27 September 20:00 to obtain the marks for this lab exercise.

Exercise
(●◌◌)
:

Print Letters, Given Their Numbers

Write a C program get_letter.c which reads in a character and an integer.

The character should be either a 'y' or a 'n'. If it is any other character, print an error message and exit the program. If the user inputs 'y', print the letter as uppercase, and if the user inputs 'n', print the letter as lowercase.

The integer should be a number between 1 and 26 inclusive. If it is outside of the range, print an error message and exit the program. This integer is the index we will use to determine the letter of the alphabet. For example, 1 will correspond to 'A', 2 will correspond to 'B' and so on till we reach 26 which will correspond to 'Z'.

./get_letter
Uppercase: y
Index: 5
The letter is E
./get_letter
Uppercase: n
Index: 18
The letter is r
./get_letter
Uppercase: a
You need to enter 'y' or 'n'
Exiting the program with error code 1
./get_letter
Uppercase: y
Index: 100
You need to enter a number between 1 and 26 inclusive
Exiting the program with error code 2

Assumptions/Restrictions/Hints

  • You may assume you will always be given a character first.
  • You may assume you will always be given a whole number second.
You can run an automated code style checker using the following command:
1511 style get_letter.c

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

1511 autotest get_letter

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab02_get_letter get_letter.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 27 September 20:00 to obtain the marks for this lab exercise.

Exercise
(●◌◌)
:

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.

Make sure you click on the link above to learn how to determine leap years!

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.
You can run an automated code style checker using the following command:
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 27 September 20:00 to obtain the marks for this lab exercise.

Exercise
(●●◌)
:

Calculator

Write a C program calculator.c which reads in three integers which make up an instruction.

The first number in the instruction determines if we add or subtract.

  • If the first number is 1, then your program should print the addition of the second and third numbers.
  • If the first number is 2, then your program should print the subtraction of the third number from the second.
./calculator
Enter instruction: 1 2 3
5
./calculator
Enter instruction: 2 7 3
4
./calculator
Enter instruction: 1 5 -3
2

Assumptions/Restrictions/Hints

  • You may assume all inputs given to your program will be integers.
  • You may assume the first number in the instruction will always be 1 or 2.
You can run an automated code style checker using the following command:
1511 style calculator.c

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

1511 autotest calculator

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab02_calculator calculator.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 27 September 20:00 to obtain the marks for this lab exercise.

Exercise
(●●◌)
:

Dice Range

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

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab02_dice_range dice_range.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 27 September 20:00 to obtain the marks for this lab exercise.

Exercise
(●●●)
:

Easter

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!

Your code won't pass 1511 style. That's okay, but only for this exericse...
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

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab02_easter easter.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 27 September 20:00 to obtain the marks for this lab exercise.

Exercise
(●●●)
:

Word Addition

Create a program called word_addition.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

./word_addition
Please enter two integers: 2 5
two + five = seven
./word_addition
Please enter two integers: 3 0
three + zero = three
./word_addition
Please enter two integers: -1 5
negative one + five = four
./word_addition
Please enter two integers: 10 5
ten + five = 15
Note: there is no special trick to this exercise. It will require many lines, and copy/paste is your friend.
You can run an automated code style checker using the following command:
1511 style word_addition.c

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

1511 autotest word_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_word_addition word_addition.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 27 September 20:00 to obtain the marks for this lab exercise.

Exercise
(☠)
:

Create a Game!

This is an open ended challenge, and is not necessarily difficult.

We encourage everyone to have a go!

Write a C program text_game.c which reads in integers and/or characters until the user wins or loses the game.

This is the starter code for the exercise. Add to it to create an adventure game.

// My Text Game
// text_game.c
//
// This program was written by YOUR-NAME-HERE (z5555555)
// on INSERT-DATE-HERE
//
// One line summary of what this exercise does.

#include <stdio.h>

int main(void) {
    // TODO: Put any setup code here
    
    // This code will make anything in the brackets run forever
    while (1) {
        // TODO: Fill this in with the game
    }
    
    return 0;
}

Tom (one of our course admins) has made his own adventure game using the concepts from Week 1 and 2 which you can play around with. Use the command 1511 toms_adventure to play!

There is no submission for this lab exercise! Unlike any other lab, though, you are welcome to share your code for this exercise. Post it on the forums at this link :)

Assumptions/Restrictions/Hints

  • You should only use the techniques taught to you in Week 1 and 2:
    • Printf / Scanf
    • Math (+, -, /, *)
    • Char, Int and Double variables
    • #define constants
    • If Statements
    • Structs
    • While Loops
  • If you want to get extra creative, you can also use:
  • You can only add to the starter code. You should not remove anything that is already there.
You can run an automated code style checker using the following command:
1511 style text_game.c

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