Week 2 Code Examples

//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: 20/09/2025

#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);

   return 0;
}
// A program to revise the following concepts
// Tricky expressions using
// int, double, char
//
// Written by: Angela Finlayson
// Date: 20/09/2025

#include <stdio.h>

int main(void) {    
   // int x;
   // printf("Enter an integer: ");
   // scanf("%d", &x);
   // int remainder = x % 3;
   // printf("Remainder when we divide %d by 3 is %d\n", x, remainder);
   
   // int y;
   // printf("Enter an integer to divide by: ");
   // scanf("%d", &y);
   // int z = x / y;
   // printf("%d\n", z);

   char letter_1 = 'B' - 'A' + 'a'; //
   char letter_2 = 'e' - 'a' + 'A'; //
   printf("%c %c\n", letter_1, letter_2);

   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: 20/09/2025

#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; 
    // How could we make it print to 1 decimal place?
    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: 20/09/2025

#include <stdio.h>

// In class
// Show error input - see how to handle this later today!!

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 grade;
    printf("Enter your grade: ");
    scanf(" %c", &grade);
    printf("Your grade was %c\n", grade);

    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("lowercase\n");
    } else if(character >= 'A' && character <= 'Z') {
        printf("uppercase\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 n_read = scanf("%d", &guess);

    if (n_read != 1) {
        printf("Incorrect Usage\n");
    } else if (guess == SECRET_NUMBER) {
        printf("Correct\n");
    } else if (guess < SECRET_NUMBER) {
        printf("Higher\n");
    } else {
        printf("Lower\n");
    }
     

    printf("Game over\n");
    return 0;
}
// Demonstration of simple if statements
// with simple conditions
// 
// Written by: Angela Finlayson
// Date: 20/09/2025

#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 nested if statements
// 
// Written by: Angela Finlayson
// Date: 20/09/2025

#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: 20/09/2025

#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);
    //printf("%d %d %lf\n", x, y, z);

     if (inputs_read == 3) {
          printf("%d %d %lf\n", x, y, z);
     } else {
          printf("Incorrect usage: Enter 2 ints and a double\n");
     }
    return 0;
}
// Demonstration of chained if statements
// with simple conditions
// 
// Written by: Angela Finlayson
// Date: 20/09/2025

#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 counting while loops
//
// Written by: Angela Finlayson
// Date: 20/09/2025

#include <stdio.h>

//Trace with 3
//Try going backwards
int main(void) {    
    // 1. Initialise loop counter before the loop
    int counter = 5;
    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: 20/09/2025

#include <stdio.h>

int main(void) {   
    // Change to a while loop 
    while (1) {
        printf("I love my COMP1511/1911 lectures!\n");
    }


    // We have fixed this so it is no longer an infinite loop
    // int push_ups = 0;
    // while (push_ups < 10) {
    //     printf("You have done %d push-ups!\n", push_ups);
    //     push_ups = push_ups + 1; //Added this line
    // }
    // printf("Congratulations you have done %d push-ups!\n", push_ups);

    return 0;
}
// Demonstration of nested while loops
// Written by: Angela Finlayson
// Date: 20/09/2025
//
// 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 3
#define MAX_MINUTES 5
#define MAX_SECONDS 60

//We want
// 00:00
// 00:01
// 00:02
// 00:03
// 00:04
// ...
// 00:58
// 00:59
// 01:00
// 01:01
// 01:02

// Try sequentially first
int main(void) { 

    int hour = 0;
    while (hour < MAX_HOURS) {      

        int minute = 0;
        while (minute < MAX_MINUTES) {      
            printf("%02d:%02d\n", hour, minute);
            minute = minute + 1;
        }

        hour = hour + 1;
    }
    return 0;
}

// Hour 0
//      minutes 0  1  2 3 4 5****
// Hour 1
//      minutes 0 1 2 3 4
// Demonstration of nested while loops
//
// Written by: Angela Finlayson
// Date: 20/09/2025
//
// 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 = 1;
    while (row <= SIZE) {
        int col = 1;
        while (col <= SIZE) {
            printf("%d ", col);
            col = col + 1;
        }
        printf("\n");
        row = row + 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 = 1;
    while (row <= SIZE) {
        int col = 1;
        while (col <= row) {
            printf("%d ", col);
            col = col + 1;
        }
        printf("\n");
        row = row + 1;
    }
    return 0;
}
// Demonstration of enum
//
// Date: 22/09/2025

#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};



int main(void) {
    enum weekdays day = MON;
    // This will print out 0
    printf("The day number is %d\n", day);

    day = JAN;
      

    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

#include <stdio.h>

enum pokemon_type {FIRE, WATER, ICE, GRASS, FAIRY, DRAGON, FLYING, GHOST, POISON};

int main(void) {
    enum pokemon_type player1_type = POISON;
    enum pokemon_type player2_type = FAIRY;

    printf("%d\n", player1_type);
    printf("%d\n", player2_type);

    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("(%d, %d, %d)\n", point_1.x, point_1.y, point_1.z);
    
    // print the point
    point_2 = point_1;
    printf("P2(%d, %d, %d)\n", point_2.x, point_2.y, point_2.z);

    return 0;
}
// Demonstration of structs
//
// Written by: Angela Finlayson
// Date: 22/09/2025
//


#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 ryan;
    ryan.first_initial = 'R';
    ryan.last_initial = 'Z';
    ryan.lab_mark = 4.4;
    ryan.age = 30;
	 
	// Initialise the members of
	// your struct variable
    

    //Add 1 to the age
    ryan.age = ryan.age + 1;


    // Print out the data
    printf("%c %c %d %lf\n", ryan.first_initial, 
                             ryan.last_initial, 
                             ryan.age, 
                             ryan.lab_mark);
   
    // Read in updated lab mark from the user
    printf("please enter your new lab mark: ");
    scanf("%lf", &ryan.lab_mark); 

    // Print out the data
    printf("%c %c %d %lf\n", ryan.first_initial, 
                             ryan.last_initial, 
                             ryan.age, 
                             ryan.lab_mark);
   

    return 0;
}