Week 5 Lecture Code

// Pantea Aria

// strings - remove_trailing_new_line

#include <stdio.h>
#include <string.h>
void remove_new_line(char input2[]);
int main(void) {
    //char input1[] = "Pantea";
    char input2[10];

    
    
    // fgets a new input2, enter "Pantea"
    if (fgets(input2, 10, stdin) == NULL) {  //Pantea\n
        return 0;
    }

    // then strcmp with "Pantea"
    int result = strcmp(input2, "Pantea");  //Pantea, Pantea
    printf ("strcmp returned %d\n", result);
    // is there any issue?
    // the issue is the \n that I entered after Pantea
    // check if we have a \n at the end of input2 remove it
    remove_new_line(input2);
    printf ("after removing new line, strcmp returns %d\n", strcmp(input2, "Pantea"));
    return 0;
}

// remove_trailing_new_line
void remove_new_line(char input2[]) {
    // check if the last character of input2 is \n
    // let's find the length of the string
    int len = strlen(input2);
    if ((input2[len - 1]) == '\n') {
        input2[len - 1] = '\0';
    }
}
// Pantea Aria

// Array of strings

// write a function that takes an array of names and print them all

#include <stdio.h>

#define MAX_NAMES 3
#define MAX_LEN 10
void print_string(char str[MAX_NAMES][MAX_LEN]);
int main(void) {
    char names[MAX_NAMES][MAX_LEN] = {"pantea", "sara", "alex"};

    // Using ONE index: access a whole string
    printf("Second name: %s\n", names[1]);   // prints "sara"

    // Using TWO indexes: access a single character
    printf("First letter of first name: %c\n", names[0][0]); // prints 'p'
    printf("Third letter of alex: %c\n", names[2][2]);      // prints 'e'
    
    // call function print all names
    print_string(names);
    
    

    return 0;
}
void print_string(char str[MAX_NAMES][MAX_LEN]) {
    int i = 0;
    while (i < MAX_NAMES) {
        printf ("%s\n", str[i]);
        i++;
    }
}
// Pantea Aria

// Command line arguments example

// write a program that calculates the total number of steps walked over several days.
// Each command line argument represents the number of steps for one day.
// Your program should:
// Read the step counts from the command line
// Convert each value from a string to an integer
// Calculate the total number of steps
// Print the final total


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int i = 1;
    int sum = 0;
    while (i < argc) {
        sum = sum + atoi(argv[i]);
        i++;
    }
    printf ("Total steps is %d\n", sum);
    return 0;


}
// Pantea Aria

// strings - remove_trailing_new_line

#include <stdio.h>
#include <string.h>

int main(void) {
    char input1[] = "Pantea";
    char input2[10];

    
    
    // fgets a new input2, enter "Pantea"
    // then strcmp with "Pantea"
    // is there any issue?
    
}

// remove_trailing_new_line
// Pantea Aria

// Array of strings

// write a function that takes an array of names and print them all

#include <stdio.h>

#define MAX_NAMES 3
#define MAX_LEN 10

int main(void) {
    char names[MAX_NAMES][MAX_LEN] = {"pantea", "sara", "alex"};

    // Using ONE index: access a whole string
    printf("Second name: %s\n", names[1]);   // prints "sara"

    // Using TWO indexes: access a single character
    printf("First letter of first name: %c\n", names[0][0]); // prints 'p'
    printf("Third letter of alex: %c\n", names[2][2]);      // prints 'e'
    
    // call function print all names
    
    

    return 0;
}
// Pantea Aria

// Command line arguments example

// write a program that calculates the total number of steps walked over several days.
// Each command line argument represents the number of steps for one day.
// Your program should:
// Read the step counts from the command line
// Convert each value from a string to an integer
// Calculate the total number of steps
// Print the final total


#include <stdio.h>
#include <stdlib.h>

int main(
// Pantea Aria

// Robot Explorer – Space Station Adventure

// A large program to help with assignment 1

// starter code
#include <stdio.h>

// Provided Constants
#define INVALID_INDEX -1
#define MAP_ROWS 8
#define MAP_COLUMNS 8

// User defined constants
#define MOVE_UP 'w'
#define MOVE_LEFT 'a'
#define MOVE_DOWN 's'
#define MOVE_RIGHT 'd'


// Provided enums and structs
enum floor_type {
    SAFE,
    RADIATION
};

enum item_type {
    EMPTY,
    ENERGY
};

struct location {
    enum item_type item;
    enum floor_type floor;
};

// User defined structs
struct robot_data {
    int row;
    int col;
    int energy_count;
    int damage_count;
};


// Provided function prototypes
void initialise_station(struct location station[MAP_ROWS][MAP_COLUMNS]);

void print_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    int robot_row,
    int robot_col,
    int energy_count,
    int damage_count
);

