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 numberif the input is below or equal to zero,That number is between 0 and 100if the input is less than 100 but greater than zero,- or
That's a big numberif 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
best_number_debug.c
#include <stdio.h>
int main(void) {
printf("What is the best number? ");
int num; // bug: was char, should be int
scanf("%d",&num);
if (num <= 0) {
printf("Thats a small number\n");
} else if ( num > 0 && num <= 100){
printf("That number is between 1 and 100\n");
} else { // bug: was missing this else nest. That's a big number was printing every time
printf("Thats a big number\n");
}
return 0;
}
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
number_guess.c
#include <stdio.h>
int main(void) {
int num;
printf("Number guess: ");
scanf("%d", &num);
if (num != 42) {
printf("Nope!\n");
} else {
printf("The answer to the Ultimate Question of Life, the Universe, and Everything!\n");
}
return 0;
}
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:
- Prompt the user with
Player Scores:and scan in two integers representing Player 1's score and Player 2's score. - 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!.
- If Player 1 has at least 11 points and is ahead by 2 points or more, print:
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
ping_pong.c
#include <stdio.h>
int main(void) {
int player1;
int player2;
printf("Player Scores: ");
scanf("%d", &player1);
scanf("%d", &player2);
if (player1 < 11 && player2 < 11) {
printf("The game continues!\n");
} else if (player1 - player2 < 2 && player1 - player2 > -2) {
printf("The game continues!\n");
} else if (player1 > player2) {
printf("Player 1 wins!\n");
} else {
printf("Player 2 wins!\n");
}
return 0;
}
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:
Diameter of the pizza:(a double).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.WhereDis the diameter of the pizza andAis 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
pizza_slice_size.c
#include <stdio.h>
#define PI 3.14159
int main(void) {
double diameter;
int slices;
double area;
printf("Diameter of the pizza: ");
scanf(" %lf", &diameter);
printf("Number of slices: ");
scanf(" %d", &slices);
area = PI * (diameter / 2) * (diameter / 2);
area = area / slices;
printf("The pizza with a diameter %.3lf has slices with area %.3lf.\n", diameter, area);
return 0;
}
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:
- If the final population is 0, print:
The final number of mice is 0. The farmer's crop is safe from the mice. - 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. - 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.
- If the final population is 0, print:
(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
mice_population.c
#include <stdio.h>
#include <math.h>
int main(void) {
int initial;
double rate;
int period;
int final;
printf("Initial population of mice: ");
scanf(" %d", &initial);
printf("Rate of increase: ");
scanf(" %lf", &rate);
printf("Number of periods: ");
scanf(" %d", &period);
final = initial * pow((1+rate),period);
if (final == 0) {
printf("The final number of mice is %d. The farmer's crop is safe from the mice.\n", final);
} else if (final < 5000) {
printf("The final number of mice is %d. The farmer's crop is being eaten by the mice.\n", final);
} else {
printf("The final number of mice is %d. The farmer's crop was eaten by the mice.\n", final);
}
return 0;
}
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:
Is the animal large (y/n)?- read as a character ('y'for Yes,'n'for No).Does the animal swim (y/n)?- read as an integer ('y'for Yes,'n'for No).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!, whereXis 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
zoo_game.c
#include <stdio.h>
int main(void) {
char large;
char swim;
char eats;
printf("Welcome to the Taronga zoo game, please answer the following questions:\n");
printf("Is the animal large (y/n)? ");
scanf(" %c", &large);
printf("Does the animal swim (y/n)? ");
scanf(" %c", &swim);
printf("Is the animal a herbivore, carnivore or omnivore (h/c/o)? ");
scanf(" %c", &eats);
if (large == 'y') {
if (swim == 'y') {
if (eats == 'o') {
printf("The animal is a Pelican!\n");
} else if (eats == 'h') {
printf("The animal is a Green Sea Turtle!\n");
} else {
printf("The animal is a Sea Lion!\n");
}
} else {
if (eats == 'o') {
printf("The animal is a Emu!\n");
} else if (eats == 'h') {
printf("The animal is a Kangaroo!\n");
} else {
printf("The animal is a Perentie!\n");
}
}
} else {
if (swim == 'y') {
if (eats == 'o') {
printf("The animal is a Black Swan!\n");
} else if (eats == 'h') {
printf("The animal is a Swamp Wallaby!\n");
} else {
printf("The animal is a Platypus!\n");
}
} else {
if (eats == 'o') {
printf("The animal is a Quokka!\n");
} else if (eats == 'h') {
printf("The animal is a Koala!\n");
} else {
printf("The animal is a Dingo!\n");
}
}
}
return 0;
}