Week 4 Code Examples
// Week 4 Lecture example
// Using arrays with functions
#include <stdio.h>
#define DATA_SIZE 6
void read_data(int size, int array[]);
void print_data(int size, int array[]);
void print_odd_data(int size, int array[]);
int find_maximum(int size, int array[]);
int main(void) {
int data[DATA_SIZE];
printf("Enter 6 integers: ");
read_data(DATA_SIZE, data);
print_data(DATA_SIZE, data);
print_odd_data(DATA_SIZE, data);
int max = find_maximum(DATA_SIZE, data);
printf("Max: %d\n", max);
return 0;
}
int find_maximum(int size, int array[]) {
int max = array[0];
int i = 1;
while (i < size) {
if (array[i] > max) {
max = array[i];
}
i++;
}
return max;
}
void print_odd_data(int size, int array[]){
int i = 0;
while (i < size) {
if (array[i] % 2 != 0) {
printf("%d ", array[i]);
}
i++;
}
printf("\n");
}
//scan numbers into the array
void read_data(int size, int array[]) {
int i = 0;
while (i < size) {
scanf("%d", &array[i]);
i++;
}
}
// Print the array
void print_data(int size, int array[]){
int i = 0;
while (i < size) {
printf("%d ", array[i]);
i++;
}
printf("\n");
}
// Week 4 example
// Using arrays of structs
#include <stdio.h>
#define MAP_SIZE 5
struct coordinate {
int x;
int y;
};
void read_map(int size, struct coordinate map[]);
void print_map(int size, struct coordinate map[]);
void translate_x(int size, struct coordinate map[], int amount);
int main(void) {
struct coordinate map[MAP_SIZE];
read_map(MAP_SIZE, map);
print_map(MAP_SIZE, map);
translate_x(MAP_SIZE, map, 2);
print_map(MAP_SIZE, map);
return 0;
}
void translate_x(int size, struct coordinate map[], int amount){
int i = 0;
while (i < size) {
map[i].x = map[i].x + amount;
i++;
}
}
void read_map(int size, struct coordinate map[]){
int i = 0;
while (i < size) {
scanf("%d %d", &map[i].x, &map[i].y);
i++;
}
}
void print_map(int size, struct coordinate map[]){
int i = 0;
while (i < size) {
printf("(%d, %d)", map[i].x, map[i].y);
i++;
}
printf("\n");
}
#include <stdio.h>
#define ROWS 2
#define COLS 3
void print_grid(int grid[ROWS][COLS]);
void scan_grid(int grid[ROWS][COLS]);
int sum_grid(int grid[ROWS][COLS]);
void print_row_sums(int grid[ROWS][COLS]);
void print_col_sums(int grid[ROWS][COLS]);
int main(void) {
int grid[ROWS][COLS] = {{3, 5, 1},
{2, 4, 9}};
// int row;
// int col;
// printf("Enter a row and col: ");
// scanf("%d %d", &row, &col);
// printf("Value: %d\n", grid[row][col]);
print_grid(grid);
//scan_grid(grid);
//print_grid(grid);
int sum = sum_grid(grid);
printf("Sum is %d\n", sum);
print_row_sums(grid);
print_col_sums(grid);
return 0;
}
void print_grid(int grid[ROWS][COLS]) {
printf("Grid\n");
int row = 0;
while (row < ROWS) {
int col = 0;
while (col < COLS) {
printf("%d ", grid[row][col]);
col++;
}
printf("\n");
row++;
}
}
void scan_grid(int grid[ROWS][COLS]) {
printf("Please enter data\n");
int row = 0;
while (row < ROWS) {
int col = 0;
while (col < COLS) {
scanf("%d", &grid[row][col]);
col++;
}
row++;
}
}
int sum_grid(int grid[ROWS][COLS]) {
int sum = 0;
int row = 0;
while (row < ROWS) {
int col = 0;
while (col < COLS) {
sum = sum + grid[row][col];
col++;
}
row++;
}
return sum;
}
void print_row_sums(int grid[ROWS][COLS]){
int row = 0;
while (row < ROWS) {
int col = 0;
int sum = 0;
while (col < COLS) {
sum = sum + grid[row][col];
col++;
}
printf("Sum of row %d is %d\n", row, sum);
row++;
}
}
void print_col_sums(int grid[ROWS][COLS]){
int col = 0;
while (col < COLS) {
int row = 0;
int sum = 0;
while (row < ROWS) {
sum = sum + grid[row][col];
row++;
}
printf("Sum of col %d 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]){
int sum = 0;
int i = 0;
while (i < SIZE) {
sum = sum + grid[i][SIZE - i - 1];
i++;
}
return sum;
}
#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(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]){
int row = 0;
while (row < ROWS) {
int col = 0;
while (col < COLS) {
grid_copy[row][col] = grid[row][col];
col++;
}
row++;
}
}
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++;
}
}
// 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[]);
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);
//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] = "Boots";
printf("%s\n", puppy);
// Copy the string "Finn" into the word array
// Make sure your array is big enough!!!
strcpy(puppy, "Finn");
printf("%s\n", puppy);
// Find string length. It does NOT include '\0' in the length
int len = strlen(puppy);
printf("%s has length %d\n", puppy, len);
// Declare an array to store a string
// char name[] = "Oscar";
// if (strcmp(name, "Oscar") == 0) {
// printf("Hi Oscar\n");
// } else {
// printf("You are not Oscar!\n");
// }
// if (strcmp(name, "Edgar") == 0) {
// printf("Hi Edgar\n");
// } else {
// printf("You are not Edgar!\n");
// }
// if (strcmp(name, "Edgar") < 0) {
// printf("%s Edgar\n", name);
// } else {
// printf("Edgar %s\n", name);
// }
/*
// Declare an array to store a string
char name2[MAX_LENGTH];
printf("\nType in a name: ");
// Read in a string
fgets(name2, MAX_LENGTH, stdin);
// Use strcmp to compare 2 strings
if (strcmp("Oscar", name2) == 0) {
printf("Yay Oscar!\n");
}
*/
return 0;
}