Week 4 Code Examples
#include <stdio.h>
// a program which creates a 2d array
// assigns values to each element
// prints out the 2d array to visualise how it stores data
#define ROWS 5
#define COLS 5
// forward declare the print function
void print_grid(int grid_to_print[ROWS][COLS]);
// returns nothing, it just prints
void print_grid(int grid_to_print[ROWS][COLS]) {
printf("Visualising the 2d array\n\n");
int row = 0;
// this will loop through each ROW
while (row < ROWS) {
// once we get to the row, we can loop through each COL
int col = 0;
while (col < COLS) {
// we will loop through each row/col
printf("%-3d", grid_to_print[row][col]);
col++;
}
row++;
printf("\n");
}
}
int main(void) {
// a 2d array of ints 5x5
// we still want to 0-out our 2d array
int grid[ROWS][COLS] = {};
//Put some data into our 2d array
grid[1][1] = 5;
grid[0][4] = 1;
grid[4][0] = 2;
print_grid(grid);
return 0;
}
#include <stdio.h>
#define SIZE 3
// good activity is to add a string here (student's name)
struct student_record {
int age;
double WAM;
int abscences;
};
void print_student(struct student_record student_to_print) {
printf("Printing student\n");
printf("Student is %d years old\n", student_to_print.age);
printf("Student has a %lf WAM\n", student_to_print.WAM);
printf("Student was absent %d times\n", student_to_print.abscences);
printf("\n\n");
}
int main(void) {
// create a single student_record named student_1
// struct student_record student_1;
// array of structs
// array it's <type> <name>[SIZE]
struct student_record students[SIZE] = {};
int i = 0;
while (i < SIZE) {
students[i].age = 25;
students[i].WAM = 80;
students[i].abscences = 0;
i++;
}
i = 0;
while (i < SIZE) {
// we pass each individual struct value to the print function
print_student(students[i]);
i++;
}
return 0;
}
#include <stdio.h>
#include <string.h>
// 49 real elements + 1 Null terminating character
#define MAX_LEN 50
int main(void) {
char first_name[MAX_LEN] = "Jake";
char last_name[MAX_LEN] = "Renzella";
char full_name[MAX_LEN] = "";
strcat(full_name, first_name);
strcat(full_name, " ");
strcat(full_name, last_name);
fputs(full_name, stdout);
return 0;
}
#include <stdio.h>
#include <string.h>
// 49 real elements + 1 Null terminating character
#define MAX_LEN 50
int main(void) {
char first_name[MAX_LEN] = "Jake";
char last_name[MAX_LEN] = "Renzella";
printf("%d\n", strcmp(first_name, "Kale"));
return 0;
}
#include <stdio.h>
#include <string.h>
// 49 real elements + 1 Null terminating character
#define MAX_LEN 50
int main(void) {
char name[MAX_LEN] = "Jake";
strcpy(name, "Bri");
fputs(name, stdout);
printf("\n");
printf("Enter your name: ");
fgets(name, MAX_LEN, stdin);
fputs(name, stdout);
return 0;
}
#include <stdio.h>
// I know my string will never need to be more than 15 chars
#define MAX_LENGTH 15
int main(void) {
char name[MAX_LENGTH];
printf("Welcome to Jakes computer\n");
printf("If your name is Jake you will get a welcome message\n");
printf("Else you'll get told to leave\n\n\n");
printf("Enter your name: ");
// fgets reads the entire string, including the newline character
while (fgets(name, MAX_LENGTH, stdin) != NULL) {
fputs(name, stdout);
// if name is something, do something
// else do something
printf("Enter your name: ");
}
return 0;
}
#include <stdio.h>
#include <string.h>
// 49 real elements + 1 Null terminating character
#define MAX_LEN 50
int main(void) {
char first_name[MAX_LEN] = "Jake";
//strlen returns count of characters before \0
// this is 4
printf("Length of first_name is: %d\n", strlen(first_name));
printf("Enter your name: ");
// it stores \n in the array
fgets(first_name, MAX_LEN, stdin);
// strlen is returning +1
printf("Length of first_name is: %d\n", strlen(first_name));
return 0;
}
#include <stdio.h>
int main(void) {
int data[7] = {};
int i = 0;
while(i < 7) {
printf("value at %d is: %d\n", i, data[i]);
i++;
}
return 0;
}
#include <stdio.h>
#include <string.h> // gets us fgets
#define ROWS 5
#define COLS 5
#define MAX_ROOM_NAME_LEN 50
struct room {
char name[MAX_ROOM_NAME_LEN];
int ocupants;
// 4 ints = 0 == wall
// 1 == door
int west;
int north;
int south;
int east;
};
struct room new_room(void) {
struct room new_room;
printf("Create a new room\n");
printf("What's its name?\n");
fgets(new_room.name, MAX_ROOM_NAME_LEN, stdin);
printf("\n");
printf("How many occupants?: ");
scanf("%d", &new_room.ocupants);
return new_room;
}
void print_building(struct room building_to_print[ROWS][COLS]) {
printf("\nBuilding layout:\n");
int row = 0;
while (row < ROWS) {
int col = 0;
while (col < ROWS) {
// print top wall!
printf("%c", building_to_print[row][col].north);
col++;
}
row++;
printf("\n");
}
}
int main(void) {
// creating a 2d array of a struct room
struct room building[ROWS][COLS] = {};
// let the user build out 5 rooms
// always start at bottom center
// ask the user for details and where the room leads
int current_row = ROWS - 1; // this is the bottom row
int current_col =
COLS / 2; // ints round to nearest floor, odd cols so this works...
int i = 0;
while (i < 1) {
printf("Welcome to room builder\n");
printf("Which direction would you like to go?\n");
printf("n is NORTH, e is EAST, w is WEST, s is SOUTH\n");
// scan the users choice into a char
char direction;
scanf(" %c ", &direction);
int next_row = current_row;
int next_col = current_col;
if (direction == 'n') {
// we need to know the NEXT ROOM, and the CURRENT ONE
next_row--;
// create a room at the new position
// this is not validated (could be an impossible room?)
building[next_row][next_col] = new_room();
building[current_row][current_col].north = 1;
building[next_row][next_col].south = 1;
} else if (direction == 'e') {
} else if (direction == 's') {
} else if (direction == 'w') {
}
i++;
}
print_building(building);
return 0;
}