void print_location(struct location location, int floor_layer, int print_robot);
void print_floor_type(enum floor_type floor);
void print_item_type(enum item_type item);

// User defined function prototypes
int is_valid_position(int row, int col);

struct robot_data station_update(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    struct robot_data robot
);

void explore_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    struct robot_data robot
);


int main(void) {
    struct location station[MAP_ROWS][MAP_COLUMNS];
    initialise_station(station);

    print_station(station, INVALID_INDEX, INVALID_INDEX, 0, 0);

    printf("Enter robot starting location: ");

    struct robot_data robot;
    robot.energy_count = 0;
    robot.damage_count = 0;

    if (scanf("%d %d", &robot.row, &robot.col) != 2 ||
        is_valid_position(robot.row, robot.col) == 0) {
        robot.row = 0;
        robot.col = 0;
    }

    robot = station_update(station, robot);

    print_station(
        station,
        robot.row,
        robot.col,
        robot.energy_count,
        robot.damage_count
    );

    printf("\nExplore the station and collect energy cells!\n");
    explore_station(station, robot);

    printf("System shutdown.\n");
    return 0;
}


// Returns 1 if row and col are within map bounds, 0 otherwise
int is_valid_position(int row, int col) {
    int valid = 1;
    if (row < 0 || col < 0 ||
        row >= MAP_ROWS || col >= MAP_COLUMNS) {
        valid = 0;
    }
    return valid;
}


// Updates robot stats based on current location
struct robot_data station_update(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    struct robot_data robot
) {
    // Collect energy cell
    if (station[robot.row][robot.col].item == ENERGY) {
        robot.energy_count++;
        station[robot.row][robot.col].item = EMPTY;
    }

    // Take radiation damage
    if (station[robot.row][robot.col].floor == RADIATION) {
        robot.damage_count++;
    }

    return robot;
}


// Handles robot movement loop
void explore_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    struct robot_data robot
) {
    char move;
    // Should we add more condition here?
    while (scanf(" %c", &move) == 1) {

        if (move == MOVE_UP) {
            robot.row--;
        } else if (move == MOVE_DOWN) {
            robot.row++;
        } else if (move == MOVE_LEFT) {
            robot.col--;
        } else if (move == MOVE_RIGHT) {
            robot.col++;
        }

        robot = station_update(station, robot);

        print_station(
            station,
            robot.row,
            robot.col,
            robot.energy_count,
            robot.damage_count
        );
    }
}


// STARTER FUNCTIONS BELOW THIS POINT //////////////////////////

// Initialises the station with empty items and safe floors
// Hard codes some energy cells and radiation
void initialise_station(struct location station[MAP_ROWS][MAP_COLUMNS]) {
    int row = 0;
    while (row < MAP_ROWS) {
        int col = 0;
        while (col < MAP_COLUMNS) {
            station[row][col].floor = SAFE;
            station[row][col].item = EMPTY;
            col++;
        }
        row++;
    }

    // Hard-coded setup
    station[0][1].item = ENERGY;
    station[1][1].item = ENERGY;
    station[7][7].item = ENERGY;
    station[5][1].item = ENERGY;

    station[0][1].floor = RADIATION;
    station[7][6].floor = RADIATION;
    station[0][2].floor = RADIATION;
    station[0][3].floor = RADIATION;
}


// Prints the station map and game statistics
void print_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    int robot_row,
    int robot_col,
    int energy_count,
    int damage_count
) {
    printf("==== Robot Explorer ====\n");
    printf("========================\n");

    for (int row = 0; row < MAP_ROWS * 2; row++) {
        for (int col = 0; col < MAP_COLUMNS; col++) {
            int print_robot = 0;
            if (robot_row == row / 2 && robot_col == col) {
                print_robot = 1;
            }
            print_location(
                station[row / 2][col],
                row % 2,
                print_robot
            );
        }
        printf("\n");
    }

    printf("========================\n");
    printf("Energy: %2d   Damage: %2d\n",
           energy_count, damage_count);
    printf("========================\n\n");
}


// Prints a single location
void print_location(
    struct location location,
    int floor_print,
    int print_robot
) {
    if (floor_print) {
        print_floor_type(location.floor);
    } else {
        if (print_robot) {
            printf("(R)");
        } else {
            print_item_type(location.item);
        }
    }
}


