Week 2 Code Examples
// A program to revise the following concepts
// Tricky expressions using
// int, double, char
//
// Written by: Angela Finlayson
// Date: 16/09/2026
#include <stdio.h>
int main(void) {
int x = 11;
int y = x / 2;
int remainder = x % 3;
char letter = 'B' - 'A' + 'a';
printf("%d modulus 3 = %d\n", x, remainder);
printf("%d / 2 = %d\n", x, y);
printf("'B' - 'A' + 'a' = %c\n", letter);
return 0;
}
// A program to revise the following concepts
// Declaring, initialising, modifying and printing variables of types
// int, double, char
// Using simple mathematical operators
//
// Written by: Angela Finlayson
// Date: 16/09/2026
//
// This program prints out details of the player's initials
// gold and health
// The gold and health get updated.
#include <stdio.h>
int main(void) {
int gold = 10;
double health = 85;
char initial = 'X';
printf("Player: %c\n", initial);
printf("Gold: %d Health: %.2lf\n", gold, health);
gold = gold + 1; //10 + 1 = 11
health = health / 2; //85 / 2 = 42.5
printf("Gold: %d Health: %.1lf\n", gold, health);
// How could we print to 1 decimal place?
return 0;
}
// A program to revise the following concepts
// Using scanf to read in data from the user of types
// int and double
//
// Written by: Angela Finlayson
// Date: 16/09/2026
//
// This program reads in integer coordinates and a real value for a length
#include <stdio.h>
// In class
// Show error input - see how to handle this later today!!
int main(void) {
int x;
int y;
printf("Enter coordinates: ");
scanf("%d %d", &x, &y);
printf("You entered (%d, %d)\n", x, y);
double length;
printf("Enter length: ");
scanf("%lf", &length);
printf("You entered %lf\n", length);
return 0;
}
// This is an example of using constants
// to give meaning to values used in our code
#include <stdio.h>
#define STARTING_GOLD 100
#define SWORD_COST 40
#define SHIELD_COST 30
#define POT_OF_GOLD 50
int main (void) {
int gold = STARTING_GOLD;
gold = gold - SWORD_COST;
gold = gold + POT_OF_GOLD;
gold = gold - SHIELD_COST;
printf("Gold: %d\n", gold);
return 0;
}
// This is an example of use magic numbers
// A magic number is an unnamed hardcoded value whose meaning is unclear.
// Compare this code to the code in constants.c that
// replaces magic numbers with #define constants
#include <stdio.h>
int main (void) {
int gold = 100;
gold = gold - 40;
gold = gold + 50;
gold = gold - 30;
printf("Gold: %d\n", gold);
return 0;
}
// Write a program to allow a user to enter a character
// Print out whether the character is an uppercase letter,
// a lowercase letter, or not a letter.
#include <stdio.h>
int main(void){
// Do we want to use " %c" or "%c"
char input;
printf("Enter a character: ");
scanf("%c", &input);
if (input >= 'a' && input <= 'z') {
printf("Lowercase\n");
} else if (input >= 'A' && input <= 'Z') {
printf("Uppercase\n");
} else {
printf("Not a letter\n");
}
return 0;
}
// Demonstration of nested if statements
// Written by: Angela Finlayson
// Date: 20/09/2026
// $ ./game_level
// Did you complete the level? (y/n): n
// Keep playing!
// $ ./game_level
// Did you complete the level? (y/n): y
// How many coins did you collect? 10
// Bonus: 3 stars!
// $ ./game_level
// Did you complete the level? (y/n): y
// How many coins did you collect? 9
// Bonus: 1 star!
#include <stdio.h>
#define MAX_COINS 10
int main(void) {
char input;
printf("Did you complete the level? (y/n) ");
scanf(" %c", &input);
if (input == 'y') {
printf("How many coins did you collect? ");
int gold;
scanf("%d", &gold);
if (gold >= MAX_COINS) {
printf("Bonus: 3 stars!\n");
} else {
printf("Bonus: 1 star!\n");
}
} else if (input == 'n'){
printf("Keep Playing!\n");
}
return 0;
}
// Write a program guessing_game.c that
// prompts the user enter an integer
// prints out higher, lower, correct if it is equal to the secret number (42)
// $ ./guessing_game
// Please enter an integer: 8
// Higher!
// $ ./guessing_game
// Please enter an integer: 42
// Correct!
#include <stdio.h>
#define SECRET_NUMBER 42
int main(void){
int guess;
printf("Please enter an integer: ");
int num_read = scanf("%d", &guess);
if (num_read != 1){
printf("Incorrect Usage: enter an integer\n");
} else if (guess == SECRET_NUMBER) {
printf("Correct!\n");
} else if (guess > SECRET_NUMBER) {
printf("Lower!\n");
} else {
printf("Higher\n");
}
printf("Game over\n");
return 0;
}
// Demonstration of simple if statements
// with simple conditions
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
int main(void) {
// Note the indentation
if (1) {
printf("Hooray\n");
}
if (0) {
printf("Yay!\n");
}
if (4 == 4) {
printf("I love C!\n");
}
if ('Z' == 'z') {
printf("I am cool!\n");
}
int x = 5;
int y = 10;
if (x < 0) {
printf("x is negative!\n");
}
if (y >= x) {
printf("y is greater or equal\n");
}
if (x != y) {
printf("x and y not equal!\n");
}
printf("I love if statements!\n");
return 0;
}
// Demonstration of nested if statements
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
int main(void) {
int x;
printf("Enter an integer: ");
scanf("%d", &x);
if (x > 0) {
if (x > 100) {
printf("big");
} else {
printf("small");
}
printf(" positive");
} else {
printf("Not a positive");
}
printf(" number\n");
return 0;
}
// Write a program odd_even.c that
// prompts the user enter an integer
// prints out whether the number is even or odd
//
// $ ./odd_even
// Please enter an integer: 8
// Even!
// ./odd_even
// Please enter an integer: 3
// Odd!
#include <stdio.h>
int main(void){
int value;
printf("Please enter an integer: ");
scanf("%d", &value);
if (value % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}
return 0;
}
// Demonstration of error checking using the return value from scanf
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
int main(void){
int x;
int y;
double z;
printf("Enter 2 integers and real number: ");
int inputs_read = scanf("%d %d %lf", &x, &y, &z);
printf("%d inputs were read in\n", inputs_read);
if (inputs_read == 3) {
printf("Inputs were: %d %d %lf\n", x, y, z);
} else {
printf("Incorrect usage: Enter 2 integers and real number\n");
}
return 0;
}
// Demonstration of chained else if statements
// with simple conditions
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
#define COLD 10
#define HOT 25
// What test cases should we use?
int main(void) {
int temperature;
printf("Please enter the temperature: ");
scanf("%d", &temperature);
if (temperature <= COLD) {
printf("I am cold!\n");
} else if (temperature < HOT) {
printf("Just right!\n");
} else {
printf("I am hot!\n");
}
printf("Have a nice day\n");
return 0;
}
// Demonstration of conditional while loops
// We do not know exactly how many times the loop will repeat
// There is no specific input value that stops it
// There is just a condition
// Written by: Angela Finlayson
// Date: 20/09/2026
#include <stdio.h>
#define MAX_KOMBUCHA 2000
int main(void) {
// 1. Initialise the loop control variable
int total_kombucha_ml = 0;
int kombucha_ml;
while (total_kombucha_ml < 2000) { // 2. Test the loop condition
printf("Please enter the ml of kombucha: ");
scanf("%d", &kombucha_ml);
// 3. Update loop control variable
total_kombucha_ml = total_kombucha_ml + kombucha_ml;
}
printf("Kombucha target reached %d!!\n", total_kombucha_ml);
return 0;
}
// Demonstration of conditional while loops using flags
//A flag variable keeps track of whether a condition has been met.
// In this example the flag variable is pins_match
// Written by: Angela Finlayson
// Date: 22/09/2026
#include <stdio.h>
#define MAX_KOMBUCHA 2000
int main(void) {
int pin;
int confirmation;
int pins_match = 0;
while (pins_match == 0) {
printf("Enter a PIN: ");
scanf("%d", &pin);
printf("Enter the PIN again: ");
scanf("%d", &confirmation);
if (pin == confirmation) {
pins_match = 1;
}
}
printf("PIN saved!\n");
return 0;
}
// Demonstration of counting while loops
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
#define MAX 5
//Try going backwards
int main(void) {
// 1. Initialise loop counter before the loop
int counter = MAX;
while (counter > 0) { // 2. check loop counter condition
printf("%d Here we go loop de loop!\n", counter);
counter = counter - 1; // 3. update loop counter
}
// What will this print?
printf("%d Yay the loop ended!!!\n", counter);
return 0;
}
// Demonstration of infinite while loops
// Typically you do not want an infinite loop!
// Type Ctrl+C to end program.
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
int main(void) {
// Change to a while loop
// while (1) {
// printf("I love my COMP1511/1911 lectures!\n");
// }
// Infinite loop
// How could we stop it?
int push_ups = 0;
while (push_ups < 20) {
printf("You have done %d push-ups!\n", push_ups);
push_ups = push_ups + 2;
}
printf("Congratulations you have done %d push-ups!\n", push_ups);
return 0;
}
// Write a loop to read in integers.
// It should end when the user enters non-integer data
//
// Written by: Angela Finlayson
// Date: 20/09/2026
#include <stdio.h>
int main(void) {
// Recap with scanf
// Lets do an if then turn it into a while
int value;
int n_read;
printf("Enter an integer: ");
n_read = scanf("%d", &value);
while (n_read == 1) {
printf("Yay you entered an int %d\n", value);
printf("Enter an integer: ");
n_read = scanf("%d", &value);
}
return 0;
}
// Write a loop to read in integers.
// It should end when the user enters non-integer data
//
// Written by: Angela Finlayson
// Date: 20/09/2026
#include <stdio.h>
int main(void) {
// Recap with scanf
// Lets do an if then turn it into a while
int value;
printf("Enter an integer: ");
while (scanf("%d", &value) == 1) {
printf("Yay you entered an int %d\n", value);
printf("Enter an integer: ");
}
return 0;
}
// Demonstration of sentinel while loops
// A sentinel value as a specific input value that terminates the loop
// Written by: Angela Finlayson
// Date: 22/09/2026
#include <stdio.h>
int main(void) {
char grade;
printf("Enter a grade (q to quit): ");
scanf(" %c", &grade);
// Stop when we reach the sentinel value 'q'
while (grade != 'q') {
printf("You entered %c\n", grade);
printf("Enter a grade (q to quit): ");
scanf(" %c", &grade);
}
return 0;
}
// Write code to print n * on one line
//
// Written by: Angela Finlayson
// Date: 22/09/2026
#include <stdio.h>
int main(void) {
int n;
printf("How many * would you like to print? ");
scanf("%d", &n);
int counter = 0;
while(counter < n) {
printf("*");
counter = counter + 1;
}
printf("\n");
return 0;
}
// Demonstration of nested while loops
//
// Written by: Angela Finlayson
// Date: 20/09/2026
//
// Print this pattern
//
// * * * * *
// * * * * *
// * * * * *
// * * * * *
// * * * * *
#include <stdio.h>
#define SIZE 3
int main(void) {
int row_counter = 0;
while(row_counter < SIZE) {
int counter = 0;
while(counter < SIZE) {
printf("* ");
counter = counter + 1;
}
printf("\n");
row_counter = row_counter + 1;
}
return 0;
}
//Trace with 3*3
// move inner variable declaration to top
// Demonstration of nested while loops
//
// Written by: Angela Finlayson
// Date: 20/09/2026
//
// Print this pattern
//
// 1 2 3 4 5
// 1 2 3 4 5
// 1 2 3 4 5
// 1 2 3 4 5
// 1 2 3 4 5
#include <stdio.h>
#define SIZE 5
int main(void) {
int row_counter = 1;
while(row_counter <= SIZE) {
int counter = 1;
while(counter <= SIZE) {
printf("%d ", counter);
counter = counter + 1;
}
printf("\n");
row_counter = row_counter + 1;
}
return 0;
}
//Trace
// move inner variable declaration to top
// Demonstration of nested while loops
//
// Written by: Angela Finlayson
// Date: 20/09/2025
//
// Print this pattern
//
// 1
// 1 2
// 1 2 3
// 1 2 3 4
// 1 2 3 4 5
#include <stdio.h>
#define SIZE 5
int main(void) {
int row_counter = 1;
while(row_counter <= SIZE) {
int counter = 1;
while(counter <= row_counter) {
printf("%d ", counter);
counter = counter + 1;
}
printf("\n");
row_counter = row_counter + 1;
}
return 0;
}
// Demonstration of enum
//
// Date: 22/09/2026
#include <stdio.h>
// Define an enum with days of the week
// make sure it is outside and before the main function
// MON will have value 0, TUE 1, WED 2, etc
enum weekdays {MON, TUE, WED, THU, FRI, SAT, SUN};
enum months {JAN, FEB, MAR}; //etc
int main(void) {
enum weekdays day = MON;
// This will print out 0
printf("The day number is %d\n", day);
if (day == MON) {
printf("MON\n");
} else if(day == TUE) {
printf("TUE\n");
} //
//day = FEB;
return 0;
}
// Demonstration of structs
//
// Written by: Angela Finlayson
// Date: 22/09/2026
//
#include <stdio.h>
struct point {
int x;
int y;
int z;
};
int main(void) {
// Declare 2 variables of
struct point p1;
struct point p2;
printf("Enter data for a point\n");
// print the point
scanf("%d %d %d", &p1.x, &p1.y, &p1.z);
printf("P1 (%d, %d, %d)\n", p1.x, p1.y, p1.z);
p2 = p1;
printf("P2 (%d, %d, %d)\n", p2.x, p2.y, p2.z);
return 0;
}
// Demonstration of structs
//
// Written by: Angela Finlayson
// Date: 22/09/2026
//
#include <stdio.h>
struct student {
char first_initial;
char last_initial;
int age;
double lab_mark;
};
int main(void) {
// Declare a variable
// of type struct student
struct student brianna;
// Assign values to
// struct members
brianna.first_initial = 'B';
brianna.last_initial = 'K';
brianna.age = 21;
brianna.lab_mark = 9.9;
//
printf("%c %c %d %lf\n", brianna.first_initial,
brianna.last_initial,
brianna.age,
brianna.lab_mark);
//Add 1 to the age
brianna.age = brianna.age + 1;
printf("%c %c %d %lf\n", brianna.first_initial,
brianna.last_initial,
brianna.age,
brianna.lab_mark);
// Print out the data
// Read in updated lab mark from the user
printf("please enter your new lab mark: ");
scanf("%lf", &brianna.lab_mark);
// Print out the data
printf("%c %c %d %lf\n", brianna.first_initial,
brianna.last_initial,
brianna.age,
brianna.lab_mark);
return 0;
}