Week 3 Code Examples
// Nested while loop Exercise
//
// Written by: Angela Finlayson
// Date: 27/09/2026
//
// Print this pattern
//
// 0
// 1
// 2
// 3
// 4
//
// Then modify to print this pattern
//
// 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 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 == (SIZE -1) || (col % 2) == 0) {
printf("%d ", row);
} else {
printf(" ");
}
col = col + 1;
}
printf("\n");
row = row + 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, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; //etc
int main(void) {
enum weekdays day = FRI;
// What will this print out?
printf("The day number is %d\n", day);
// Can we do this?
// day = FEB;
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");
}
//What if we have another day variable and want to print it out?
return 0;
}
#include <stdio.h>
enum pokemon_type {
FIRE,
FAIRY,
NORMAL,
DRAGON,
STEEL,
BUG,
GROUND,
ROCK,
WATER,
NONE
};
struct pokemon {
int hit_points;
double attack;
double special_attack;
double defense;
double special_defense;
double speed;
enum pokemon_type primary_type;
enum pokemon_type secondary_type;
};
int main(void) {
struct pokemon groudon;
groudon.hit_points = 310;
groudon.attack = 274;
groudon.special_attack = 100;
groudon.defense = 256;
groudon.special_defense = 166;
groudon.speed = 166;
groudon.primary_type = GROUND;
groudon.secondary_type = FIRE;
printf("%d %lf %lf\n", groudon.hit_points,
groudon.attack,
groudon.special_attack);
printf("PT: ");
if (groudon.primary_type == FIRE){
printf("Fire");
} else if(groudon.primary_type == GROUND) {
printf("Ground");
}
printf("ST: ");
if (groudon.secondary_type == FIRE){
printf("Fire");
} else if(groudon.secondary_type == GROUND) {
printf("Ground");
}
return 0;
}
#include <stdio.h>
enum pokemon_type {
FIRE,
WATER,
ICE,
GRASS,
FAIRY,
DRAGON,
FLYING,
GHOST,
POISON,
NORMAL
};
struct pokemon {
int hit_points;
double attack;
double defense;
enum pokemon_type primary_type;
enum pokemon_type secondary_type;
};
int main(void) {
struct pokemon jiggly_puff;
jiggly_puff.hit_points = 110;
jiggly_puff.attack = 98.5;
jiggly_puff.defense = 78;
jiggly_puff.primary_type = FAIRY;
jiggly_puff.secondary_type = NORMAL;
printf("Pokemon stats:\n");
printf("Hit Points: %d\n", jiggly_puff.hit_points);
printf("Attack: %lf\n", jiggly_puff.attack);
printf("Defense: %lf\n", jiggly_puff.defense);
printf("Pokemon Type: ");
if (jiggly_puff.primary_type == FIRE) {
printf("Fire");
} else if (jiggly_puff.primary_type == WATER) {
printf("Water");
} else if (jiggly_puff.primary_type == ICE) {
printf("Ice");
} else if (jiggly_puff.primary_type == GRASS) {
printf("Grass");
} else if (jiggly_puff.primary_type == FAIRY) {
printf("Fairy");
} else if (jiggly_puff.primary_type == DRAGON) {
printf("Dragon");
} else if (jiggly_puff.primary_type == FLYING) {
printf("Flying");
} else if(jiggly_puff.primary_type == GHOST) {
printf("Ghost");
} else if(jiggly_puff.primary_type == POISON) {
printf("Poison");
} else if(jiggly_puff.primary_type == NORMAL) {
printf("Normal");
} else {
printf("Unknown type");
}
printf("\n");
printf("Secondary Type: ");
if (jiggly_puff.secondary_type == FIRE) {
printf("Fire");
} else if (jiggly_puff.secondary_type == WATER) {
printf("Water");
} else if (jiggly_puff.secondary_type == ICE) {
printf("Ice");
} else if (jiggly_puff.secondary_type == GRASS) {
printf("Grass");
} else if (jiggly_puff.secondary_type == FAIRY) {
printf("Fairy");
} else if (jiggly_puff.secondary_type == DRAGON) {
printf("Dragon");
} else if (jiggly_puff.secondary_type == FLYING) {
printf("Flying");
} else if(jiggly_puff.secondary_type == GHOST) {
printf("Ghost");
} else if(jiggly_puff.secondary_type == POISON) {
printf("Poison");
} else if(jiggly_puff.secondary_type == NORMAL) {
printf("Normal");
} else {
printf("Unknown type");
}
printf("\n");
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;
}
// An example of a program using a simple function.
#include <stdio.h>
double add_numbers (double x, double y);
// Show if statment
int main(void) {
int num = 9;
double answer = add_numbers(1.5, num);
printf("The answer is %lf\n", answer);
if (add_numbers(3,99) > 100) {
printf("Greater\n");
}
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;
}
//
// Cat Cafe
//
// A simple program demonstrating different kinds of functions.
// The program welcomes customers to the Cat Cafe, works out how many
// complimentary cat treats are available, prints cat sounds, and
// calculates the total cost of a visit.
//
// Date: 27/09/2026
// Written by Angela Finlayson
#include <stdio.h>
#define TREATS_PER_CAT 2
#define COST_PER_CAT_PER_HOUR 8.50
#define CLEANING_COST 12.00
void welcome(void);
int total_cat_treats(int cats);
double calculate_total_cost(int cats, double hours);
void print_cat_sound(char sound);
int scan_cats(void);
int main(void) {
welcome();
int num_cats = scan_cats();
int treats = total_cat_treats(num_cats);
printf("Enjoy %d complimentary cat treats!\n",treats);
print_cat_sound('p');
double cost = calculate_total_cost(num_cats, 1.5);
printf("The cost is $%.2lf\n", cost);
print_cat_sound('m');
return 0;
}
int scan_cats(void) {
int num_cats;
printf("How many cats are joining us today? ");
scanf("%d", &num_cats);
return num_cats;
}
// Prints a welcome message for the Cat Cafe to the terminal.
void welcome(void) {
printf("================================\n");
printf(" /\\_/\\\\\n");
printf(" (=^.^=)\n");
printf(" (\")_(\") c[_]\n");
printf("\n");
printf(" Welcome to the Cat Cafe!\n");
printf("================================\n\n");
}
// Calculates and returns the total number of complimentary cat treats
// for the given number of cats.
int total_cat_treats(int cats) {
int total = cats * TREATS_PER_CAT;
return total;
}
// Calculates and returns the total cost etc
double calculate_total_cost(int cats, double hours) {
double total = cats * hours * COST_PER_CAT_PER_HOUR + CLEANING_COST;
return total;
}
// Prints the cat sound represented by the given character to the terminal.
void print_cat_sound(char sound) {
if (sound == 'm') {
printf("Meow!\n");
} else if (sound == 'p') {
printf("Purr...\n");
} else if (sound == 'h') {
printf("Hiss!\n");
} else {
printf("The cat stares silently.\n");
}
}
//
// Cat Cafe
//
// A simple program demonstrating different kinds of functions.
// The program welcomes customers to the Cat Cafe, works out how many
// complimentary cat treats are available, prints cat sounds, and
// calculates the total cost of a visit.
//
// Date: 27/09/2026
// Written by Angela Finlayson
//
#include <stdio.h>
#define TREATS_PER_CAT 2
#define COST_PER_CAT_PER_HOUR 8.50
#define CLEANING_COST 12.00
void welcome(void);
int get_number_of_cats(void);
int total_cat_treats(int cats);
void print_cat_sound(char sound);
double calculate_total_cost(int cats, double hours);
int main(void) {
welcome();
int cats = get_number_of_cats();
int treats = total_cat_treats(cats);
printf("Enjoy %d complimentary cat treats!\n", treats);
print_cat_sound('p');
double hours;
printf("\nHow many hours will you spend at the cafe? ");
scanf("%lf", &hours);
double total_cost = calculate_total_cost(cats, hours);
printf("Your total cost is $%.2lf.\n", total_cost);
print_cat_sound('m');
return 0;
}
// Prints a welcome message for the Cat Cafe to the terminal.
void welcome(void) {
printf("================================\n");
printf(" /\\_/\\\\\n");
printf(" (=^.^=)\n");
printf(" (\")_(\") c[_]\n");
printf("\n");
printf(" Welcome to the Cat Cafe!\n");
printf("================================\n\n");
}
// Reads the number of cats visiting the cafe from the terminal
// and returns this number.
int get_number_of_cats(void) {
int cats;
printf("How many cats are visiting the cafe today? ");
scanf("%d", &cats);
return cats;
}
// Calculates and returns the total number of complimentary cat treats
// for the given number of cats.
int total_cat_treats(int cats) {
int total = cats * TREATS_PER_CAT;
return total;
}
// Prints the cat sound represented by the given character to the terminal.
void print_cat_sound(char sound) {
if (sound == 'm') {
printf("Meow!\n");
} else if (sound == 'p') {
printf("Purr...\n");
} else if (sound == 'h') {
printf("Hiss!\n");
} else {
printf("The cat stares silently.\n");
}
}
// Calculates and returns the total cost for the given number of cats
// and the given number of hours.
double calculate_total_cost(int cats, double hours) {
double cat_cost = cats * hours * COST_PER_CAT_PER_HOUR;
double total_cost = cat_cost + CLEANING_COST;
return total_cost;
}
//
// Cat Cafe
//
// A simple program demonstrating different kinds of functions.
// The program welcomes customers to the Cat Cafe, works out how many
// complimentary cat treats are available, prints cat sounds, and
// calculates the total cost of a visit.
//
// Date: 27/09/2026
// Written by Angela Finlayson
#include <stdio.h>
void print_cat_sound(char sound);
int main(void) {
print_cat_sound('p');
print_cat_sound('m');
return 0;
}
// Prints the cat sound represented by the given character to the terminal.
void print_cat_sound(char sound) {
if (sound == 'm') {
printf("Meow!\n");
} else if (sound == 'p') {
printf("Purr...\n");
} else if (sound == 'h') {
printf("Hiss!\n");
} else {
printf("The cat stares silently.\n");
}
}
//
// Cat Cafe
//
// A simple program demonstrating different kinds of functions.
// The program welcomes customers to the Cat Cafe, works out how many
// complimentary cat treats are available, prints cat sounds, and
// calculates the total cost of a visit.
//
// Date: 27/09/2026
// Written by Angela Finlayson
#include <stdio.h>
void welcome(void);
int main(void) {
welcome();
return 0;
}
// Prints a welcome message for the Cat Cafe to the terminal.
void welcome(void) {
printf("================================\n");
printf(" /\\_/\\\\\n");
printf(" (=^.^=)\n");
printf(" (\")_(\") c[_]\n");
printf("\n");
printf(" Welcome to the Cat Cafe!\n");
printf("================================\n\n");
}
// An example of a program
// to discuss memory and scope
#include <stdio.h>
// Global variable do not use in COMP1511/1911
int num = 10;
double add_numbers (double x, double y);
// Show if statment
int main(void) {
int x = 9;
printf("num %d\n", num);
double answer = add_numbers(1.5, x);
printf("The answer is %lf\n", answer);
printf("num %d\n", num);
//printf("%d\n", sum);
return 0;
}
// This function returns the sum of 2 given doubles
double add_numbers (double x, double y) {
double sum;
sum = x + y;
num = -100;
return sum;
}
// Week 3 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("Increment: %d\n", x);
}
#include <stdio.h>
enum pokemon_type {
FIRE,
FAIRY,
NORMAL,
DRAGON,
STEEL,
BUG,
GROUND,
ROCK,
WATER,
NONE
};
struct pokemon {
int hit_points;
double attack;
double special_attack;
double defense;
double special_defense;
double speed;
enum pokemon_type primary_type;
enum pokemon_type secondary_type;
};
void print_pokemon(struct pokemon pokemon);
void print_pokemon_type(enum pokemon_type type);
int main(void) {
struct pokemon groudon;
groudon.hit_points = 310;
groudon.attack = 274;
groudon.special_attack = 100;
groudon.defense = 256;
groudon.special_defense = 166;
groudon.speed = 166;
groudon.primary_type = GROUND;
groudon.secondary_type = FIRE;
printf("=== Stats Groudon ===\n");
print_pokemon(groudon);
return 0;
}
void print_pokemon(struct pokemon pokemon){
printf("Hit points: %d\n", pokemon.hit_points);
printf("Attack: %.2lf Special attack: %.2lf\n", pokemon.attack,
pokemon.special_attack);
printf("Defense: %.2lf Special Defense: %.2lf\n", pokemon.defense,
pokemon.special_defense);
printf("Speed: %.2lf\n", pokemon.speed);
printf("Primary Type: ");
print_pokemon_type(pokemon.primary_type);
printf("\nSecondary Type: ");
print_pokemon_type(pokemon.secondary_type);
printf("\n");
}
void print_pokemon_type(enum pokemon_type type) {
if (type == FIRE){
printf("Fire");
} else if(type == FAIRY) {
printf("Fairy");
} else if (type == NORMAL) {
printf("Normal");
} else if (type == DRAGON) {
printf("Dragon");
} else if (type == STEEL) {
printf("Steel");
} else if (type == BUG) {
printf("Bug");
} else if (type == GROUND) {
printf("Ground");
} else if (type == ROCK) {
printf("Rock");
} else if (type == WATER) {
printf("Water");
} else if (type == NONE) {
printf("None");
}
}
#include <stdio.h>
enum pokemon_type {
FIRE,
FAIRY,
NORMAL,
DRAGON,
STEEL,
BUG,
GROUND,
ROCK,
WATER,
NONE
};
struct pokemon {
int hit_points;
double attack;
double special_attack;
double defense;
double special_defense;
double speed;
enum pokemon_type primary_type;
enum pokemon_type secondary_type;
};
int main(void) {
struct pokemon groudon;
groudon.hit_points = 310;
groudon.attack = 274;
groudon.special_attack = 100;
groudon.defense = 256;
groudon.special_defense = 166;
groudon.speed = 166;
groudon.primary_type = GROUND;
groudon.secondary_type = FIRE;
printf("=== Stats Groudon ===\n");
printf("Hit points: %d\n", groudon.hit_points);
printf("Attack: %.2lf Special attack: %.2lf\n", groudon.attack,
groudon.special_attack);
printf("Defense: %.2lf Special Defense: %.2lf\n", groudon.defense,
groudon.special_defense);
printf("Speed: %.2lf\n", groudon.speed);
printf("Primary Type: ");
if (groudon.primary_type == FIRE){
printf("Fire");
} else if(groudon.primary_type == FAIRY) {
printf("Fairy");
} else if (groudon.primary_type == NORMAL) {
printf("Normal");
} else if (groudon.primary_type == DRAGON) {
printf("Dragon");
} else if (groudon.primary_type == STEEL) {
printf("Steel");
} else if (groudon.primary_type == BUG) {
printf("Bug");
} else if (groudon.primary_type == GROUND) {
printf("Ground");
} else if (groudon.primary_type == ROCK) {
printf("Rock");
} else if (groudon.primary_type == WATER) {
printf("Water");
} else if (groudon.primary_type == NONE) {
printf("None");
}
printf("\nSecondary Type: ");
if (groudon.secondary_type == FIRE){
printf("Fire");
} else if(groudon.secondary_type == FAIRY) {
printf("Fairy");
} else if (groudon.secondary_type == NORMAL) {
printf("Normal");
} else if (groudon.secondary_type == DRAGON) {
printf("Dragon");
} else if (groudon.secondary_type == STEEL) {
printf("Steel");
} else if (groudon.secondary_type == BUG) {
printf("Bug");
} else if (groudon.secondary_type == GROUND) {
printf("Ground");
} else if (groudon.secondary_type == ROCK) {
printf("Rock");
} else if (groudon.secondary_type == WATER) {
printf("Water");
} else if (groudon.secondary_type == NONE) {
printf("None");
}
printf("\n");
return 0;
}
// Week 3 Lecture
// Demonstration of using a function return value in a condition!
// We use the scanf return value to check whether we got an integer
// and stop when we get non-numeric input
#include <stdio.h>
int main(void) {
printf("Please enter integers: ");
int n;
while (scanf("%d", &n) == 1) {
printf("I just read an int %d\n", n);
}
printf("That was not an int!!!\n");
return 0;
}
//
// Simple demo of variable scope
//
// Date: 27/09/2026
// Written by Angela Finlayson
#include <stdio.h>
void some_function(void);
int main(void) {
int x = 10;
printf("main %d\n", x);
some_function();
return 0;
}
// Prints the cat sound represented by the given character to the terminal.
void some_function(void) {
int x = 100;
printf("some function %d\n", x);
}
// 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;
}
#include <stdio.h>
int main(void) {
int a=0;
int n2=0;
int darth_vader;
while(a<5){ printf("Duration: ");
scanf("%d",&darth_vader);
if(darth_vader>=600){
printf("Extended\n");
}else if(darth_vader>=300){
printf("Long\n");
}
n2=n2+darth_vader;
a=a+1;
} if(n2>=1200){ printf("Long playlist\n");
}
printf("Total duration: %d\n",n2);
return 0;
}
// This program reads the duration of several songs and
// calculates the total duration.
// It also counts the number of long songs and identifies extended songs.
// Written by Angela Finlayson
// Date: 27/09/2026
#include <stdio.h>
#include <stdio.h>
#define NUM_SONGS 5
#define LONG_SONG 300
#define EXTENDED_SONG 600
#define LONG_PLAYLIST 1200
int main(void) {
int song = 0;
int total_duration = 0;
while (song < NUM_SONGS) {
int duration;
printf("Duration: ");
scanf("%d", &duration);
if (duration >= EXTENDED_SONG) {
printf("Extended\n");
} else if (duration >= LONG_SONG) {
printf("Long\n");
}
total_duration = total_duration + duration;
song = song + 1;
}
if (total_duration >= LONG_PLAYLIST) {
printf("Long playlist\n");
}
printf("Total duration: %d\n", total_duration);
return 0;
}
// This program reads the duration of several songs and
// calculates the total duration.
// It also counts the number of long songs and identifies extended songs.
// Written by Angela Finlayson
// Date: 27/09/2026
#include <stdio.h>
#define NUM_SONGS 5
#define LONG_SONG 300
#define EXTENDED_SONG 600
#define LONG_PLAYLIST_COUNT 3
#include <stdio.h>
#define NUM_SONGS 5
#define LONG_SONG 300
#define EXTENDED_SONG 600
#define LONG_PLAYLIST 1200
int process_playlist(void);
int get_song_duration(void);
void print_song_type(int duration);
void print_playlist_summary(int total_duration);
int main(void) {
int total_duration = process_playlist();
print_playlist_summary(total_duration);
return 0;
}
// Processes all songs in the playlist and returns the total duration.
// Prompts the user to enter each song duration and prints each song's type.
int process_playlist(void) {
int song = 0;
int total_duration = 0;
while (song < NUM_SONGS) {
int duration = get_song_duration();
print_song_type(duration);
total_duration = total_duration + duration;
song = song + 1;
}
return total_duration;
}
// Gets a song duration from the user and returns the entered duration.
// Prompts the user to enter a duration.
int get_song_duration(void) {
int duration;
printf("Duration: ");
scanf("%d", &duration);
return duration;
}
// Prints the type of song for the given duration.
void print_song_type(int duration) {
if (duration >= EXTENDED_SONG) {
printf("Extended\n");
} else if (duration >= LONG_SONG) {
printf("Long\n");
}
}
// Prints summary information for the playlist using the given total duration.
void print_playlist_summary(int total_duration) {
if (total_duration >= LONG_PLAYLIST) {
printf("Long playlist\n");
}
printf("Total duration: %d\n", total_duration);
}
// Week 3 Lecture 2 example
// Arrays of arcade game scores
#include <linux/limits.h>
#include <stdio.h>
#define NUM_GAMES 6
int main(void) {
// Declare array of size NUM_GAMES of double to store game scores
double game_scores[NUM_GAMES];
printf("Enter %d game scores: ", NUM_GAMES);
int i = 0;
while (i < NUM_GAMES){
scanf("%lf", &game_scores[i]);
i = i + 1;
}
// Print all values in array
i = 0;
while (i < NUM_GAMES){
printf("%.2lf ", game_scores[i]);
i = i + 1;
//i++;
}
printf("\n");
// Calculate the total scores
i = 0;
int sum = 0;
while (i < NUM_GAMES){
sum = sum + game_scores[i];
i = i + 1;
}
printf("Sum is %d\n", sum);
// Add a 10-point bonus to every score
// Print all scores again
// Print scores greater than 100
// Find highest score
double max = game_scores[0];
i = 0;
while (i < NUM_GAMES) {
if (game_scores[i] > max) {
max = game_scores[i];
}
i = i + 1;
}
printf("Maximum %lf\n", max);
return 0;
}
// Week 3 Lecture 2 example
// Arrays of arcade game scores
#include <stdio.h>
#define NUM_GAMES 6
int main(void) {
// Declare array of size NUM_GAMES of integers to store game scores
// Print all values in array
printf("Enter %d game scores: ", NUM_GAMES);
// Scan scores into the array
// Print all scores
// Calculate the total scores
// Add a 10-point bonus to every score
// Print all scores again
// Print scores greater than 100
// Find highest score
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("Element at index 1: %d\n", chocolate_eaten[1]);
// What if we print element at index 7?
//printf("Element at index 7: %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 %d\n", sum);
// How can I modify the middle element and make it 99?
chocolate_eaten[DAYS_IN_WEEK/2] = 99;
// How can I print out all data? (check bounds)
int i = 0;
while (i < DAYS_IN_WEEK) {
printf("%d ", chocolate_eaten[i]);
i = i + 1;
}
printf("\n");
return 0;
}