Week 4 Code Examples
#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]);
void copy_array(int grid[ROWS][COLS], int grid_copy[ROWS][COLS]);
//Create a new grid
//copy grid
//Do some for loops
int main(void) {
int grid[ROWS][COLS] = {{3, 5, 1},
{2, 4, 9}};
int grid2[ROWS][COLS];
copy_array(grid, grid2);
print_grid(grid);
print_grid(grid2);
//Make a copy
//Turn it into a function
return 0;
}
void copy_array(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]) {
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");
printf("Grid\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++;
}
}
/*
printf("The sum was %d\n", sum_grid(grid));
print_row_sums(grid);
print_col_sums(grid);
*/
//convert to for loops?
// int row;
// int col;
// printf("Enter a row and col: ");
// scanf("%d %d", &row, &col);
// printf("Element was %d\n", grid[row][col]);
// //scan_grid(grid);
// printf("The sum is %d\n", sum_grid(grid));
// print_row_sums(grid);
// print_col_sums(grid);
#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 index = 0;
int sum = 0;
while (index < SIZE) {
sum = sum + grid[index][index];
index++;
}
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 row = 0;
// while (row < SIZE) {
// int col = 0;
// while (col < SIZE) {
// if (row + col == SIZE - 1) {
// sum = sum + grid[row][col];
// }
// col++;
// }
// row++;
// }
// return sum;
// }
int sum_diagonal_top_right(int grid[SIZE][SIZE]){
int sum = 0;
for (int index = 0; index < SIZE; index++) {
sum = sum + grid[index][SIZE - index - 1];
}
return sum;
}
// 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);
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]);
}
}
// Week 4 Lecture recap example
// Using arrays with functions
#include <stdio.h>
#define DATA_SIZE 6
void print_data(int size, int data[]);
void read_data(int size, int data[]);
// TODO
void print_odd_data(int size, int data[]);
int find_maximum(int size, int data[]);
int main(void) {
int data[DATA_SIZE] = {4, 6, 7, -3, 9, 2};
//int data[DATA_SIZE];
//read_data(DATA_SIZE, data);
print_data(DATA_SIZE, data);
print_odd_data(DATA_SIZE, data);
printf("The max is %d\n", find_maximum(DATA_SIZE, data));
return 0;
}
int find_maximum(int size, int data[]) {
int max = data[0];
int i = 0;
while (i < size) {
if (data[i] > max) {
max = data[i];
}
i++;
}
return max;
}
// WHY CAN WE MODIFY DATA IN THE ARRAY FROM THE FUNCTION?
void read_data(int size, int data[]) {
printf("Enter data: ");
int i = 0;
while (i < size) {
scanf("%d", &data[i]);
i++;
}
}
void print_data(int size, int data[]) {
int i = 0;
while (i < size) {
printf("%d ", data[i]);
i++;
}
printf("\n");
}
void print_odd_data(int size, int data[]) {
int i = 0;
while (i < size) {
if (data[i] % 2 != 0) {
printf("%d ", data[i]);
}
i++;
}
printf("\n");
}
// Week 4 Lecture 1 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 move_x(int size, struct coordinate map[], int x_amount);
// Translate function
int main(void) {
struct coordinate map[MAP_SIZE];
read_map(MAP_SIZE, map);
print_map(MAP_SIZE, map);
move_x(MAP_SIZE, map, 10);
print_map(MAP_SIZE, map);
return 0;
}
void read_map(int size, struct coordinate map[]){
int i = 0;
printf("Enter data:");
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");
}
void move_x(int size, struct coordinate map[], int x_amount) {
int i = 0;
while (i < size){
map[i].x = map[i].x + x_amount;
i++;
}
}
// Week 5 pre-recorded lecture Part 2 code demo
// showing the use of atoi
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int sum = 0;
for (int i = 1; i < argc; i++) {
// atoi converts a string to an int
// For example atoi("123") would return the int 123.
sum = sum + atoi(argv[i]);
}
printf("%d is the sum of all command line args\n", sum);
return 0;
}
// Week 5 pre-recorded lecture Part 2 code demo
// showing the use of command line arguments
// For example you can run this program like
// ./command_line_args "hello world" 5 BLAH
//
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("There are %d command line arguments\n", argc);
// argv[0] is always the program name
printf("This program name is %s\n", argv[0]);
// print out all arguments in the argv array
for (int i = 1; i < argc; i++) {
printf("Argument at index %d is %s\n", i, argv[i]);
}
return 0;
}
// Lecture example for COMP1511/1911 Week 4 Thursday
// 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("Enter your favourite movies: ");
while (fgets(movie, MAX, stdin) != NULL) {
capitalise(movie);
printf("The movie: %s\n", movie);
}
return 0;
}
void capitalise(char s[]) {
for (int i = 0; s[i] != '\0'; i++){
s[i] = toupper(s[i]);
}
}
// Read in one string (try one that is too long)
// Print it out
// Read in many strings instead of 1
// Capitalise each string
// Lecture example for COMP1511/1911 Week 4 Thursday
// Some simple examples demonstrating how strings work in C
#include <stdio.h>
#define MAX_LEN 100
void print_string(char s[]);
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";
//print_string(favourite_food);
// How do you think we read them in?
return 0;
}
// Our own version of printf("%s")
void print_string(char s[]) {
}
// 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 Oscar\n");
} else {
printf("You are not Oscar!\n");
}
if (strcmp(name, "Edgar") < 0) {
printf("Oscar Edgar\n");
} else {
printf("Edgar Oscar\n");
}
// 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);
// remove trailing new line character
int name_len = strlen(name2);
if (name2[name_len - 1] == '\n'){
name2[name_len - 1] = '\0';
}
// Use strcmp to compare 2 strings
if (strcmp("Oscar", name2) == 0) {
printf("Yay Oscar!\n");
}
return 0;
}