Week 5 Code Examples
#include <stdio.h>
// this is a program that prints out it's command line arguments.
// ./program hello
// print -> hello
// int argc (argument count)
// char *argv[] -> an array of strings
int main(int argc, char *argv[]) {
printf("There are %d parameter\n", argc);
if (argc == 1) {
printf("Yay, 1 parameter\n");
fputs(argv[0], stdout);
printf("\n");
}
if (argc > 1) {
// now we know a second parameter must be there
fputs(argv[1], stdout);
printf("\n");
}
return 0;
}
// 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;
}
#include <stdio.h>
#define MAP_ROWS 8
#define MAP_COLUMNS 8
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'
enum tile {
EMPTY,
WALL,
CHURRO,
TRAP,
TELEPORT,
PLAYER
};
enum player_action {
MOVE_UP,
MOVE_DOWN,
MOVE_LEFT,
MOVE_RIGHT,
NOTHING,
COLLECT
};
struct location {
enum tile tile;
enum player_action player_action;
};
void initialise_map(struct location map[MAP_ROWS][MAP_COLUMNS]);
void print_map(struct location map[MAP_ROWS][MAP_COLUMNS]);
void print_location(struct location location, int place_print);
void place_churros(struct location map[MAP_ROWS][MAP_COLUMNS]);
void place_walls(struct location map[MAP_ROWS][MAP_COLUMNS]);
void move_player(struct location map[MAP_ROWS][MAP_COLUMNS], int player_row, int player_col);
int check_validity(int row, int col, struct location map[MAP_ROWS][MAP_COLUMNS]);
int main(void) {
struct location map[MAP_ROWS][MAP_COLUMNS];
initialise_map(map);
int player_row, player_col;
printf("Where are you? ");
scanf("%d %d", &player_row, &player_col);
map[player_row][player_col].tile = PLAYER;
place_churros(map);
place_walls(map);
print_map(map);
move_player(map, player_row, player_col);
return 0;
}
void place_churros(struct location map[MAP_ROWS][MAP_COLUMNS]) {
}
void place_walls(struct location map[MAP_ROWS][MAP_COLUMNS]) {
int wall_number;
printf("How many walls would you like to place? ");
scanf("%d", &wall_number);
for (int i = 0; i < wall_number; i++) {
int wall_row, wall_col;
printf("Where would you like to place the wall? ");
scanf("%d %d", &wall_row, &wall_col);
while (!check_validity(wall_row, wall_col, map) || map[wall_row][wall_col].tile != EMPTY) {
printf("Input not valid!\nWhere would you like to place the wall? ");
scanf("%d %d", &wall_row, &wall_col);
}
map[wall_row][wall_col].tile = WALL;
}
}
void move_player(struct location map[MAP_ROWS][MAP_COLUMNS], int player_row, int player_col) {
char direction;
printf("Move using (w/a/s/d). Press Ctrl+D to stop.\n");
while (scanf(" %c", &direction) == 1) { // Continue reading until EOF (Ctrl+D)
int new_row = player_row;
int new_col = player_col;
if (direction == UP) {
new_row--;
} else if (direction == DOWN) {
new_row++;
} else if (direction == LEFT) {
new_col--;
} else if (direction == RIGHT) {
new_col++;
}
if (!check_validity(new_row, new_col, map)) {
printf("You can't move there!\n");
}
if (map[new_row][new_col].tile == WALL) {
printf("There's a wall! Can't move.\n");
} else if (map[new_row][new_col].tile == CHURRO) {
printf("You collected a churro!\n");
}
map[player_row][player_col].tile = EMPTY;
player_row = new_row;
player_col = new_col;
map[player_row][player_col].tile = PLAYER;
print_map(map);
}
printf("\nGame ended (Ctrl+D detected).\n");
}
// we want to make sure that our struct location is within the bounds of the map
// row > 0 and row < MAP_ROWS, etc for col
int check_validity(int row, int col, struct location map[MAP_ROWS][MAP_COLUMNS]) {
int result = 0;
if (row < 0 || row >= MAP_ROWS) {
return 0;
}
else if (col < 0 || col >= MAP_COLUMNS) {
return 0;
}
if (map[row][col].tile == WALL) {
return 0;
}
return 1;
}
// It initialises the map
// it should set each map[row][col] to EMPTY and NOTHING
void initialise_map(struct location map[MAP_ROWS][MAP_COLUMNS]) {
for(int row = 0; row < MAP_ROWS; row++) {
for(int col = 0; col < MAP_COLUMNS; col++) {
map[row][col].tile = EMPTY;
map[row][col].player_action = NOTHING;
}
}
}
void print_map(struct location map[MAP_ROWS][MAP_COLUMNS]) {
for (int row = 0; row < MAP_ROWS * 2; row++) {
for (int col = 0; col < MAP_COLUMNS; col++) {
print_location(map[row / 2][col], row % 2);
}
printf("\n");
}
}
void print_location(struct location location, int place_print) {
if (place_print) {
if (location.player_action == MOVE_DOWN) {
printf(" v ");
} else if (location.player_action == MOVE_UP) {
printf(" ^ ");
} else if (location.player_action == MOVE_LEFT) {
printf(" < ");
} else if (location.player_action == MOVE_RIGHT) {
printf(" > ");
} else {
printf(" . ");
}
} else {
if (location.tile == EMPTY) {
printf(" ");
} else if (location.tile == WALL) {
printf(" # ");
} else if (location.tile == CHURRO) {
printf(" C ");
} else if (location.tile == TRAP) {
printf("(!)");
} else if (location.tile == TELEPORT) {
printf(" T ");
} else if (location.tile == PLAYER) {
printf(" S ");
} else {
printf(" ? ");
}
}
}
#include <stdio.h>
#include <string.h>
// int argc (argument count)
// char *argv[] -> an array of strings
int main(int argc, char *argv[]) {
if (strcmp(argv[0], "./special_program") == 0) {
printf("Special mode activated\n");
}
printf("There are %d parameter\n", argc);
int i = 0;
while (i < argc) {
fputs(argv[i], stdout);
printf("\n");
i++;
}
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 () {
printf("There are %d command line arguments in this program\n", );
// 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!
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// this is a program that prints out it's command line arguments.
// ./program hello
// print -> hello
// int argc (argument count)
// char *argv[] -> an array of strings
int main(int argc, char *argv[]) {
printf("Doubling %s\n", argv[1]);
int arg_input = atoi(argv[1]);
printf("%d\n", arg_input * 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;
}
struct pokemon {
int HP;
double attack;
};
int battle_two_pokemon(struct pokemon pokemon_a, struct pokemon pokemon_b) {
if ((pokemon_a.HP - pokemon_b.attack) > (pokemon_b.HP - pokemon_a.attack)) {
return 0;
} else if ((pokemon_b.HP - pokemon_a.attack) >
(pokemon_a.HP - pokemon_b.attack)) {
return 1;
} else {
return -1;
}
}
int main(void) {
struct pokemon my_pokemon;
my_pokemon.HP = 50;
my_pokemon.attack = 5.0;
struct pokemon second_pokemon;
second_pokemon.HP = 45;
second_pokemon.attack = 2.5;
battle_two_pokemon(my_pokemon, second_pokemon);
}
// 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>
#include <string.h>
int main() {
// 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 () {
printf("Not enough arguments!");
return 1;
}
// So what steps do we need to do to solve this problem?
/* 1. Initialise variables to track the most frequent argument, to keep
track of the highest frequency and the index of the most frequent arg
2. For each argument (what index should you start with to skip the
program name?)
- count how many times this argument appears among all arguments
- compare this count with the highest count and
- if the current argument appears more times than the previous
most frequent one, update the counters and index
3. After checking all arguments, print the most common argument
and its frequency
*/
printf("Most common term is: %s, which occurs %d times\n", );
return 0;
}
ELF >