Week 4 Code Examples
// Week 3 Lecture 2 example continued in Week 4 Lecture 1
// Arrays of arcade game scores
#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);
// 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);
// Print values 10 or more
printf("Values 10 or more\n");
i = 0;
while (i < NUM_GAMES) {
if (game_scores[i] >= 10) {
printf("%.2lf ", game_scores[i]);
}
i++;
}
printf("\n");
// Print in reverse
i = NUM_GAMES - 1;
while (i >= 0) {
printf("%.2lf ", game_scores[i]);
i--;
}
printf("\n");
return 0;
}
// Week 4 Lecture example
// Using arrays with functions
#include <stdio.h>
#define NUM_GAMES 6
void read_scores(int size, double scores[]);
void print_scores(int size, double scores[]);
void scale_data_within_range(
int size,
double scores[],
double scale,
double min,
double max
);
int main(void) {
double game_scores[NUM_GAMES];
read_scores(NUM_GAMES, game_scores);
print_scores(NUM_GAMES, game_scores);
scale_data_within_range(NUM_GAMES, game_scores, 2, 5, 8);
print_scores(NUM_GAMES, game_scores);
return 0;
}
// Reads the given number of scores from
// the terminal into the given array
void read_scores(int size, double scores[]) {
printf("Enter %d game scores: ", size);
int i = 0;
while (i < size) {
scanf("%lf", &scores[i]);
i = i + 1;
}
}
// Prints the given number of scores from the
// array to the terminal
void print_scores(int size, double scores[]) {
int i = 0;
while (i < size) {
printf("%.2lf ", scores[i]);
i = i + 1;
}
printf("\n");
}
// Adds the given scale factor to the values in the given array
// that are within the given range.
void scale_data_within_range(
int size,
double scores[],
double scale,
double min,
double max
) {
for (int i = 0; i < size; i++) {
if (scores[i] >= min && scores[i] <= max) {
scores[i] = scores[i] + scale;
}
}
}
// Week 4 example
// Using arrays of structs
#include <stdio.h>
#define MAX_TILES 5
struct tile {
char letter;
int score;
};
void read_tiles(int size, struct tile tiles[]);
void print_tiles(int size, struct tile tiles[]);
int search_tile(int size, struct tile tiles[], char search_char);
int main(void) {
// Declare the array
struct tile tiles[MAX_TILES];
printf("Enter 5 letter score pairs: ");
read_tiles(MAX_TILES, tiles);
print_tiles(MAX_TILES, tiles);
printf("Enter a letter to search for: ");
char input;
scanf(" %c", &input);
int score = search_tile(MAX_TILES, tiles, input);
if (score != -1) {
printf("The score for %c is %d\n", input, score);
} else {
printf("Not found\n");
}
return 0;
}
void read_tiles(int size, struct tile tiles[]) {
for (int i = 0; i < size; i++) {
scanf(" %c %d", &tiles[i].letter, &tiles[i].score);
}
}
void print_tiles(int size, struct tile tiles[]) {
for (int i = 0; i < size; i++) {
printf("(%c %d) ", tiles[i].letter, tiles[i].score);
}
printf("\n");
}
// Search for a tile with the given 'search_char'
// return the score or -1 if it does not exist
int search_tile(int size, struct tile tiles[], char search_char) {
for (int i = 0; i < size; i++) {
if (tiles[i].letter == search_char) {
return tiles[i].score;
}
}
// We did not find it :(
return -1;
}
#include <stdio.h>
#define ROWS 2
#define COLS 3
void print_grid(int row_size, int grid[][COLS]);
void scan_grid(int row_size, int grid[][COLS]);
int sum_grid(int row_size, int grid[][COLS]);
void print_row_sums(int row_size, int grid[][COLS]);
void print_col_sums(int row_size, int grid[][COLS]);
int main(void) {
// int grid[ROWS][COLS] = {{3, 5, 1},
// {2, 4, 9}};
int grid[ROWS][COLS];
// int row;
// int col;
// printf("Enter a row and col: ");
// scanf("%d %d", &row, &col);
// printf("Value: %d\n", grid[row][col]);
printf("Enter values:\n");
scan_grid(ROWS, grid);
print_grid(ROWS, grid);
int sum = sum_grid(ROWS, grid);
printf("The total sum is %d\n", sum);
print_row_sums(ROWS, grid);
print_col_sums(ROWS, grid);
return 0;
}
void print_grid(int row_size, int grid[][COLS]) {
//Print first row only
int row = 0;
while (row < row_size) {
int col = 0;
while (col < COLS) {
printf("%d ", grid[row][col]);
col++;
}
printf("\n");
row++;
}
}
void scan_grid(int row_size, int grid[][COLS]) {
//Print first row only
int row = 0;
while (row < row_size) {
int col = 0;
while (col < COLS) {
scanf("%d", &grid[row][col]);
col++;
}
row++;
}
}
int sum_grid(int row_size, int grid[][COLS]) {
int sum = 0;
int row = 0;
while (row < row_size) {
int col = 0;
while (col < COLS) {
sum = sum + grid[row][col];
col++;
}
row++;
}
return sum;
}
void print_row_sums(int row_size, int grid[][COLS]){
int row = 0;
while (row < row_size) {
int sum = 0;
int col = 0;
while (col < COLS) {
sum = sum + grid[row][col];
col++;
}
printf("Row %d sum is %d\n", row, sum);
row++;
}
}
// We are going column by column instead of row by row
void print_col_sums(int row_size, int grid[][COLS]){
int col = 0;
while (col < COLS) {
int sum = 0;
int row = 0;
while (row < row_size) {
sum = sum + grid[row][col];
row++;
}
printf("Col %d sum is %d\n", col, sum);
col++;
}
}
#include <stdio.h>
#define SIZE 4
int sum_diagonal_top_left(int grid[SIZE][SIZE]);
int sum_diagonal_top_right(int grid[SIZE][SIZE]);
int main(void) {
int grid[SIZE][SIZE] = {{3, 5, 1, 2},
{2, 4, 9, 8},
{6, 0, 2, 7},
{4, 1, 6, 5}};
printf("The sum diagonal top left %d\n", sum_diagonal_top_left(grid));
printf("The sum diagonal top right %d\n", sum_diagonal_top_right(grid));
return 0;
}
int sum_diagonal_top_left(int grid[SIZE][SIZE]){
int sum = 0;
int i = 0;
while (i < SIZE) {
sum = sum + grid[i][i];
i++;
}
return sum;
}
// int sum_diagonal_top_left(int grid[SIZE][SIZE]){
// int sum = 0;
// int row = 0;
// while (row < SIZE) {
// int col = 0;
// while (col < SIZE){
// if (row == col) {
// sum = sum + grid[row][col];
// }
// col++;
// }
// row++;
// }
// return sum;
// }
int sum_diagonal_top_right(int grid[SIZE][SIZE]){
return -1;
}
#include <stdio.h>
#define ROWS 3
#define COLS 2
void print_grid(int grid[ROWS][COLS]);
int main(void) {
int grid1[ROWS][COLS] = {{2, 9},
{5, 1},
{8, 0}};
int grid2[ROWS][COLS];
print_grid(grid1);
//Write code to copy a grid
return 0;
}
void print_grid(int grid[ROWS][COLS]) {
int row = 0;
while (row < ROWS) {
int col = 0;
while (col < COLS) {
printf("%d ", grid[row][col]);
col++;
}
printf("\n");
row++;
}
}
#include <stdio.h>
#define ROWS 3
#define COLS 2
void print_grid(int grid[ROWS][COLS]);
void copy_grid(int grid[ROWS][COLS], int grid_copy[ROWS][COLS]);
int main(void) {
int grid1[ROWS][COLS] = {{2, 9},
{5, 1},
{8, 0}};
int grid2[ROWS][COLS];
// copy grid code
//grid2 = grid1; CAN'T DO THIS
copy_grid(grid1, grid2);
printf("Grid 1\n");
print_grid(grid1);
printf("\nGrid 2\n");
print_grid(grid2);
return 0;
}
void copy_grid(int grid[ROWS][COLS], int grid_copy[ROWS][COLS]){
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
grid_copy[row][col] = grid[row][col];
}
}
}
void print_grid(int grid[ROWS][COLS]) {
int row = 0;
while (row < ROWS) {
int col = 0;
while (col < COLS) {
printf("%d ", grid[row][col]);
col++;
}
printf("\n");
row++;
}
}
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 50
int main(void) {
char high_score_name[MAX_LENGTH] = "DarkReaper17";
char player_name[MAX_LENGTH];
printf("You got a new high score!\n");
printf("Enter your name: ");
fgets(player_name, MAX_LENGTH, stdin);
// Check for a trailing new line
// and get rid of it
int len = strlen(player_name);
if (len > 0 && player_name[len - 1] == '\n') {
player_name[len - 1] = '\0';
}
// What is wrong with this code?
if (strcmp(player_name, high_score_name) == 0) {
printf("You beat your own high score!\n");
} else {
strcpy (high_score_name, player_name);
printf("New high score holder: %s\n", high_score_name);
}
return 0;
}
// Lecture example for COMP1511/1911 Week 4
// Some simple examples demonstrating how strings work in C
#include <stdio.h>
#include <ctype.h>
#define MAX 20
void capitalise(char s[]);
// Change to read many strings
int main(void) {
char movie[MAX];
printf("Type in your favourite movies: ");
while (fgets(movie, MAX, stdin) != NULL){
printf("%s\n", movie);
capitalise(movie);
printf("%s\n", movie);
}
return 0;
}
// convert every lowercase character in the given string, s to uppercase
void capitalise(char s[]) {
int i = 0;
while (s[i] != '\0') {
s[i] = toupper(s[i]);
i++;
}
}
// Lecture example for COMP1511/1911 Week 4
// Some simple examples demonstrating how strings work in C
#include <stdio.h>
#define MAX_LEN 100
int main(void) {
// What do these look like in memory
char favourite_food[] = {'k', 'f', 'c', '\0'};
char favourite_drink[] = "kombucha";
char favourite_book[MAX_LEN] = "Folk of the Faraway Tree";
// Can print out characters
printf("%c\n",favourite_food[0]);
printf("%c\n",favourite_drink[3]);
// Can print out the whole string
printf("%s\n", favourite_food);
printf("%s\n", favourite_drink);
printf("%s\n", favourite_book);
favourite_drink[0] = 'Z';
printf("%s\n", favourite_drink);
favourite_drink[4] = '\0';
printf("%s\n", favourite_drink);
favourite_food[3] = 'd';
printf("%s\n", favourite_food);
// We can't do this like this!!!!
favourite_book = "Programming in C";
// How do you think we read them in?
return 0;
}
// Example of using string functions from string.h library
// strings can get a bit tricky and messy in C
// It is easy to overflow buffers or lose our '\0' characters!
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
int main(void) {
// Declare an array to store a string
char puppy[MAX_LENGTH] = "Finn";
char puppy2[MAX_LENGTH];
printf("%s\n", puppy);
strcpy(puppy2,"Boris");
printf("%s\n", puppy2);
// Find string length. It does NOT include '\0' in the length
int len = strlen(puppy);
int len2 = strlen(puppy2);
printf("%s has length %d\n", puppy, len);
printf("%s has length %d\n", puppy2, len2);
// Declare an array to store a string
char name[] = "Oscar";
if (strcmp(name, "Oscar") == 0) {
printf("%s equals Oscar\n", name);
} else {
printf("%s does not equal Oscar!\n", name);
}
if (strcmp(name, "Edgar") == 0) {
printf("%s equals Edgar\n", name);
} else {
printf("%s does not equal Edgar!\n", name);
}
if (strcmp(name, "Edgar") < 0) {
printf("%s less than Edgar\n", name);
} else {
printf("%s greater than or equal to Edgar\n", name);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 50
int main(void) {
char high_score_name[MAX_LENGTH] = "DarkReaper17";
char player_name[MAX_LENGTH];
printf("You got a new high score!\n");
printf("Enter your name: ");
fgets(player_name, MAX_LENGTH, stdin);
// What is wrong with this code?
if (player_name == high_score_name) {
printf("You beat your own high score!\n");
} else {
high_score_name = player_name;
printf("New high score holder: %s\n", high_score_name);
}
return 0;
}
// Lecture example for COMP1511/1911 Week 4
// Some simple examples demonstrating how strings work in C
#include <stdio.h>
#include <ctype.h>
#define MAX 20
void capitalise(char s[]);
// Change to read many strings
int main(void) {
char movie[MAX];
printf("Type in your favourite movies: ");
return 0;
}
// convert every lowercase character in the given string, s to uppercase
void capitalise(char s[]) {
}
// Lecture example for COMP1511/1911 Week 4
// Some simple examples demonstrating how strings work in C
#include <stdio.h>
#define MAX_LEN 100
int main(void) {
// What do these look like in memory
char favourite_food[] = {'k', 'f', 'c', '\0'};
char favourite_drink[] = "kombucha";
char favourite_book[MAX_LEN] = "Folk of the Faraway Tree";
// Can print out characters
printf("%c\n",favourite_food[0]);
printf("%c\n",favourite_drink[3]);
// Can print out the whole string
printf("%s\n", favourite_food);
printf("%s\n", favourite_drink);
printf("%s\n", favourite_book);
// favourite_drink[0] = 'Z';
// printf("%s\n", favourite_drink);
// favourite_drink[4] = '\0';
// printf("%s\n", favourite_drink);
// favourite_food[3] = 'd';
// printf("%s\n", favourite_food);
// We can't do this like this!!!!
//favourite_book = "Programming in C";
// How do you think we read them in?
return 0;
}
// Example of using string functions from string.h library
// strings can get a bit tricky and messy in C
// It is easy to overflow buffers or lose our '\0' characters!
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
int main(void) {
// Declare an array to store a string
char puppy[MAX_LENGTH] = "Finn";
char puppy2[MAX_LENGTH];
printf("%s\n", puppy);
puppy2 = "Boris";
printf("%s\n", puppy2);
// Find string length. It does NOT include '\0' in the length
int len = strlen(puppy);
int len2 = strlen(puppy2);
printf("%s has length %d\n", puppy, len);
printf("%s has length %d\n", puppy2, len2);
// Declare an array to store a string
// char name[] = "Oscar";
// if (strcmp(name, "Oscar") == 0) {
// printf("%s equals Oscar\n", name);
// } else {
// printf("%s does not equal Oscar!\n", name);
// }
// if (strcmp(name, "Edgar") == 0) {
// printf("%s equals Edgar\n", name);
// } else {
// printf("%s does not equal Edgar!\n", name);
// }
// if (strcmp(name, "Edgar") < 0) {
// printf("%s less than Edgar\n", name);
// } else {
// printf("%s greater than or equal to Edgar\n", name);
// }
return 0;
}
// An example of an array of strings
// This assumes we have at most 20 9 letter words
#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 20
#define MAX_LEN 10
// 0 1 2 3 4 5 6 7 8 9
//0 h a t \0
//1 c a k e \0
//2 t e a \0
//3 s u n \0
//4 \0
//5 \0
//etc
void print_text(char text[MAX_STRINGS][MAX_LEN], int max_lines);
int main(void){
char text[MAX_STRINGS][MAX_LEN] = {"hat", "cake", "tea"};
int num_strings = 3;
// Print one string
printf("%s\n", text[2]);
// Print one character
printf("%c\n", text[1][2]);
printf("Printing out text\n");
print_text(text, num_strings);
//text[3] = "sun"; NO we can't use = for strings
strcpy(text[3], "sun");
num_strings++;
printf("Printing out text\n");
print_text(text, num_strings);
return 0;
}
void print_text(char data[MAX_STRINGS][MAX_LEN], int size){
for (int i = 0; i < size; i++) {
printf("%s\n", data[i]);
}
}