Week 3 Code Examples
// 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
// Print the even ones
#include <stdio.h>
#define SIZE_NUMBERS 6
int main(void) {
//int numbers[] = {2, 5, -8, 4, 1, 9};
int numbers[SIZE_NUMBERS];
//scan numbers into the array
printf("Enter %d numbers: ", SIZE_NUMBERS);
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");
// Increment all values by 10
i = 0;
while ( i < SIZE_NUMBERS) {
numbers[i] = numbers[i] + 10;
i++;
}
// Print the array
i = 0;
while ( i < SIZE_NUMBERS) {
printf("%d ", numbers[i]);
i++;
}
printf("\n");
return 0;
}
// Week 3 Lecture 2 example
// Arrays of integers
// Create an array of ints
// Print them out
// Find the sum
// Find the average
// Print the even ones
// Multiply all elements by 10
// Create another array scan them in
#include <stdio.h>
#define SIZE_NUMBERS 6
#define SIZE_NUMBERS_2 20
#define SIZE_NUMBERS_3 4
void print_array(int size, int array[]);
void scan_array(int size, int array[]);
void increment_array(int size, int array[], int amount);
int sum_array(int size, int array[]);
int main(void) {
int numbers[] = {2, 5, -8, 4, 1, 9};
//int numbers2[SIZE_NUMBERS_2] = {};
//int numbers3[SIZE_NUMBERS_3];
print_array(SIZE_NUMBERS, numbers);
// Increment all values by 10
int i = 0;
while ( i < SIZE_NUMBERS) {
numbers[i] = numbers[i] + 10;
i++;
}
print_array(SIZE_NUMBERS, numbers);
return 0;
}
void print_array(int size, int array[]) {
int i = 0;
while (i < size) {
printf("%d ", array[i]);
i++;
}
printf("\n");
}
// Week 3 Thursday Lecture
// Demonstration of arrays
//
#include <stdio.h>
#define DAYS_IN_WEEK 7
int main(void) {
int chocolate_eating[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_eating[1]);
int i = 0;
while ( i < DAYS_IN_WEEK) {
printf("%d ", chocolate_eating[i]);
i++;
}
printf("\n");
// What if we print element at index 7?
//int i = 7;
//printf("%d\n", chocolate_eating[i]);
// How can I get the sum of the first and last element
int sum = chocolate_eating[0] + chocolate_eating[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_eating[DAYS_IN_WEEK/2] = 99;
printf("Middle value %d\n", chocolate_eating[DAYS_IN_WEEK/2]);
// How can I print out all data? (check bounds)
i = 0;
while ( i < DAYS_IN_WEEK) {
printf("%d ", chocolate_eating[i]);
i++;
}
printf("\n");
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(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;
}
// Demonstration of enum
//
// Date: 22/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};
enum spring_months {SEP, OCT, NOV};
int main(void) {
enum weekdays day;
day = THU;
// What will this print out?
printf("The day number is %d\n", day);
if (day == MON) {
printf("Monday");
} else if (day == TUE) {
printf("Tuesday");
} else if (day == WED) {
printf("Wednesday");
} else if (day == THU) {
printf("Thursday");
} else if (day == FRI) {
printf("Friday");
} else if (day == SAT) {
printf("Saturday");
} else if (day == SUN) {
printf("Sunday");
} else {
printf("Unknown weekday");
}
printf("\n");
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
// Lets print out the data
#include <stdio.h>
enum pokemon_type {FAIRY,FIRE, PSYCHIC, WATER, GRASS, GHOST, DRAGON, 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 jigglypuff;
jigglypuff.hit_points = 80;
jigglypuff.attack = 40;
jigglypuff.defense= 1004.5;
jigglypuff.primary_type = FAIRY;
jigglypuff.secondary_type = NORMAL;
printf("Pokemon Stats: JigglyPuff\n");
printf("Hit Points: %d\n", jigglypuff.hit_points);
printf("Attack: %lf\n", jigglypuff.attack);
printf("Defense: %lf\n", jigglypuff.defense);
if (jigglypuff.primary_type == FAIRY) {
printf("Fairy");
} else if (jigglypuff.primary_type == FIRE) {
printf("Fire");
} else if (jigglypuff.primary_type == PSYCHIC) {
printf("Psychic");
} else if (jigglypuff.primary_type == WATER) {
printf("Water");
} else if (jigglypuff.primary_type == GRASS) {
printf("Ghost");
} else if (jigglypuff.primary_type == DRAGON) {
printf("Dragon");
} else if (jigglypuff.primary_type == NORMAL) {
printf("Normal");
} else {
printf("Unknown Type");
}
printf("\n");
if (jigglypuff.secondary_type == FAIRY) {
printf("Fairy");
} else if (jigglypuff.secondary_type == FIRE) {
printf("Fire");
} else if (jigglypuff.secondary_type == PSYCHIC) {
printf("Psychic");
} else if (jigglypuff.secondary_type == WATER) {
printf("Water");
} else if (jigglypuff.secondary_type == GRASS) {
printf("Grass");
} else if (jigglypuff.secondary_type == GHOST) {
printf("Ghost");
} else if (jigglypuff.secondary_type == DRAGON) {
printf("Dragon");
} else if (jigglypuff.secondary_type == NORMAL) {
printf("Normal");
} else {
printf("Unknown Type");
}
printf("\n");
return 0;
}
// Demonstration of structs
//
// Written by: Angela Finlayson
// Date: 22/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
struct student tina;
printf("Please enter first initial and last initial: ");
scanf("%c %c", &tina.first_initial, &tina.last_initial);
printf("Please enter age: ");
scanf("%d", &tina.age);
printf("Please enter lab mark: ");
scanf("%lf",&tina.lab_mark);
printf("%c %c %d %lf\n", tina.first_initial,
tina.last_initial,
tina.age,
tina.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>
double area_triangle(double base, double height);
int main(void) {
double base;
double height;
double area;
printf("Please enter base and height: ");
scanf("%lf %lf", &base, &height);
area = area_triangle(base, height);
printf("The area is %lf\n", area);
return 0;
}
double area_triangle(double base, double height) {
double area = 0.5 * base * height;
return area;
}
// Week 3 Monday 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
//double num = 10;
double add_numbers (double x, double y);
double num = 99;
int main(void) {
double x = 5.5;
double answer = add_numbers(x, 1.5);
printf("The answer is %lf\n", answer);
printf("x in main %lf\n",x);
printf("%lf\n",num);
return 0;
}
// This function returns the sum of 2 given doubles
double add_numbers (double x, double y) {
double sum;
sum = x + y;
x = x + 1;
printf("X in function %lf\n", x);
num = 0;
printf("%lf\n",num);
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;
// passes the value 10 into the function
increment(x);
// x will still be 10
printf("Main: %d\n", x);
return 0;
}
// Increments the local copy of x only!
void increment(int x) {
// This modifies the local copy of x
x = x + 1;
printf("Inc: %d\n", x);
}
// 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
// Lets print out the data
#include <stdio.h>
enum pokemon_type {FAIRY, FIRE, PSYCHIC, WATER, GRASS, GHOST, DRAGON, NORMAL};
struct pokemon {
int hit_points;
double attack;
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 jigglypuff;
jigglypuff.hit_points = 80;
jigglypuff.attack = 40;
jigglypuff.defense= 1004.5;
jigglypuff.primary_type = FAIRY;
jigglypuff.secondary_type = NORMAL;
printf("Pokemon Stats: JigglyPuff\n");
print_pokemon(jigglypuff);
return 0;
}
void print_pokemon_type(enum pokemon_type type){
if (type == FAIRY) {
printf("Fairy");
} else if (type == FIRE) {
printf("Fire");
} else if (type == PSYCHIC) {
printf("Psychic");
} else if (type == WATER) {
printf("Water");
} else if (type == GRASS) {
printf("Grass");
} else if (type == GHOST) {
printf("Ghost");
} else if (type == DRAGON) {
printf("Dragon");
} else if (type == NORMAL) {
printf("Normal");
} else {
printf("Unknown Type");
}
printf("\n");
}
void print_pokemon(struct pokemon my_pokemon) {
printf("Hit Points: %d\n", my_pokemon.hit_points);
printf("Attack: %lf\n", my_pokemon.attack);
printf("Defense: %lf\n", my_pokemon.defense);
print_pokemon_type(my_pokemon.primary_type);
print_pokemon_type(my_pokemon.secondary_type);
}
// Week 3 Monday 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) {
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;
printf("Please enter integers: ");
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;
printf("Please enter integers: ");
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;
printf("Please enter integers: ");
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");
}
// Week 3 Monday Lecture
// Demonstrations of writing and using a functions
// It is good style to have prototypes at the top, in the order
// they are used in the program.
// Then have the actual implementations below main.
#include <stdio.h>
// Function prototypes
int maximum(int x, int y);
int main(void) {
int num = 7;
// Store the return value in a variable to use later
int max = maximum(10, num);
printf("%d\n", max);
printf("The max of 1 and 99 is %d\n", maximum(1,99));
return 0;
}
// Given two integer inputs, x and y,
// returns the larger of the two integers
int maximum(int x, int y) {
int max = x;
if (x < y) {
max = y;
}
// returns an int value
return max;
}
// Week 3 Monday Lecture
// Demonstrations of writing and using functions with different prototypes
// It is good style to have prototypes at the top, in the order
// they are used in the program.
// Then have the actual implementations below main.
#include <stdio.h>
// Function prototypes
int maximum(int x, int y);
void print_stars(int number_of_stars);
void print_warning(void);
int main(void) {
int num = 7;
// Store the return value in a variable to use later
int max = maximum(10, num);
print_stars(max);
print_warning();
return 0;
}
// Given two integer inputs, x and y,
// returns the larger of the two integers
int maximum(int x, int y) {
int max = x;
if (x < y) {
max = y;
}
// returns an int value
return max;
}
// Given an int input, prints the specified number of stars
// on a single line
void print_stars(int number_of_stars) {
int i = 0;
while (i < number_of_stars) {
printf("*");
i = i + 1;
}
printf("\n");
}
// Prints a warning message
void print_warning(void) {
printf("#########################\n");
printf("Warning: Don't plagiarise\n");
printf("#########################\n");
}