// Prints the floor type
void print_floor_type(enum floor_type floor) {
    if (floor == SAFE) {
        printf(" . ");
    } else if (floor == RADIATION) {
        printf(" X ");
    } else {
        printf(" ? ");
    }
}


// Prints the item type
void print_item_type(enum item_type item) {
    if (item == EMPTY) {
        printf("   ");
    } else if (item == ENERGY) {
        printf(" E ");
    } else {
        printf(" ? ");
    }
}
// Pantea Aria

// Robot Explorer – Space Station Adventure

// A large program to help with assignment 1

// starter code

#include <stdio.h>

// Provided Constants
#define INVALID_INDEX -1
#define MAP_ROWS 8
#define MAP_COLUMNS 8

// User defined constants go here


// Provided enums and structs
enum floor_type {
    SAFE,
    RADIATION
};

enum item_type {
    EMPTY,
    ENERGY
};

struct location {
    enum item_type item;
    enum floor_type floor;
};

// User defined enums and structs


// Provided function prototypes
void initialise_station(struct location station[MAP_ROWS][MAP_COLUMNS]);

void print_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    int robot_row,
    int robot_col,
    int energy_count,
    int damage_count
);

void print_location(struct location location, int floor_layer, int print_robot);
void print_floor_type(enum floor_type floor);
void print_item_type(enum item_type item);

// User defined function prototypes


int main(void) {
    struct location station[MAP_ROWS][MAP_COLUMNS];
    initialise_station(station);

    print_station(station, INVALID_INDEX, INVALID_INDEX, 0, 0);

    printf("Enter robot starting location: ");
    // TODO: scan in starting position
    

    printf("\nExplore the station and collect energy cells!\n");
    // TODO: movement loop

    printf("System shutting down...\n");
    return 0;
}


// STARTER FUNCTIONS BELOW THIS POINT //////////////////////////

// Initialises the station with empty items and safe floors
// Hard codes some energy cells and radiation
void initialise_station(struct location station[MAP_ROWS][MAP_COLUMNS]) {
    int row = 0;
    while (row < MAP_ROWS) {
        int col = 0;
        while (col < MAP_COLUMNS) {
            station[row][col].floor = SAFE;
            station[row][col].item = EMPTY;
            col++;
        }
        row++;
    }

    // Hard-coded items and hazards
    station[0][1].item = ENERGY;
    station[1][1].item = ENERGY;
    station[7][7].item = ENERGY;
    station[5][1].item = ENERGY;

    station[0][1].floor = RADIATION;
    station[7][6].floor = RADIATION;
    station[0][2].floor = RADIATION;
    station[0][3].floor = RADIATION;
}


// Prints the station map and game statistics
// Each row prints two lines:
// top line shows robot or item
// bottom line shows floor type
void print_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    int robot_row,
    int robot_col,
    int energy_count,
    int damage_count
) {
    printf("==== Robot Explorer ====\n");
    printf("========================\n");

    for (int row = 0; row < MAP_ROWS * 2; row++) {
        for (int col = 0; col < MAP_COLUMNS; col++) {
            int print_robot = 0;
            if (robot_row == row / 2 && robot_col == col) {
                print_robot = 1;
            }
            print_location(station[row / 2][col], row % 2, print_robot);
        }
        printf("\n");
    }

    printf("========================\n");
    printf("Energy: %2d   Damage: %2d\n", energy_count, damage_count);
    printf("========================\n\n");
}


// Prints a single location (used by print_station)
void print_location(
    struct location location,
    int floor_print,
    int print_robot
) {
    if (floor_print) {
        print_floor_type(location.floor);
    } else {
        if (print_robot) {
            printf("(R)");
        } else {
            print_item_type(location.item);
        }
    }
}


// Prints the floor type
void print_floor_type(enum floor_type floor) {
    if (floor == SAFE) {
        printf(" . ");
    } else if (floor == RADIATION) {
        printf(" X ");
    } else {
        printf(" ? ");
    }
}


// Prints the item type
void print_item_type(enum item_type item) {
    if (item == EMPTY) {
        printf("   ");
    } else if (item == ENERGY) {
        printf(" E ");
    } else {
        printf(" ? ");
    }
}
// Pantea Aria

// Robot Explorer – Space Station Adventure

// A large program to help with assignment 1

// starter code
#include <stdio.h>

// Provided Constants
#define INVALID_INDEX -1
#define MAP_ROWS 8
#define MAP_COLUMNS 8

// User defined constants
#define MOVE_UP 'w'
#define MOVE_LEFT 'a'
#define MOVE_DOWN 's'
#define MOVE_RIGHT 'd'


