Programming Fundamentals

Information

  • This page contains additional revision exercises for week 02.
  • These exercises are not compulsory, nor do they provide any marks in the course.
  • You cannot submit any of these exercises, however autotests may be available for some them (Command included at bottom of each exercise if applicable).

Exercise — individual:
Debug Best Number

Download best_number_debug.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity best_number_debug

Debugging Tips!

Some debugging tips for you:

  • dcc output - as you run into issues, dcc will point you to where the errors are. Remember that dcc gives you the line number the issue is on, and will give some sort of explanation. Make sure you read everything dcc gives you. Sometimes we get “errors carried forward”, so find your first error, fix that, then recompile.
  • print statements - sometimes it can be handy to see if the flow of your code puts you in the spot you expect it to be (ie. inside the right if statement, or going through a loop the correct amount of times). A quick way you can check this is by putting print statements in your code for testing purposes, like "the value of x is %d and y is %d". This lets you check that you got against what you expected.
  • COMP1511 debugging guide

The Task

This exercise should scan in an integer and, based on the input, print:

  • That's a small number if the input is below or equal to zero,
  • That number is between 0 and 100 if the input is less than 100 but greater than zero,
  • or That's a big number if the input is larger than 100.

Currently it has some issues - it is your job to figure them out and fix the code.

Examples

dcc best_number_debug.c -o best_number_debug
./best_number_debug
What is the best number? 0
Thats a small number
./best_number_debug
What is the best number? 50
That number is between 1 and 100
./best_number_debug
What is the best number? 101
Thats a big number

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

1511 autotest best_number_debug

Exercise — individual:
Number Guess

Write a program called number_guess.c that scans in a integer and compares the integer to the number 42.

  • If the input is equivalent to 42, print the win message: The answer to the Ultimate Question of Life, the Universe, and Everything!.
  • If the input is not equivalent to 42, print the loss message: Nope!.

Examples

dcc number_guess.c -o number_guess
./number_guess
Number guess: 9
Nope!
./number_guess
Number guess: 42
The answer to the Ultimate Question of Life, the Universe, and Everything!

Assumptions/Restrictions/Clarifications

  • You may assume that the values entered will always be positive whole numbers (i.e. integers).

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

1511 autotest number_guess

Exercise — individual:
Ping Pong

In table tennis, a game is played to 11 points. However, to win the game, a player must have a lead of at least 2 points over their opponent.

If a player has 11 or more points but does not have a 2-point lead, the game must continue until one player successfully establishes a 2-point advantage.

Write a C program called ping_pong.c that evaluates the current scores of a ping pong game and determines the state of the match.

The program should:

  1. Prompt the user with Player Scores: and scan in two integers representing Player 1's score and Player 2's score.
  2. Evaluate the scores and print exactly one of the following outcomes:
    • If Player 1 has at least 11 points and is ahead by 2 points or more, print: Player 1 wins!.
    • If Player 2 has at least 11 points and is ahead by 2 points or more, print: Player 2 wins!.
    • Otherwise, print: The game continues!.

Examples

dcc ping_pong.c -o ping_pong
./ping_pong
Player Scores: 11 9
Player 1 wins!
./ping_pong
Player Scores: 8 11
Player 2 wins!
./ping_pong
Player Scores: 11 10
The game continues!

Assumptions/Restrictions/Clarifications

  • You can assume that the values entered will always be positive whole numbers.

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

1511 autotest ping_pong

Exercise — individual:
Pizza Slice Size

You work for a pizza store that would like a program to calculate the area of each slice of their pizzas.

Write a program called pizza_slice_size.c that should:

  • Prompt the user and scan in two values:

    1. Diameter of the pizza: (a double).
    2. Number of slices: (an integer).
  • Calculate the area of each slice of pizza.

  • Print the output: The pizza with a diameter D has slice's with area A. Where D is the diameter of the pizza and A is the area per slice, both printed to 3 decimal places.

Examples

