Week 2 Code Examples
// Demonstration of enum
//
// Date: 18/09/2024
#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};
int main(void) {
enum weekdays day = MON;
// This will print out 5
printf("The day number is %d\n", day);
return 0;
}
// Demonstration of structs and enums together
// Lets define enum for pokemon type
// Lets define a struct for a pokemon
// Lets create a variable for a pokemon and give it some data
enum pokemon_type {FIRE, PSYCHIC, WATER, GRASS, GHOST, DRAGON};
struct pokemon {
enum pokemon_type type1;
enum pokemon_type type2;
double attack;
double defense;
};
int main(void) {
return 0;
}
// Demonstration of structs
//
// Written by: Angela Finlayson
// Date: 19/09/2024
//
#include <stdio.h>
struct coordinate {
int x;
int y;
int z;
};
int main(void) {
// Declare 2 variables of
// type struct coordinate
struct coordinate point_1;
struct coordinate point_2;
printf("Enter data for a point\n");
scanf("%d %d %d", &point_1.x, &point_1.y, &point_1.z);
printf("P1 (%d, %d, %d)\n",point_1.x, point_1.y, point_1.z);
point_2 = point_1;
printf("P2 (%d, %d, %d)\n",point_2.x, point_2.y, point_2.z);
point_2.x = 0;
printf("P1 (%d, %d, %d)\n",point_1.x, point_1.y, point_1.z);
printf("P2 (%d, %d, %d)\n",point_2.x, point_2.y, point_2.z);
//point_2.x = point_1.x + point_1.x;
// Can we use = or maths on these?
return 0;
}
// Demonstration of structs
//
// Written by: Angela Finlayson
// Date: 19/09/2024
//
#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;
// Initialise the members of
// your struct variable
brianna.first_initial = 'B';
brianna.last_initial = 'K';
brianna.age = 21;
brianna.lab_mark = 9.9;
brianna.age = brianna.age + 1;
// Print out the data
printf("%c %c %d %lf\n", brianna.first_initial,
brianna.last_initial,
brianna.age,
brianna.lab_mark);
// Create another student and read in data from 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;
}
// 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){
char character;
printf("Enter a character: ");
scanf("%c", &character);
if (character >= 'A' && character <= 'Z') {
printf("Uppercase\n");
} else if (character >= 'a' && character <= 'z') {
printf("Lowercase\n");
} else {
printf("Not a letter\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_inputs = scanf("%d", &guess);
if (num_inputs != 1) {
printf("Incorrect input....\n");
} else if (guess == SECRET_NUMBER) {
printf("Correct!\n");
} else if (guess > SECRET_NUMBER) {
printf("Lower!\n");
} else {
// guess < SECRET_NUMBER
printf("Higher\n");
}
printf("Game over\n");
return 0;
}
// Demonstration of simple if statements
// with simple conditions
//
// Written by: Angela Finlayson
// Date: 14/09/2024
#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");
}
return 0;
}
// Demonstration of simple if statements
// with simple conditions
//
// Written by: Angela Finlayson
// Date: 14/09/2024
#include <stdio.h>
int main(void) {
char c = 'g';
int x = 4;
// 1 is true
// 0 is false
printf("%d\n", (c >= 'a') && (c <= 'm'));
printf("%d\n", (x < 0) || (x > 10));
printf("%d\n", !((x < 0) || (x > 10)));
return 0;
}
// Demonstration of nested if statements
//
// Written by: Angela Finlayson
// Date: 14/09/2024
#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 n;
printf("Please enter an integer: ");
scanf("%d", &n);
if (n % 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: 14/09/2024
#include <stdio.h>
int main(void){
int x;
int y;
double z;
int inputs_read = scanf("%d %d %lf", &x, &y, &z);
printf("%d inputs\n", inputs_read);
if (inputs_read == 3) {
printf("%d %d %lf\n", x, y, z);
} else {
printf("Incorrect usage\n");
}
return 0;
}
// Demonstration of chained if statements
// with simple conditions
//
// Written by: Angela Finlayson
// Date: 14/09/2024
#include <stdio.h>
#define COLD 10
int main(void) {
int temperature;
printf("Please enter the temperature: ");
scanf("%d", &temperature);
if (temperature <= COLD) {
printf("I am cold!\n");
} else {
printf("I am not cold!\n");
}
printf("Have a nice day\n");
return 0;
}
// Demonstration of chained if statements
// with simple conditions
//
// Written by: Angela Finlayson
// Date: 14/09/2024
#include <stdio.h>
#define COLD 10
#define HOT 25
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;
}
//Write a program convert.c that
//prompts the user enter the number of hours
//calculates how many minutes that is equivalent to
//prints out the number of minutes
//See sample output below:
// $ ./convert
// Please enter the number of hours: 2.5
// That is 150.00 minutes
#include <stdio.h>
int main(void){
return 0;
}
// A program to revise the following concepts
// Tricky expressions using
// int, double, char
//
// Written by: Angela Finlayson
// Date: 14/09/2024
#include <stdio.h>
int main(void) {
int x;
int y;
printf("Enter 2 integers: ");
scanf("%d %d", &x, &y);
double average = (x + y) / 2.0;
printf("The average of %d and %d is %lf\n", x, y, average);
char letter_1 = 'B' - 'A' + 'a';
char letter_2 = 'e' - 'a' + 'A';
printf("%c %c\n", letter_1, letter_2);
int z = 5 / 0;
printf("%d\n", z);
return 0;
}
// A program to revise the following concepts
// Declaring, initialising, modifying and printing variables of types
// int, double, char
// Using a simple expression
//
// Written by: Angela Finlayson
// Date: 14/09/2024
#include <stdio.h>
int main(void) {
int number_of_weeks = 10;
double distance_in_cm = 95.5;
char grade = 'B';
printf("There are %d weeks\n", number_of_weeks);
printf("The distance is %lfcm\n", distance_in_cm);
printf("You got a %c!\n", grade);
distance_in_cm = distance_in_cm - 5;
printf("The distance is now %lfcm\n", distance_in_cm);
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: 14/09/2024
#include <stdio.h>
int main(void) {
int x;
int y;
printf("Enter 2 integers: ");
scanf("%d %d", &x, &y);
printf("You entered %d %d\n", x, y);
// double real;
// printf("Enter a real number: ");
// scanf("%lf", &real);
// printf("You entered %lf\n", real);
char c;
printf("Enter a character: ");
scanf(" %c", &c);
printf("You entered %c\n", c);
return 0;
}
// Demonstration of nested while loops
// Written by: Angela Finlayson
// Date: 19/09/2024
//
// Print time
//
// hh:mm:ss
// Note that %02d means to pad an integer with leading 0s to make sure it
// is at least 2 digits
#include <stdio.h>
#define MAX_HOURS 24
#define MAX_MINUTES 60
#define MAX_SECONDS 60
int main(void) {
int hours = 0;
while (hours < MAX_HOURS) {
int minutes = 0;
while (minutes < MAX_MINUTES) {
int seconds = 0;
while (seconds < MAX_SECONDS) {
printf("%02d:%02d:%02d\n", hours, minutes, seconds);
seconds = seconds + 1;
}
minutes = minutes + 1;
}
hours = hours + 1;
}
return 0;
}
// Demonstration of nested while loops
//
// Written by: Angela Finlayson
// Date: 19/09/2024
//
// 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
// Try with a size inputted by user too
int main(void) {
int row = 1;
while (row <= SIZE) {
int col = 1;
while (col <= SIZE) {
printf("%d ", col);
col = col + 1;
}
printf("\n");
row = row + 1;
}
return 0;
}
//Row 1:
// col 1 2 3 4 5 (6)
//Row 2:
// col (6)
// Row 3:
// col (6)
// Demonstration of nested while loops
//
// Written by: Angela Finlayson
// Date: 19/09/2024
//
// 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 = 1;
while (row <= SIZE) {
int col = 1;
while (col <= row) {
printf("%d ", col);
col = col + 1;
}
printf("\n");
row = row + 1;
}
}
// Demonstration of conditional while loops
//
// Written by: Angela Finlayson
// Date: 14/09/2024
#include <stdio.h>
#define MAX_KOMBUCHA 2000
int main(void) {
// 1. Initialise the loop control variable
int total_kombucha_ml = 0;
int kombucha_ml;
// 2. Test the loop condition
while (total_kombucha_ml <= MAX_KOMBUCHA) {
// Why are we putting this printf over 2 lines?
printf("You have drunk %d/%dml"
" of kombucha so far\n", total_kombucha_ml, MAX_KOMBUCHA);
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("Stop! That would bring you to %dml!!\n", total_kombucha_ml);
return 0;
}
// Demonstration of counting while loops
//
// Written by: Angela Finlayson
// Date: 14/09/2024
#include <stdio.h>
// Try with limit of 3
//
int main(void) {
// 1. Initialise loop counter before the loop
int counter = 0;
while (counter < 10) { // 2. check loop counter condition
printf("%d Here we go loop de loop!\n", counter);
counter = counter + 2; // 3. update loop counter
}
// What will this print?
printf("%d Yay the loop ended!!!\n", counter);
return 0;
}
// Demonstration of infinite while loops
//
// Written by: Angela Finlayson
// Date: 14/09/2024
#include <stdio.h>
int main(void) {
while (1) {
printf("I love my COMP1511 lectures!\n");
}
// int push_ups = 10;
// while (push_ups > 0) {
// printf("You have %d push-ups left!\n", push_ups);
// push_ups = push_ups - 1;
// }
// printf("Congratulations you did %d push-ups!\n", push_ups);
return 0;
}
// Modify this code to calculate the sum of all integers entered
// by the user
// The loop should end when the user enters non-integer data
//
// Written by: Angela Finlayson
// Date: 25/02/2025
#include <stdio.h>
int main(void) {
int number = 0;
int end_loop = 0; // 1. Initialise the loop control variable
int sum = 0;
int scanf_return;
while (end_loop == 0) { // 2. Test the loop condition
scanf_return = scanf("%d", &number);
if (scanf_return != 1) { // We want a negative value to end the loop
end_loop = 1; // 3. Update the loop control variable
} else {
printf("You entered %d\n", number);
sum = sum + number;
}
}
printf("Sum was %d\n", sum);
return 0;
}
// Demonstration of sentinel while loops
//
// Written by: Angela Finlayson
// Date: 14/09/2024
#include <stdio.h>
int main(void) {
int number = 0;
int end_loop = 0; // 1. Initialise the loop control variable
while (end_loop == 0) { // 2. Test the loop condition
scanf("%d", &number);
if (number < 0) { // We want a negative value to end the loop
end_loop = 1; // 3. Update the loop control variable
} else {
printf("You entered %d\n", number);
}
}
return 0;
}