// Provided enums and structs
enum floor_type {
    SAFE,
    RADIATION
};

enum item_type {
    EMPTY,
    ENERGY
};

struct location {
    enum item_type item;
    enum floor_type floor;
};

// User defined structs
struct robot_data {
    int row;
    int col;
    int energy_count;
    int damage_count;
};


// Provided function prototypes
void initialise_station(struct location station[MAP_ROWS][MAP_COLUMNS]);

void print_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    int robot_row,
    int robot_col,
    int energy_count,
    int damage_count
);

void print_location(struct location location, int floor_layer, int print_robot);
void print_floor_type(enum floor_type floor);
void print_item_type(enum item_type item);

// User defined function prototypes
int is_valid_position(int row, int col);

struct robot_data station_update(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    struct robot_data robot
);

void explore_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    struct robot_data robot
);


int main(void) {
    struct location station[MAP_ROWS][MAP_COLUMNS];
    initialise_station(station);

    print_station(station, INVALID_INDEX, INVALID_INDEX, 0, 0);

    printf("Enter robot starting location: ");

    struct robot_data robot;
    robot.energy_count = 0;
    robot.damage_count = 0;

    if (scanf("%d %d", &robot.row, &robot.col) != 2 ||
        is_valid_position(robot.row, robot.col) == 0) {
        robot.row = 0;
        robot.col = 0;
    }

    robot = station_update(station, robot);

    print_station(
        station,
        robot.row,
        robot.col,
        robot.energy_count,
        robot.damage_count
    );

    printf("\nExplore the station and collect energy cells!\n");
    explore_station(station, robot);

    printf("System shutdown.\n");
    return 0;
}


// Returns 1 if row and col are within map bounds, 0 otherwise
int is_valid_position(int row, int col) {
    int valid = 1;
    if (row < 0 || col < 0 ||
        row >= MAP_ROWS || col >= MAP_COLUMNS) {
        valid = 0;
    }
    return valid;
}


// Updates robot stats based on current location
struct robot_data station_update(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    struct robot_data robot
) {
    // Collect energy cell
    if (station[robot.row][robot.col].item == ENERGY) {
        robot.energy_count++;
        station[robot.row][robot.col].item = EMPTY;
    }

    // Take radiation damage
    if (station[robot.row][robot.col].floor == RADIATION) {
        robot.damage_count++;
    }

    return robot;
}


// Handles robot movement loop
void explore_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    struct robot_data robot
) {
    char move;
    // Should we add more condition here?
    while (scanf(" %c", &move) == 1) {

        if (move == MOVE_UP) {
            robot.row--;
        } else if (move == MOVE_DOWN) {
            robot.row++;
        } else if (move == MOVE_LEFT) {
            robot.col--;
        } else if (move == MOVE_RIGHT) {
            robot.col++;
        }

        robot = station_update(station, robot);

        print_station(
            station,
            robot.row,
            robot.col,
            robot.energy_count,
            robot.damage_count
        );
    }
}


// STARTER FUNCTIONS BELOW THIS POINT //////////////////////////

// Initialises the station with empty items and safe floors
// Hard codes some energy cells and radiation
void initialise_station(struct location station[MAP_ROWS][MAP_COLUMNS]) {
    int row = 0;
    while (row < MAP_ROWS) {
        int col = 0;
        while (col < MAP_COLUMNS) {
            station[row][col].floor = SAFE;
            station[row][col].item = EMPTY;
            col++;
        }
        row++;
    }

    // Hard-coded setup
    station[0][1].item = ENERGY;
    station[1][1].item = ENERGY;
    station[7][7].item = ENERGY;
    station[5][1].item = ENERGY;

    station[0][1].floor = RADIATION;
    station[7][6].floor = RADIATION;
    station[0][2].floor = RADIATION;
    station[0][3].floor = RADIATION;
}


// Prints the station map and game statistics
void print_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    int robot_row,
    int robot_col,
    int energy_count,
    int damage_count
) {
    printf("==== Robot Explorer ====\n");
    printf("========================\n");

    for (int row = 0; row < MAP_ROWS * 2; row++) {
        for (int col = 0; col < MAP_COLUMNS; col++) {
            int print_robot = 0;
            if (robot_row == row / 2 && robot_col == col) {
                print_robot = 1;
            }
            print_location(
                station[row / 2][col],
                row % 2,
                print_robot
            );
        }
        printf("\n");
    }

    printf("========================\n");
    printf("Energy: %2d   Damage: %2d\n",
           energy_count, damage_count);
    printf("========================\n\n");
}


