The Ultimate COMP1511 Guide!

Addition (*)

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

Note: You can assume that the values entered will always be 2 whole numbers (i.e integers).

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.


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


Don't Be So Negative! (*)

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:

$ ./negative 3 You have entered a positive number.
$ ./negative -3 Don't be so negative!
$ ./negative 0 You have entered zero.


Print Letters, Given Their Numbers (*)

Write a C program that prints a letter from the alphabet in either uppercase or lowercase.

First, scan in a character. This character will determine whether the letter printed is in uppercase or lowercase."

Should the character be 'y' or 'n', i.e. 'yes' or 'no', the letter will be uppercase or lowercase respectively. If given any other character, print the corresponding error message and terminate the program with the appropriate error number as given in the examples below.

Note: "Terminate the program" does NOT mean an 'exit' function! What is the line that occurs at the end of every main function to terminate the program? (Starts with 'r')

Secondly, scan in an integer. This integer is the index we will use to determine which letter of the alphabet is printed.

Should the integer be a number between 1 and 26 (inclusive), print the corresponding letter of the alphabet. If it is outside of the range, print the corresponding error message and terminate the program with the appropriate error number as given in the examples below.

For example, index 1 will correspond to 'A', index 2 will correspond to 'B' and so on till we reach index 26 which will correspond to 'Z'.

We could manually do a ton of if statements for each index we have to translate to a letter.

BUT, our ASCII table handily puts all the letters side by side in value.

So consider here that, in the alphabet:

  • 'A' has index=1
  • 'B' has index=2
  • ...
  • 'Z' has index=26

We can see our ASCII table by typing ascii into our terminal.

How can we use both the alphabet index and the ASCII table together for this problem?


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

  • You may assume you will always be given a character first.
  • You may assume you will always be given a whole number second.


Leap Year Calculator (**)

Write a C program that reads a year and then prints whether that year is a leap year.

You only need use the int type, modulus (%) and if statement(s).


$ ./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 may assume the tested year is not negative.
  • You may assume the tested year is a whole number.


Dice Range (**)

We will often roll multiple dice at the same time.

Write a C program 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.

Make sure to replace the n with the numbers entered in the same order and the sum with the sum of the two numbers.

Note: You can assume that the user will always enter 2 integers.

$ ./dice_range Enter the number of sides on your dice: 6 Enter the number of sides on your dice: 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 sides on your dice: 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 sides on your dice: 4 Your dice range is 4 to 80. The average value is 42.000000
$ ./dice_range Enter the number of sides on your dice: -5 Enter the number of sides on your dice: 4 These dice will not produce a range.
$ ./dice_range Enter the number of sides on your dice: 6 Enter the number of sides on your dice: 0 These dice will not produce a range.


Word Addition (***)

Create a program that asks for two integers using the message Please enter two integers: and then displays 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.

Note: Make sure to replace the n with the numbers entered in the same order and the sum with the sum of the two numbers.

Note: there is no special trick to convert numbers to words in this exercise. It will require many many if statements, and copy/paste is your friend.

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


Create a Game! (****)

Write a C program 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.

  • 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:
    • The random() function from stdlib.h
    • Functions from math.h (like sin or cos)
    • Colors in your terminal
  • You can only add to the starter code. You should not remove anything that is already there.