Week 3 Code Examples

// Nested while loop Exercise
//
// Written by: Angela Finlayson
// Date: 27/09/2025
//
// Print this pattern
//
// 0
// 1
// 2
// 3
// 4
//
// 0 0 0 0 0
// 1 1 1 1 1
// 2 2 2 2 2
// 3 3 3 3 3
// 4 4 4 4 4
//
//Then modify to print this pattern
// 0 0 0 0 0
// 1   1   1
// 2   2   2
// 3   3   3
// 4 4 4 4 4



#include <stdio.h>

#define SIZE 5
// 0 1 2 3 4
// 0 0 0 0 0
// 1   1   1
// 2   2   2
// 3   3   3
// 4 4 4 4 4
 
int main(void) { 
    int row = 0;
    while (row < SIZE) {
        int col = 0;
        while (col < SIZE) {
            if (row == 0 || row == 4 || col % 2 == 0) {
                printf("%d ", row);
            } else {
                printf("  ");
            }
            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, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

int main(void) {
    enum weekdays day = WED;
    // What will this print out?
    printf("The day number is %d\n", day);

    if (day == MON) {
        printf("Monday\n");
    } else if (day == TUE) {
        printf("Tuesday\n");
    } else if (day == WED) {
        printf("Wednesday\n");
    } else if (day == THU) {
        printf("Thursday\n");
    } else if (day == FRI) {
        printf("Friday\n");
    } else if (day == SAT) {
        printf("Saturday\n");
    } else if (day == SUN) {
        printf("Sunday\n");
    } else {
        printf("Invalid Day\n");
    }
    
    return 0;
}
#include <stdio.h>

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

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

struct pokemon {
    int health_points;
    int level;
    double defense;
    enum pokemon_type primary_type;
    enum pokemon_type secondary_type;
};

int main(void) {
    struct pokemon jiggly_puff;
    jiggly_puff.health_points = 110;
    jiggly_puff.level = 1;
    jiggly_puff.defense = 70.5;
    jiggly_puff.primary_type = FAIRY;
    jiggly_puff.secondary_type = NORMAL;

    printf("Pokemon stats:\n");
    printf("HP: %d\n", jiggly_puff.health_points);
    printf("L: %d\n", jiggly_puff.level);
    printf("D: %lf\n", jiggly_puff.defense);
    printf("PT: ");
    if (jiggly_puff.primary_type == FIRE) {
        printf("Fire\n");
    } else if (jiggly_puff.primary_type == WATER) {
        printf("Water\n");
    } else if (jiggly_puff.primary_type == ICE) {
        printf("Ice\n");
    } else if (jiggly_puff.primary_type == GRASS) {
        printf("Grass\n");
    } else if (jiggly_puff.primary_type == FAIRY) {
        printf("Fairy\n");
    } else if (jiggly_puff.primary_type == DRAGON) {
        printf("Dragon\n");
    } else if (jiggly_puff.primary_type == FLYING) {
        printf("Flying\n");
    } else if(jiggly_puff.primary_type == GHOST) {
        printf("Ghost\n");
    } else if(jiggly_puff.primary_type == POISON) {
        printf("Poison\n");
    } else if(jiggly_puff.primary_type == NORMAL) {
        printf("Normal\n");
    } else {
        printf("Unknown type\n");
    }  

    printf("ST: ");
    if (jiggly_puff.secondary_type == FIRE) {
        printf("Fire\n");
    } else if (jiggly_puff.secondary_type == WATER) {
        printf("Water\n");
    } else if (jiggly_puff.secondary_type == ICE) {
        printf("Ice\n");
    } else if (jiggly_puff.secondary_type == GRASS) {
        printf("Grass\n");
    } else if (jiggly_puff.secondary_type == FAIRY) {
        printf("Fairy\n");
    } else if (jiggly_puff.secondary_type == DRAGON) {
        printf("Dragon\n");
    } else if (jiggly_puff.secondary_type == FLYING) {
        printf("Flying\n");
    } else if(jiggly_puff.secondary_type == GHOST) {
        printf("Ghost\n");
    } else if(jiggly_puff.secondary_type == POISON) {
        printf("Poison\n");
    } else if(jiggly_puff.secondary_type == NORMAL) {
        printf("Normal\n");
    } else {
        printf("Unknown type\n");
    }  


    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;
    // Initialise the members of your struct variable
    ryan.first_initial = 'R';
    ryan.last_initial = 'Z';
    ryan.lab_mark = 4.4;
    ryan.age = 30;
	 
    //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;
}
// Week 3 Monday Lecture
// Demonstration of writing and calling a simple function
// to calculate the area of a triangle. (1/2 * b *h)

#include <stdio.h>

//First step implement without function
//Convert to function

double triangle_area(double base, double height);

int main(void) {
    double b;
    double h;
    double a;
    
    printf("Please enter base and height: ");
    scanf("%lf %lf", &b, &h);
    a = triangle_area(b, h);
    printf("Area is %lf\n", a);

    return 0;    
}

double triangle_area(double base, double height) {
    double area = 0.5 * base * height;
    return area;
}



 
#include <stdio.h>


int maximum(int x, int y);

int main(void) {
    int num = 7;
    int max = maximum(10, num);
    
 	printf("The maximum value is %d\n", max);

    int n1, n2;
    printf("Enter 2 ints: ");
    scanf("%d %d", &n1, &n2);
    printf("The maximum of %d and %d is %d\n", n1, n2, maximum(n1, n2));

    return 0;
}

// This function returns the maximum of the given values x and y
int maximum(int x, int y) {
    int max;
    if (x > y) {
        max = x;
    } else {
        max = y;
    } 
    // returns an int value 
    return max;
}
// Week 3 Lecture
// Demonstration of memory, scope and lifetime of variables

#include <stdio.h>

// This is a global variable as it is not declared inside a function
// DO NOT USE these in COMP1511

//int num = 10;

double add_numbers (double x, double y);
 
int main(void) {
    int x = 9;
    double answer = add_numbers(1.5, x);
    printf("The answer is %lf\n", answer);

    return 0;
}

// This function returns the sum of 2 given doubles
double add_numbers (double x, double y) {
    double sum;
    sum = x + y;
    return sum;
}
// Week 3 Monday Lecture
// Demonstration of passing by value in C

#include <stdio.h>

void increment(int x);


int main(void) {
    int x = 10;
    increment(x);
    printf("Main: %d\n", x);
    return 0;
}

void increment(int x) {
    x = x + 1;
    printf("Inc: %d\n", x);
}
#include <stdio.h>

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

struct pokemon {
    int health_points;
    int level;
    double defense;
    enum pokemon_type primary_type;
    enum pokemon_type secondary_type;
};

void print_pokemon_type(enum pokemon_type type);
void print_pokemon(struct pokemon my_pokemon);

int main(void) {
    struct pokemon jiggly_puff;
    jiggly_puff.health_points = 110;
    jiggly_puff.level = 1;
    jiggly_puff.defense = 70.5;
    jiggly_puff.primary_type = FAIRY;
    jiggly_puff.secondary_type = NORMAL;

    print_pokemon(jiggly_puff);

    return 0;
}

void print_pokemon(struct pokemon my_pokemon){
    printf("Pokemon stats:\n");
    printf("HP: %d\n", my_pokemon.health_points);
    printf("L: %d\n", my_pokemon.level);
    printf("D: %lf\n", my_pokemon.defense);
    printf("PT: ");
    print_pokemon_type(my_pokemon.primary_type);
    printf("ST: ");
    print_pokemon_type(my_pokemon.secondary_type);
}

void print_pokemon_type(enum pokemon_type type) {
    if (type == FIRE) {
        printf("Fire\n");
    } else if (type == WATER) {
        printf("Water\n");
    } else if (type == ICE) {
        printf("Ice\n");
    } else if (type == GRASS) {
        printf("Grass\n");
    } else if (type == FAIRY) {
        printf("Fairy\n");
    } else if (type == DRAGON) {
        printf("Dragon\n");
    } else if (type == FLYING) {
        printf("Flying\n");
    } else if(type == GHOST) {
        printf("Ghost\n");
    } else if(type == POISON) {
        printf("Poison\n");
    } else if(type == NORMAL) {
        printf("Normal\n");
    } else {
        printf("Unknown type\n");
    }  
}
// Week 3 Lecture
// Demonstration of using a function return value in a condition!
// 

#include <stdio.h>

void scanf_loop_example_1(void);
void scanf_loop_example_2(void);
void scanf_loop_example_3(void);

int main(void) {
    printf("Please enter integers: ");
    scanf_loop_example_1();
    return 0;
}

// This function uses the return value from scanf 
// in the while loop condition
void scanf_loop_example_1(void) {
    int n;
    
    while (scanf("%d", &n) == 1) {
        printf("I just read an int %d\n", n);
    }

    printf("That was not an int!!!\n");
}

// This is an alternative way
void scanf_loop_example_2(void) {
    int n;
    int scanf_return;

    scanf_return = scanf("%d", &n);

    while (scanf_return == 1) {
        printf("I just read an int %d\n", n);
        scanf_return = scanf("%d", &n);
    }

    printf("That was not an int!!!\n");
}

// This is similar to the sentinel loop example
// from lectures last week
void scanf_loop_example_3(void) {
    int n;
    int scanf_return;
    int end_loop = 0;

    while (end_loop == 0) {     
        scanf_return = scanf("%d", &n);
        if (scanf_return == 1) {
            printf("I just read an int %d\n", n);
        } else {
            end_loop = 1;
        }
    }

    printf("That was not an int!!!\n");
}
#include <stdio.h>

void print_square(int size);

int main(void) {
    int square_size;
    printf("Enter the size of the square: ");
    scanf("%d", &square_size);
    print_square(square_size);
    return 0;
}

// Given a size, print a square of '*'
void print_square(int size) {
    int row = 0;
    while (row < size) {
        int col = 0;
        while (col < size) {
            printf("#");
            col = col + 1;
        }
        printf("\n");
        row = row + 1;
    }
}
#include <stdio.h>

void print_warning(void);

int main(void) {
    print_warning();
    return 0;
}

// Display a helpful warning on the terminal
void print_warning(void) {
    printf("#########################\n");
    printf("Warning: Don't plagiarise\n");
    printf("#########################\n");
}
// This is an example of very bad style... 
// Let's see if we can clean it right up. 
// How is everyone feeling, dizzy yet?

// /* 1. What do you think this program does?
// 2. Can you see any mistakes?
// 3. How do we fix *this*? */

#include <stdio.h>

#define BUDGET 10
#define O_S 1.25

struct ice_cream { 
    double price; 
    int scoop; 
    char flavour;
};

enum flavours {DULCE,VANILLA,CHOC,mint, 
PISTACHIO};

int main(void) {
    struct ice_cream bill;
    int total = 0;
    char loop = 'y';
        int scanf_return; 
        // I am assigning to the bill structure - the price member gets the value 1.25
    bill.price = O_S;         
    while (loop == 'y') { // ?!
    printf("Starting .........\n"); // Printing out a statement
    printf("What flavour do you want and how many scoops of that flavour: "); // print out another statement
    scanf_return = scanf(" %c %d", &bill.flavour, &bill.scoop);
    if (scanf_return != 2) printf("Error, you did not put in flavour and scoops. Did you now? \n");
    return 1;
    total = total + (bill.price * bill.scoop); 
    if (total > BUDGET) printf("You do not have enough money to buy this much ice-cream.\n"); else printf("Yay! You have enough to get some ice-cream\n");    
    printf("Would you like to try ordering more ice-cream (y/n)? "); 
    scanf(" %c", &loop);
    }
    return 0;
}
// This is an example of very bad style... 
// Let's see if we can clean it right up. 
// How is everyone feeling, dizzy yet?

// /* 1. What do you think this program does?
// 2. Can you see any mistakes?
// 3. How do we fix *this*? */

#include <stdio.h>
#define Budget 10
#define O_S 1.25
struct ice_cream { double price; int scoop; char flavour;};
enum flavours {DULCE,VANILLA,CHOC,mint, 
PISTACHIO};
int main() {
struct ice_cream bill;
int total = 0;
char loop = 'y';
    int scanf_return; 
    // I am assigning to the bill structure - the price member gets the value 1.25
 bill.price = O_S;         
while (loop == 'y') { // ?!
printf("Starting .........\n"); // Printing out a statement
printf("What flavour do you want and how many scoops of that flavour: "); // print out another statement
scanf_return = scanf(" %c %d", &bill.flavour, &bill.scoop);
if (scanf_return != 2) printf("Error, you did not put in flavour and scoops. Did you now? \n");
return 1;
total = total + (bill.price * bill.scoop); 
if (total > Budget) printf("You do not have enough money to buy this much ice-cream.\n"); else printf("Yay! You have enough to get some ice-cream\n");    
printf("Would you like to try ordering more ice-cream (y/n)? "); 
scanf(" %c", &loop);
 }
return 0;
}
// Week 3 Lecture 2 example
// Arrays of integers

// Create an array of ints
// Scan in data
// Print them out
// Find the sum
// Add 10 to all elements
// Print them out

#include <stdio.h>

#define SIZE_NUMBERS 6

int main(void) {
    int numbers[SIZE_NUMBERS];

    printf("Enter 6 integers: ");
    //scan numbers into the array
    int i = 0;
    while (i < SIZE_NUMBERS) {
        scanf("%d", &numbers[i]);
        i++;
    }

    // Print the array 
    i = 0;
    while (i < SIZE_NUMBERS) {
        printf("%d ", numbers[i]);
        i++;
    }
    printf("\n");

    // Sum the elements
    int sum = 0;
    i = 0;
    while (i < SIZE_NUMBERS) {
        sum = sum + numbers[i];
        i++;
    }
    printf("sum: %d\n", sum);

    // Increment all values by 10
    i = 0;
    while (i < SIZE_NUMBERS) {
       numbers[i] = numbers[i] + 10;
       i++;
    }
   
    // Print every second value in array 
    i = 0;
    while (i < SIZE_NUMBERS) {
        printf("%d ", numbers[i]);
        i = i + 2;
    }
    printf("\n");
     

    return 0;
}
// Week 3  Lecture
// Demonstration of arrays
// 
#include <stdio.h>

#define DAYS_IN_WEEK 7

int main(void) {                         
    int chocolate_eaten[DAYS_IN_WEEK] = {4, 2, 5, 2, 0, 3, 1};
    // How do we print the element at index 1 and what will that print?
    printf("%d\n", chocolate_eaten[1]);

    // What if we print element at index 7?
    //printf("%d\n", chocolate_eaten[7]);
    

    // How can I get the sum of the first and last element
    int sum = chocolate_eaten[0] + chocolate_eaten[DAYS_IN_WEEK-1];
    
    printf("Sum of first and last is %d\n", sum);
     
    // How can I modify the middle element and make it 99?
    chocolate_eaten[DAYS_IN_WEEK/2] = 99;
    printf("Middle value %d\n", chocolate_eaten[DAYS_IN_WEEK/2]);
     
   
    // How can I print out all data? (check bounds) 
    int i = 0;
    while (i < DAYS_IN_WEEK) {
        printf("%d ", chocolate_eaten[i]);
        i++;
    }
    printf("\n");
    return 0;
}