// Prints a single location
void print_location(
    struct location location,
    int floor_print,
    int print_robot
) {
    if (floor_print) {
        print_floor_type(location.floor);
    } else {
        if (print_robot) {
            printf("(R)");
        } else {
            print_item_type(location.item);
        }
    }
}


// Prints the floor type
void print_floor_type(enum floor_type floor) {
    if (floor == SAFE) {
        printf(" . ");
    } else if (floor == RADIATION) {
        printf(" X ");
    } else {
        printf(" ? ");
    }
}


// Prints the item type
void print_item_type(enum item_type item) {
    if (item == EMPTY) {
        printf("   ");
    } else if (item == ENERGY) {
        printf(" E ");
    } else {
        printf(" ? ");
    }
}
// Pantea Aria

// Robot Explorer – Space Station Adventure

// A large program to help with assignment 1

// starter code

#include <stdio.h>

// Provided Constants
#define INVALID_INDEX -1
#define MAP_ROWS 8
#define MAP_COLUMNS 8

// User defined constants go here


// Provided enums and structs
enum floor_type {
    SAFE,
    RADIATION
};

enum item_type {
    EMPTY,
    ENERGY
};

struct location {
    enum item_type item;
    enum floor_type floor;
};

// User defined enums and structs


// Provided function prototypes
void initialise_station(struct location station[MAP_ROWS][MAP_COLUMNS]);

void print_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    int robot_row,
    int robot_col,
    int energy_count,
    int damage_count
);

void print_location(struct location location, int floor_layer, int print_robot);
void print_floor_type(enum floor_type floor);
void print_item_type(enum item_type item);

// User defined function prototypes


int main(void) {
    struct location station[MAP_ROWS][MAP_COLUMNS];
    initialise_station(station);

    print_station(station, INVALID_INDEX, INVALID_INDEX, 0, 0);

    printf("Enter robot starting location: ");
    // TODO: scan in starting position
    

    printf("\nExplore the station and collect energy cells!\n");
    // TODO: movement loop

    printf("System shutting down...\n");
    return 0;
}


// STARTER FUNCTIONS BELOW THIS POINT //////////////////////////

// Initialises the station with empty items and safe floors
// Hard codes some energy cells and radiation
void initialise_station(struct location station[MAP_ROWS][MAP_COLUMNS]) {
    int row = 0;
    while (row < MAP_ROWS) {
        int col = 0;
        while (col < MAP_COLUMNS) {
            station[row][col].floor = SAFE;
            station[row][col].item = EMPTY;
            col++;
        }
        row++;
    }

    // Hard-coded items and hazards
    station[0][1].item = ENERGY;
    station[1][1].item = ENERGY;
    station[7][7].item = ENERGY;
    station[5][1].item = ENERGY;

    station[0][1].floor = RADIATION;
    station[7][6].floor = RADIATION;
    station[0][2].floor = RADIATION;
    station[0][3].floor = RADIATION;
}


// Prints the station map and game statistics
// Each row prints two lines:
// top line shows robot or item
// bottom line shows floor type
void print_station(
    struct location station[MAP_ROWS][MAP_COLUMNS],
    int robot_row,
    int robot_col,
    int energy_count,
    int damage_count
) {
    printf("==== Robot Explorer ====\n");
    printf("========================\n");

    for (int row = 0; row < MAP_ROWS * 2; row++) {
        for (int col = 0; col < MAP_COLUMNS; col++) {
            int print_robot = 0;
            if (robot_row == row / 2 && robot_col == col) {
                print_robot = 1;
            }
            print_location(station[row / 2][col], row % 2, print_robot);
        }
        printf("\n");
    }

    printf("========================\n");
    printf("Energy: %2d   Damage: %2d\n", energy_count, damage_count);
    printf("========================\n\n");
}


// Prints a single location (used by print_station)
void print_location(
    struct location location,
    int floor_print,
    int print_robot
) {
    if (floor_print) {
        print_floor_type(location.floor);
    } else {
        if (print_robot) {
            printf("(R)");
        } else {
            print_item_type(location.item);
        }
    }
}


// Prints the floor type
void print_floor_type(enum floor_type floor) {
    if (floor == SAFE) {
        printf(" . ");
    } else if (floor == RADIATION) {
        printf(" X ");
    } else {
        printf(" ? ");
    }
}


// Prints the item type
void print_item_type(enum item_type item) {
    if (item == EMPTY) {
        printf("   ");
    } else if (item == ENERGY) {
        printf(" E ");
    } else {
        printf(" ? ");
    }
}