dcc pizza_slice_size.c -o pizza_slice_size
./pizza_slice_size
Diameter of the pizza: 29.8
Number of slices: 12
The pizza with a diameter 29.800 has slices with area 58.122.
./pizza_slice_size
Diameter of the pizza: 10.0
Number of slices: 4
The pizza with a diameter 10.000 has slices with area 19.635.
./pizza_slice_size
Diameter of the pizza: 30.0
Number of slices: 1
The pizza with a diameter 30.000 has slices with area 706.858.

Assumptions/Restrictions/Clarifications

  • You can assume that the values entered will always be positive numbers.

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

1511 autotest pizza_slice_size

Exercise — individual:
Mice Population

Mice populations grow rapidly and pose a severe threat to a farmer's crops. To help farmers prepare, you will write a program that models population growth using the following mathematical formula:

final_population = initial_population * (1 + growth_rate) ^ periods

Create a program called mice_population.c that should:

  • Prompt the user and scan in three distinct values in this exact order:
    • Initial Population: The starting number of mice (an integer).
    • Growth Rate: The rate of increase per period (a double, e.g., 0.15 for 15%).
    • Periods: The number of time periods tracked (an integer).
  • Calculate the final population as an integer.
  • Print one of three messages depending on the result:
    1. If the final population is 0, print: The final number of mice is 0. The farmer's crop is safe from the mice.
    2. If the final population is between 1 and 4999 (inclusive), print: The final number of mice is X. The farmer's crop is being eaten by the mice.
    3. If the final population is 5000 or more, print: The final number of mice is X. The farmer's crop is eaten by the mice.

(Where X is the final truncated integer population).

Examples

dcc mice_population.c -o mice_population
./mice_population
Initial population of mice: 50
Rate of increase: 0.5
Number of periods: 15
The final number of mice is 21894. The farmer's crop was eaten by the mice.
./mice_population
Initial population of mice: 10
Rate of increase: -0.5
Number of periods: 7
The final number of mice is 0. The farmer's crop is safe from the mice.
./mice_population
Initial population of mice: 500
Rate of increase: 0.05
Number of periods: 4
The final number of mice is 607. The farmer's crop is being eaten by the mice.

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

1511 autotest mice_population

Exercise — individual:
Zoo Game

Taronga Zoo wants an educational, interactive game to teach children about the characteristics of iconic Australian wildlife. Your task is to write a program called zoo_game.c that:

  • Prompts the user and scans inputs in this exact order:

    1. Is the animal large (y/n)? - read as a character ('y' for Yes, 'n' for No).
    2. Does the animal swim (y/n)? - read as an integer ('y' for Yes, 'n' for No).
    3. Diet type: - read as a single lowercase character ('h' for Herbivore, 'c' for Carnivore, 'o' for Omnivore).
  • Determines the unique animal that matches the details provided by the inputs and prints: The animal is a X!, where X is the name of the animal.

Your program must map the user's choices to the correct animal using the table below:

Animal Large? Swims? Diet
Green Sea Turtle Yes Yes Herbivore
Perentie Yes No Carnivore
Pelican Yes Yes Omnivore
Swamp Wallaby No Yes Herbivore
Quokka No No Omnivore
Black Swan No Yes Omnivore
Kangaroo Yes No Herbivore
Koala No No Herbivore
Sea Lion Yes Yes Carnivore
Dingo No No Carnivore
Platypus No Yes Carnivore
Emu Yes No Omnivore

Examples

dcc zoo_game.c -o zoo_game
./zoo_game
Welcome to the Taronga zoo game, please answer the following questions: 
Is the animal large (y/n)? y
Does the animal swim (y/n)? y
Is the animal a herbivore, carnivore or omnivore (h/c/o)? o
The animal is a Pelican!
./zoo_game
Welcome to the Taronga zoo game, please answer the following questions: 
Is the animal large (y/n)? n
Does the animal swim (y/n)? n
Is the animal a herbivore, carnivore or omnivore (h/c/o)? h
The animal is a Koala!

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

1511 autotest zoo_game