Week 5 Code Examples
// Week 4 Lecture 2
// This program is aiming
// to demonstrate the use of simple
// array of arrays
// Basic printing out of an array of arrays
#include <stdio.h>
#define ROWS 3
#define COLS 4
int sum_grid(int array[ROWS][COLS]);
int max_number(int array[ROWS][COLS]);
int main(void) {
//int array[ROWS] = {1, 2, 3};
// 1 2 3 4 5 6 7 8
int array[ROWS][COLS] = {
{101, 22, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
printf("The beautiful array of arrays:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%3d ", array[i][j]);
}
printf("\n");
}
// Function to sum the grid!
printf("The sum of the grid is: %d\n", sum_grid(array));
printf("The max number is %d\n", max_number(array));
return 0;
}
// FUNCTION to find the maximum numbe in the 2D array
// INPUTS: a 2D array
// OUTPUT: int (the max number)
//
int max_number(int array[ROWS][COLS]) {
int max_num = array[0][0];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (array[i][j] > max_num) {
max_num = array[i][j];
}
}
}
return max_num;
}
// FUNCTION is going to sum up the grid
// INPUT: array, size
// OUTPUT: int
int sum_grid(int array[ROWS][COLS]) {
int sum = 0;
for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLS; j++) {
sum = sum + array[i][j];
}
}
return sum;
}
// This program reads positive integers off the comand line
// and then gives us the maximum number in that set
#include <stdio.h>
// Which library is atoi in?
#include <>
int main() {
// TODO Check if arguments were provided
if () {
printf("There are not enough arguments!");
return 1;
}
// Display the maximum number in the set
printf("Maximum number: %d\n", );
return 0;
}
// This program demonstrates how command line arguments
// work in C and what they are (yay being able to take input straight up!)
// Week 5 Lecture 9 (rehash)
#include <stdio.h>
// Note here: if we want to take in arguments, we will change void to
// signal that there are inputs into the main... argc is the count of
// the number of arguments, and argv[] is the actual arguments themselves
int main (int argc, char *argv[]) {
printf("There are %d command line arguments in this program\n", argc);
// argv[0] is always the program name
printf("The program name is %s (argv[0])\n", argv[0]);
// TODO: What about the other command line arguments? Let's loop through
// the array and print them all out!
for(int i = 0; i < argc; i++) {
printf("Argument %d is %s\n", i, argv[i]);
}
//int sum = argv[1] + argv[2];
return 0;
}
// This program takes a command line
// argument name and greets them
#include <stdio.h>
int main() {
//TODO check that there are enough arguments!
if () {
printf("There are not enough arguments to run
program %s\n", );
return ;
}
// TODO Print out the greetings
printf("Greetings, %s!\n", );
return 0;
}
#include <stdio.h>
// Standard library (stdlib.h) - included to have access to a very special function
// atoi() = converts to an integer!
#include <stdlib.h>
// This program doubles the odd numbers entered on
// the command line
// ./program 3 5 6 10
int main (int argc, char *argv[]) {
// ./program 3 5 6 7
// argv[0] = ./program
// argv[1] = 3
// argv[2] = 5
// argv[3] = 6
// argv[4] = 7
int sum = 0;
if (argc < 3) {
printf("There are not enough arguments to add up!\n");
return 1;
}
for (int i = 1; i < argc; i++) {
// thhis function atoi() in stdlib.h converts from a string to
// an integer
printf("argv[%d] = %s\n", i, argv[i]);
sum = sum + atoi(argv[i]);
}
printf("The sum is %d\n", sum);
return 0;
}
#include <stdio.h>
#define MAP_ROWS 8
#define MAP_COLUMNS 8
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'
// --- Enums and Structs ---
enum tile {
EMPTY,
WALL,
CHURRO,
TRAP,
PLAYER
};
struct location {
enum tile tile;
};
struct player {
int x;
int y;
int churros_eaten;
};
// --- Starter Function Prototypes ---
void initialise_map(struct location map[MAP_ROWS][MAP_COLUMNS]);
void print_map(struct location map[MAP_ROWS][MAP_COLUMNS]);
void print_welcome(void);
void place_walls(struct location map[MAP_ROWS][MAP_COLUMNS]);
int check_validity(int row, int col, struct location map[MAP_ROWS][MAP_COLUMNS]);
void place_churros(struct location map[MAP_ROWS][MAP_COLUMNS]);
void play_game(struct location map[MAP_ROWS][MAP_COLUMNS], struct player hero);
int count_churros(struct location map[MAP_ROWS][MAP_COLUMNS]);
int main(void) {
struct location map[MAP_ROWS][MAP_COLUMNS];
initialise_map(map);
//int player_x;
//int player_y;
struct player hero;
printf("Enter the player's starting location: ");
scanf("%d %d", &hero.x, &hero.y);
map[hero.x][hero.y].tile = PLAYER;
// Function here to place some walls!
place_walls(map);
// Function here to place some churros!
place_churros(map);
print_welcome();
print_map(map);
printf("\n\n");
play_game(map, hero);
return 0;
}
// Play the game function - yay!
// INPUT: map and hero
// OUTPUT: void
void play_game(struct location map[MAP_ROWS][MAP_COLUMNS], struct player hero){
char move;
hero.churros_eaten = 0;
printf("Move: ");
while (scanf(" %c", &move) == 1) {
int row = hero.x;
int col = hero.y;
if (move == UP){
row--;
} else if (move == DOWN) {
row++;
} else if (move == LEFT) {
col--;
} else if (move == RIGHT) {
col++;
}
// If it is not a valid move, i want to stop
// the move from happening, and the player won't
// move
//
//if(!check_validity(row, col, map)) {
// printf("Not a valid move...\n");
if (row < 0 || row >= MAP_ROWS || col < 0 || col >= MAP_COLUMNS) {
printf("Not a valid move, out of bounds!\n");
} else if (map[row][col].tile == WALL) {
printf("You have walked into a wall! Try again!\n");
} else {
map[hero.x][hero.y].tile = EMPTY;
if (map[row][col].tile == CHURRO) {
hero.churros_eaten++;
printf("Yum! You ate a churro!\n");
printf("Total churros eaten: %d\n", hero.churros_eaten);
}
hero.x = row;
hero.y = col;
map[hero.x][hero.y].tile = PLAYER;
}
print_map(map);
if (count_churros(map) == 0) {
printf("You ate all the churros! VICTORY is yours!\n");
return;
}
printf("Move: ");
}
}
// ADD YOUR OWN FUNctions HERE
int count_churros(struct location map[MAP_ROWS][MAP_COLUMNS]){
int count = 0;
for (int i = 0; i < MAP_ROWS; i++) {
for (int j = 0; j < MAP_COLUMNS; j++) {
if (map[i][j].tile == CHURRO) {
count++;
}
}
}
return count;
}
// Function to place some walls
// INPUTS: map
// OUTPUT: void
void place_walls(struct location map[MAP_ROWS][MAP_COLUMNS]) {
int num;
printf("How many walls would you like to place?");
scanf("%d", &num);
for (int i = 0; i < num; i++) {
int row;
int col;
printf("Where would you like to place wall? ");
scanf("%d %d", &row, &col);
while (!check_validity(row, col, map)){
printf("Invalid spot, try again ");
scanf("%d %d", &row, &col);
}
map[row][col].tile = WALL;
}
}
// Function to place some churros
// INPUTS: map
// OUTPUT: void
void place_churros(struct location map[MAP_ROWS][MAP_COLUMNS]) {
int num;
printf("How many churros would you like to place?");
scanf("%d", &num);
for (int i = 0; i < num; i++) {
int row;
int col;
printf("Where would you like to place a churro? ");
scanf("%d %d", &row, &col);
while (!check_validity(row, col, map)){
printf("Invalid spot, try again ");
scanf("%d %d", &row, &col);
}
map[row][col].tile = CHURRO;
}
}
// Function to check valid input
// INPUT int row, int col, map
// OUTPUT int (1 or 0 depending if it is a valid move or not)
int check_validity(int row, int col, struct location map[MAP_ROWS][MAP_COLUMNS]) {
if (row < 0 || row >= MAP_ROWS || col < 0 || col >= MAP_COLUMNS) {
return 0;
} else if (map[row][col].tile != EMPTY) {
return 0;
}
return 1;
}
// STARTER FUNCTIONS - DO NOT CHANGE
void initialise_map(struct location map[MAP_ROWS][MAP_COLUMNS]) {
for (int r = 0; r < MAP_ROWS; r++) {
for (int c = 0; c < MAP_COLUMNS; c++) {
map[r][c].tile = EMPTY;
}
}
}
void print_map(struct location map[MAP_ROWS][MAP_COLUMNS]) {
for (int r = 0; r < MAP_ROWS; r++) {
for (int c = 0; c < MAP_COLUMNS; c++) {
if (map[r][c].tile == PLAYER) {
printf(" P ");
} else if (map[r][c].tile == WALL) {
printf(" # ");
} else if (map[r][c].tile == CHURRO) {
printf(" C ");
} else if (map[r][c].tile == TRAP) {
printf("(!)");
} else {
printf(" . ");
}
}
printf("\n");
}
}
void print_welcome(void) {
printf("____________________________\n\n");
printf("Welcome to the Churro Quest!\n\n");
printf("____________________________\n\n");
}
#include <stdio.h>
int main (void) {
// normal int variable
int my_variable = 42;
// now i want a pointer to store the address
// of the variable above
int *my_variable_ptr = &my_variable;
printf("The pointer my_variable_ptr has value %p\n", my_variable_ptr);
printf("The pointer has its own address %p\n", &my_variable_ptr);
printf("It is pointing to the variable my_variable, %d\n", *my_variable_ptr);
return 0;
}
// This program takes in command line arguments and then
// prints out the most common command line arguments and how
// many times it occurs
// GOAL: We need to find the most frequently occurring command line argument
// - We need to count how many times each argument appears
// - We need to track which argument has the highest count
// Week 5 Lecture 9
#include <stdio.h>
// I am going to use strcmp() which means I need the string library!
#include <string.h>
int main(int argc, char *argv[]) {
// TODO Check that there are enough arguments
// Don't forget the first argument (argv[0]) is the program name,
// so we need at least one more argument to find a "most common" term.
if (argc < 2) {
printf("Not enough arguments!");
return 1;
}
// ./program dog dog dog cat
// Most common term is: dog, which occurs 3 times
// So what steps should we take to solve the problem?
// 1. Declare and Initialise some variables to track the most frequent argument
// track highest frequency
// 2. For each argument (where should we start counting the arguemtns?argv[1]):
// go through the array of strings (argv[]) count how many times it appears
// if hte current that we are counting appears less than the next one, we
// we want our most frequent to be the next one....
int max_freq = 0;
int most_common_index = 1;
// Outer loop - pick the word to count
for (int i = 1; i < argc; i++) {
int current_count = 0;
// Inner loop is picking up all the other words to compare against
for (int j = 1; j < argc; j++) {
if (strcmp(argv[i], argv[j]) == 0) {
current_count++;
}
}
// Update if this word is more frequent than any other that has
// already been counted!
if (current_count > max_freq) {
max_freq = current_count;
most_common_index = i;
}
}
printf("Most common term is: %s, which occurs %d times\n", argv[most_common_index], max_freq);
return 0;
}
// I want to swap two numbers in a function....
#include <stdio.h>
void swap(int *number1_ptr, int *number2_ptr);
int main(void) {
int number1 = 10;
int number2 = 20;
printf("Before swap: \n");
printf("Number 1: %d, Number 2: %d, \n", number1, number2);
swap(&number1, &number2);
printf("After swap: \n");
printf("Number 1: %d, Number 2: %d, \n", number1, number2);
}
// Function that swaps two numbers
// OUTPUT: void
// INPUT: two ints to swap
void swap(int *number1_ptr, int *number2_ptr) {
int temp = *number1_ptr;
*number1_ptr = *number2_ptr;
*number2_ptr = temp;
}