version: 1.03 last updated: 2023-15-04 21:20

Assignment 2 - Carriage Simulator

Welcome to CS: Carriage Simulator (or CS_CS for short)!

In order to address complaints about Light Rail, Transport NSW has hired you to make a program that can design and test train layouts, with the end goal being to increase passenger happiness.

Overview

Assignment Structure

This assignment will test your ability to create, use, manipulate and solve problems using linked lists. To do this, you'll be implementing a train as a linked list of carriages.

The following struct definition for a single carriage is provided for you in the starter code:

// A Train Carriage
struct carriage {
    // carriage id in the form "N1002", unique, null terminated
    char carriage_id[ID_SIZE];
    //  PASSENGER, BUFFET, RESTROOM, FIRST_CLASS
    enum carriage_type type;
    // Maximum number of passengers 
    int capacity;
    // Current number of passengers
    int occupancy;

    struct carriage *next;
};

Purpose:

  • Represents a single carriage, which can be connected to other carriages to form a train.
  • Stores information about the carriage and its passengers.

Fields:

  • char carriage_id[ID_SIZE]

    • The carriage's unique id, in the form of a null-terminated string of length 5.
  • enum carriage_type type

    • The type of carriage as an enum.
    • Can be either PASSENGER, BUFFET, RESTROOM, or FIRST_CLASS.
  • int capacity

    • The maximum number of passengers that can fit inside the carriage.
  • int occupancy

    • The number of passengers currently seated in the carriage. occupancy must always be less than or equal to the capacity.
    • Note that there is no standing capacity. We'll assume for this assignment that all passengers have a seat to themselves.
  • struct task *next

    • The pointer to the next carriage in the train.

You may modify this struct if you wish, but you are not required to.

The following enum definition is also provided for you:

// The types of train carriages
enum carriage_type {INVALID_TYPE, PASSENGER, BUFFET, RESTROOM, FIRST_CLASS};

Purpose:

  • Represents the different types of train carriages that we'll be dealing with in this assignment.

Values:

  • PASSENGER

    • A regular passenger carriage.
  • BUFFET

    • A train carriage with a built in restaurant.
    • Sure to improve the travel experience of hungry passengers!
  • RESTROOM

    • A carriage equipped with restroom facilities.
    • Certainly good to have on a train, but maybe not the most pleasant to sit near.
  • FIRST_CLASS

    • The most luxurious travel experience NSW has to offer.
    • Fully equipped with all the amenities a passenger could ask for.
  • INVALID_TYPE

    • Not an actual train carriage type.
    • This usually represents if some error has occured when trying to scan in enum carriage_types.

From Stage 3 onwards, you will need to use an additional struct type: struct train. This is further explained in Stage 3, and you won't need to worry about it until then.

Your tasks:

This assignment consists of four stages. Each stage builds on the work of the previous stage, and each stage has a higher complexity than its predecessor. You should complete the stages in order.

  • Stage 1.1 - Basic setup: Implement code to create a single struct carriage.

  • Stage 1.2 - Setup Command loop: Implement the Command loop.

    In this stage, you will be setting up the program to read commands continuously until CTRL-D (similar to Assignment 1).

    Each command will start with a single unique character, and depending on the command, may be followed by some specific number of arguments relevant to that command.

  • Stage 1.3 onwards - Implement the commands:

    Your bosses at Transport NSW have asked you to implement some different commands, which will allow them to create, modify and examine train designs.

How To Get Started

There are a few steps to getting started with Carriage Simulator.

  1. Create a new folder for your assignment work and move into it.
mkdir ass2
cd ass2
  1. Download the starter code (cs_cs.c) here or use this command on your CSE account to copy the file into your current directory:
cp -n /web/cs1511/23T1/activities/cs_cs/cs_cs.c .
  1. Run 1511 autotest cs_cs to make sure you have correctly downloaded the file.
1511 autotest cs_cs
  1. Read through Stage 1.
  1. Think about your solution, draw some diagrams to help you get started.

  2. Start coding!

Reference Implementation

To help you understand the proper behaviour of the Carriage Simulator, we have provided a reference implementation. If you have any questions about the behaviour of your assignment, you can check and compare it to the reference implementation.

To run the reference implementation, use the following command:

 1511 cs_cs

You might want to start by running the ? command:

  1511 cs_cs 
Enter command: ?
=====================[ Carriage Simulator ]=====================
      ===============[     Usage Info     ]===============      
  a [carriage_id] [type] [capacity]                             
    Add a carriage to the train                                 
  p                                                             
    Print out all of the carriages in the train                 
  i [n] [carriage_id] [type] [capacity]                         
    Insert a carriage into the train at position `n`            
                                                                
  s [carriage_id] [n]                                           
    Seat `n` passengers onto the train starting from carriage   
    `carriage_id`                                               
  d [carriage_id] [n]                                           
    Remove `n` passengers from carriage `carriage_id`           
  T                                                             
    Display the total number of passengers and empty seats on   
    the train                                                   
  c [start_id] [end_id]                                         
    Display the number of passengers and empty seats on between 
    carriage `start_id` and carriage `end_id`                   
  m [source_id] [destination_id] [n]                            
    Move `n` passengers from one carrige to another, without    
    kicking anyone off the train.                               
  h [carriage_id]                                               
    Display the happiness of passengers in carriage             
    `carriage_id`                                               
  H                                                             
    Display the average happiness of all passengers on the train
                                                                
  N                                                             
    Create a new empty train                                    
  >                                                             
    Select the next train in the train list.                    
  <                                                             
    Select the previous train in the train list.                
  P                                                             
    Display the train list.                                     
  r [carriage_id]                                               
    Remove carriage `carriage_id` from the selected train.      
  R                                                             
    Remove the selected train.                                  
                                                                
  M                                                             
    Merge the selected train with the train after it.           
  S [n]                                                         
    Split the current train into smaller trains.                
  O                                                             
    Rearrange passengers on the selected train to optimise      
    happiness.                                                  
  ?                                                             
    Show help                                                   
================================================================

Starter code:

The starter code contains some provided functions to help simplify some stages of the assignment. These functions have been fully implemented for you and should not need to be modified to complete the assignment.

These provided functions will be explained in the relevant stages of the assignment. 
Please read the comments and the spec as we will suggest certain provided functions for you to use.

It also contains one stub function, create_carriage, which you will need to complete in Stage 1.1.

Finally, the main function contains some comments to help guide you through Stage 1.1 and Stage 1.2, as well as some printf messages which run when the program starts and ends.

On program start:

Welcome to Carriage Simulator
All aboard!

On program end:

Goodbye

Allowed C Features

In this assignment, you cannot use arrays, other than char arrays, and cannot use the features explicitly banned in the Style Guide.

We strongly encourage you to complete the assessment using only features taught in lectures up to and including Weeks 8 and 9. The only C features you will need to get full marks in the assignment are:

  • intchar, and double variables.
  • Enums.
  • Structs.
  • If statements.
  • While and for loops.
  • Your own functions.
  • Pointers.
  • char arrays/strings (you are not allowed to use arrays that are not char arrays).
  • Linked lists.
  • Standard libraries: stdio.hstdlib.h, and string.h.
  • Good Code Style!
    (Header comments, function comments, constants (#define's), and whitespace and indentation.)

Using any other features will not increase your marks (and will make it more likely you make style mistakes that cost you marks).

If you choose to disregard this advice, you must still follow the Style Guide. You also may be unable to get help from course staff if you use features not taught in COMP1511.

Features that the Style Guide strongly discourages or bans will be penalised during marking.

Stage 1

For Stage 1 of this assignment, you will be implementing the command loop, as well as the commands to add carriages to a train.

Specifically, this will include:

  • Implementing the create_carriage function.
  • Implementing the command loop, to scan commands until CTRL-D.
  • Adding Carriages to the end of a train.
  • Printing out all carriages in the train.
  • Checking that a carriage is valid.

By the end of this stage, your linked list of carriages will look something like:

Stage 1: A linked list of carriages

Stage 1.1 - Creating a carriage

As you've probably found out by now, it can be really handy to have a function that mallocs, initialises, and returns a single linked list node.
So in Stage 1.1, we will be implementing a function which does exactly that for a struct carriage.

You'll find the following unimplemented function in the starter code:

struct carriage *create_carriage(
    char id[ID_SIZE], 
    enum carriage_type type,
    int capacity
) {
    // STAGE 1.1
    // TODO: malloc, initialise, and return a new carriage.
    
    // hint: you will have to replace NULL in this return statement.
    return NULL; 
}

Your task is to complete this function, so that it:

  1. Malloc's a new struct carriage.
  2. Copies the id , type and capacity into the corresponding struct fields.
  3. Initialises all other fields to some reasonable value.
  4. Returns the struct.

Assumptions

  • char id[ID_SIZE] will always be 5 or less characters long (with a null terminator at the end).
  • No error handling is required for this stage.

Testing

There are no autotests for Stage 1.1.

Instead, you may want to double check your work by compiling your code using dcc and make sure there are no warnings or errors.

You could also write some temporary testing code to check that your create_carriage function works properly when it runs.

For example, you could copy the following testing code into your main function:

///////////////////////////// TESTING CODE /////////////////////////////

// create a struct carriage with 
//      id       : "N1001"
//      type     : PASSENGER
//      capacity : 20
struct carriage *test_carriage = create_carriage("N1001", PASSENGER, 20);

// print out all it's fields.
printf("carriage_id: %s\n", test_carriage->carriage_id);
printf("capacity: %d\n", test_carriage->capacity);
printf("occupancy: %d\n", test_carriage->occupancy);
printf("next field: %p\n", test_carriage->next);
if (test_carriage->type == PASSENGER) {
    printf("type: passenger\n");
} else {
    printf("not a passenger carriage\n");
}

///////////////////////////// TESTING CODE /////////////////////////////

This code just calls create_carriage to malloc and initialise a struct carriage, and then prints out all of its fields.

If you run it, it should print out something like:

carriage_id: N1001
capacity: 20
occupancy: 0
next field: (nil)
type: passenger

Stage 1.2 - Implement the Command Loop

Now we'll be implementing the command loop, allowing your program to take in and perform different operations on the train.

From this stage onwards, your program should run in a loop, scanning in and executing commands until CTRL-D. You should implement this command loop between the welcome and goodbye messages, so that your program runs in the following order:

  1. First, print out the welcome message (the printf statement is included in the starter code).

  2. Then run in a loop until CTRL-D, and:

    1. Print out the prompt Enter command:;
    2. Scan in a command character;
    3. Scan in any arguments following the command character and execute the command.
  3. After CTRL-D is pressed, the goodbye message should be printed (the printf statement is included in the starter code), and then your program should end.

Each command will start with a single unique character, and may be followed by a variable number of arguments depending on the command.
The unique character for each different command and the number and type of the command arguments are specified in the relevant stages of this assignment.

The first command you have to implement is the Help command, which is described below.

Command: Help

?

Description

When the character '?' is entered into your program, it should run the Help command. This command doesn't read in any arguments following the initial command and should display a message which lists the different commands and their arguments.

The purpose of this command is to allow anyone using our program to easily look up the different commands.

A helper function: void print_usage(void) has been provided to help you print and format this message.
You can find more information about it here: Provided helper function.

This is what it should look like when you run the Help command:

Enter command: ?
=====================[ Carriage Simulator ]=====================
      ===============[     Usage Info     ]===============      
  a [carriage_id] [type] [capacity]                             
    Add a carriage to the train                                 
  p                                                             
    Print out all of the carriages in the train                 
  i [n] [carriage_id] [type] [capacity]                         
    Insert a carriage into the train at position `n`            
                                                                
  s [carriage_id] [n]                                           
    Seat `n` passengers onto the train starting from carriage   
    `carriage_id`                                               
  d [carriage_id] [n]                                           
    Remove `n` passengers from carriage `carriage_id`           
  T                                                             
    Display the total number of passengers and empty seats on   
    the train                                                   
  c [start_id] [end_id]                                         
    Display the number of passengers and empty seats on between 
    carriage `start_id` and carriage `end_id`                   
  m [source_id] [destination_id] [n]                            
    Move `n` passengers from one carrige to another, without    
    kicking anyone off the train.                               
  h [carriage_id]                                               
    Display the happiness of passengers in carriage             
    `carriage_id`                                               
  H                                                             
    Display the average happiness of all passengers on the train
                                                                
  N                                                             
    Create a new empty train                                    
  >                                                             
    Select the next train in the train list.                    
  <                                                             
    Select the previous train in the train list.                
  P                                                             
    Display the train list.                                     
  r [carriage_id]                                               
    Remove carriage `carriage_id` from the selected train.      
  R                                                             
    Remove the selected train.                                  
                                                                
  M                                                             
    Merge the selected train with the train after it.           
  S [n]                                                         
    Split the current train into smaller trains.                
  O                                                             
    Rearrange passengers on the selected train to optimise      
    happiness.                                                  
  ?                                                             
    Show help                                                   
================================================================

Because of the command loop, after the Help command has finished running, your program should print out

Enter command: 

and then wait for the user to either enter another command, or press CTRL-D to end the program.

Provided helper functions

A procedure,print_usage, has been provided for you in the starter code which prints out the help message.

So all you need to do for this command is call the print_usage procedure when the ? character is scanned in.

Assumptions

  • All commands will begin with a single unique character.
    Some will be followed by a number of arguments, depending on the command.
  • You can assume that commands will always be given the correct number of arguments, so you do NOT need to check for this.
  • You do not need to handle unknown commands.
  • No error handling is required for this stage.

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command:  ? 
=====================[ Carriage Simulator ]=====================
      ===============[     Usage Info     ]===============      
  a [carriage_id] [type] [capacity]                             
    Add a carriage to the train                                 
  p                                                             
    Print out all of the carriages in the train                 
  i [n] [carriage_id] [type] [capacity]                         
    Insert a carriage into the train at position `n`            
                                                                
  s [carriage_id] [n]                                           
    Seat `n` passengers onto the train starting from carriage   
    `carriage_id`                                               
  d [carriage_id] [n]                                           
    Remove `n` passengers from carriage `carriage_id`           
  T                                                             
    Display the total number of passengers and empty seats on   
    the train                                                   
  c [start_id] [end_id]                                         
    Display the number of passengers and empty seats on between 
    carriage `start_id` and carriage `end_id`                   
  m [source_id] [destination_id] [n]                            
    Move `n` passengers from one carrige to another, without    
    kicking anyone off the train.                               
  h [carriage_id]                                               
    Display the happiness of passengers in carriage             
    `carriage_id`                                               
  H                                                             
    Display the average happiness of all passengers on the train
                                                                
  N                                                             
    Create a new empty train                                    
  >                                                             
    Select the next train in the train list.                    
  <                                                             
    Select the previous train in the train list.                
  P                                                             
    Display the train list.                                     
  r [carriage_id]                                               
    Remove carriage `carriage_id` from the selected train.      
  R                                                             
    Remove the selected train.                                  
                                                                
  M                                                             
    Merge the selected train with the train after it.           
  S [n]                                                         
    Split the current train into smaller trains.                
  O                                                             
    Rearrange passengers on the selected train to optimise      
    happiness.                                                  
  ?                                                             
    Show help                                                   
================================================================
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: ?
=====================[ Carriage Simulator ]=====================
      ===============[     Usage Info     ]===============      
  a [carriage_id] [type] [capacity]                             
    Add a carriage to the train                                 
  p                                                             
    Print out all of the carriages in the train                 
  i [n] [carriage_id] [type] [capacity]                         
    Insert a carriage into the train at position `n`            
                                                                
  s [carriage_id] [n]                                           
    Seat `n` passengers onto the train starting from carriage   
    `carriage_id`                                               
  d [carriage_id] [n]                                           
    Remove `n` passengers from carriage `carriage_id`           
  T                                                             
    Display the total number of passengers and empty seats on   
    the train                                                   
  c [start_id] [end_id]                                         
    Display the number of passengers and empty seats on between 
    carriage `start_id` and carriage `end_id`                   
  m [source_id] [destination_id] [n]                            
    Move `n` passengers from one carrige to another, without    
    kicking anyone off the train.                               
  h [carriage_id]                                               
    Display the happiness of passengers in carriage             
    `carriage_id`                                               
  H                                                             
    Display the average happiness of all passengers on the train
                                                                
  N                                                             
    Create a new empty train                                    
  >                                                             
    Select the next train in the train list.                    
  <                                                             
    Select the previous train in the train list.                
  P                                                             
    Display the train list.                                     
  r [carriage_id]                                               
    Remove carriage `carriage_id` from the selected train.      
  R                                                             
    Remove the selected train.                                  
                                                                
  M                                                             
    Merge the selected train with the train after it.           
  S [n]                                                         
    Split the current train into smaller trains.                
  O                                                             
    Rearrange passengers on the selected train to optimise      
    happiness.                                                  
  ?                                                             
    Show help                                                   
================================================================
Enter command: ?
=====================[ Carriage Simulator ]=====================
      ===============[     Usage Info     ]===============      
  a [carriage_id] [type] [capacity]                             
    Add a carriage to the train                                 
  p                                                             
    Print out all of the carriages in the train                 
  i [n] [carriage_id] [type] [capacity]                         
    Insert a carriage into the train at position `n`            
                                                                
  s [carriage_id] [n]                                           
    Seat `n` passengers onto the train starting from carriage   
    `carriage_id`                                               
  d [carriage_id] [n]                                           
    Remove `n` passengers from carriage `carriage_id`           
  T                                                             
    Display the total number of passengers and empty seats on   
    the train                                                   
  c [start_id] [end_id]                                         
    Display the number of passengers and empty seats on between 
    carriage `start_id` and carriage `end_id`                   
  m [source_id] [destination_id] [n]                            
    Move `n` passengers from one carrige to another, without    
    kicking anyone off the train.                               
  h [carriage_id]                                               
    Display the happiness of passengers in carriage             
    `carriage_id`                                               
  H                                                             
    Display the average happiness of all passengers on the train
                                                                
  N                                                             
    Create a new empty train                                    
  >                                                             
    Select the next train in the train list.                    
  <                                                             
    Select the previous train in the train list.                
  P                                                             
    Display the train list.                                     
  r [carriage_id]                                               
    Remove carriage `carriage_id` from the selected train.      
  R                                                             
    Remove the selected train.                                  
                                                                
  M                                                             
    Merge the selected train with the train after it.           
  S [n]                                                         
    Split the current train into smaller trains.                
  O                                                             
    Rearrange passengers on the selected train to optimise      
    happiness.                                                  
  ?                                                             
    Show help                                                   
================================================================
Enter command: [CTRL+D]
Goodbye

Stage 1.3 - Add carriages

Now it's time to start constructing your train!
When you run your program, the train (and your linked list representing the train) will start out completely empty.

It should look something like this:

visual representation of an empty linked list
A train with no carriages

To make your train more interesting (or even just a train at all), we need a way of adding empty carriages onto the end.

Command: Append carriage to train

a [carriage_id] [type] [capacity]

Description

The a command takes in 3 arguments, a string of length 5 called carriage_id, an enum carriage_type called type, and an integer capacity.

Some helper functions have been provided to help you scan in these arguments:

  • void scan_id(char id[ID_SIZE])
  • enum carriage_type scan_type(void)

You can find more information about them here: Provided helper functions.

When the a command is entered, your program should create a new carriage containing the carriage_id, type and capacity, then append it to the end of the list of carriages (i.e. perform a tail insertion).

Finally, it should print out a message to confirm that the command was successful:
Carriage: '[carriage_id]' attached!
You should replace [carriage_id] in this message with the carriage_id you scanned in.

For example, if we have just started the program, and we use the a command once, it would look like:

Welcome to Carriage Simulator
All aboard!
Enter command: a N1001 passenger 10
Carriage: 'N1001' attached!

Our linked list should now look like this:

visual representation of linked list with one node
A train with one carriage

If we then run the a command again:

Enter command: a N1002 buffet 20
Carriage: 'N1002' attached!

then our linked list should now look like:

A train with two carriages

Provided helper functions

Two helper functions have been provided for this stage:

  • void scan_id(char id[ID_SIZE]):
    • Scans the carriage_id into char id[ID_SIZE].
  • enum carriage_type scan_type(void):
    • Scans the type and returns it as an enum carriage_type .
    • Returns INVALID_TYPE if the type did not correspond to one of the valid carriage types.

You should use these functions to help you scan in the carriage_id and type arguments.

Remember that the arguments must be scanned in the correct order, so your code to scan arguments will look something like the following:

...
// Create variables to scan arguments into
char id[ID_SIZE];
enum carriage_type type;
int capacity;
// We receive the arguments in the order: [carriage_id] [type] [capacity]

// 1. Scan id first
scan_id(id);
// 2. Then scan the carriage type
type = scan_type();
// 3. Then scan the capacity. We can just use scanf() for this
scanf(" %d", &capacity);

// We've scanned in all the arguments! Now we can use them in our code
...

Errors

There is no error handling required for this stage. You'll be adding this later in Stage 1.5.

Assumptions

  • carriage_id will always be 5 or less characters long (with a null terminator at the end).

  • type will be entered as a lowercase string and automatically converted to the correct enum carriage_type for you by the scan_type function, when the function is used.

    Corresponding string enum carriage_type
    “passenger” PASSENGER
    "first_class" FIRST_CLASS
    “buffet" BUFFET
    “restroom” RESTROOM
    any invalid string INVALID_TYPE

    Note that you don't need to worry about INVALID_TYPE until Stage 1.5.
    Until then, you can assume that the returned enum carriage_type will never be INVALID_TYPE.

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1001 passenger 40
Carriage: 'N1001' attached!
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1000 first_class 30
Carriage: 'N1000' attached!
Enter command: a N1001 buffet 60
Carriage: 'N1001' attached!
Enter command: a N1002 passenger 50
Carriage: 'N1002' attached!
Enter command: a N1003 restroom 10
Carriage: 'N1003' attached!
Enter command: [CTRL+D]
Goodbye

Stage 1.4 - Printing out the train

Now we want to be able to display the train and all its carriages.

Command: Print train

p

Description

The p command takes no arguments.

When the p command is run, your program should print out all carriages in the train, from head to tail.

A function has been provided for you to format and print a single carriage:
Provided helper functions.

If there are no carriages in the train, the following message should be printed instead:
This train is empty!

Provided helper functions

One helper function has been provided for this stage:

  • void print_carriage(struct carriage *carriage)
    • Takes in a pointer to a single struct carriage and prints it in the correct format.

This means you don't need to worry about copying the exact output format for the p command. Instead, your code should loop through the linked list, and call the print_carriage function for each carriage.

Errors/Assumption/Clarifications: N/A

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: p
This train is empty!
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1000 passenger 30
Carriage: 'N1000' attached!
Enter command: p
 ---------\/--------- 
|       N1000        |
|    (PASSENGER)     |
| Occupancy:   0/30  |
 ---------||--------- 
Enter command: a N1001 buffet 60
Carriage: 'N1001' attached!
Enter command: a N0152 passenger 100
Carriage: 'N0152' attached!
Enter command: a N0153 first_class 10
Carriage: 'N0153' attached!
Enter command: p
 ---------\/--------- 
|       N1000        |
|    (PASSENGER)     |
| Occupancy:   0/30  |
 ---------||--------- 
 ---------\/--------- 
|       N1001        |
|      (BUFFET)      |
| Occupancy:   0/60  |
 ---------||--------- 
 ---------\/--------- 
|       N0152        |
|    (PASSENGER)     |
| Occupancy:   0/100 |
 ---------||--------- 
 ---------\/--------- 
|       N0153        |
|   (FIRST CLASS)    |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

Stage 1.5 - Handling Errors

By this stage, you should now be able to construct and display a train full of different carriages.
Well done!

You show the program so far to your bosses and they seem impressed.
There's one problem... You accidentally run the command a N0001 baffet -5:

Enter command: a N0001 baffet -5
Carriage: 'N0001' attached!
Enter command: p
 ---------\/--------- 
|       N0001        |
|     (INVALID)      |
| Occupancy:   0/-5  |
 ---------||--------- 

ruh roh
That train carriage doesn't make much sense.
Its type is invalid, and what does a -5 capacity even mean??

On top of this, you remember from your extensive train-ing that each carriage id has to be unique! But right now there's nothing in your code stopping someone from adding 100 carriages all with the same id.

Your bosses now seem less impressed, and they politely ask you to fix these bugs.

So in this stage you will be modifying your code from Stage 1.3, to add some restrictions on what carriages can be added to a train.

Error Conditions:

When running the a command, you scanned in the carriage_id, type and capacity for the new carriage.

Now, if any one of the following conditions are met, then you should not append a new carriage to linked list. You should instead print out an error message:

  • If type (returned by the scan_type function) is INVALID_TYPE, the following error should be printed:
    ERROR: Invalid carriage type

  • If capacity <= 0 or capacity > 999, the following error should be printed:
    ERROR: Capacity should be between 1 and 999

  • If there is already a carriage in the linked list that contains this carriage_id, the following error should be printed:
    ERROR: a carriage with id: '[carriage_id]' already exists in this train

    This last condition might be a little harder than it seems. How can you check that none of the carriages in your list contain a particular carriage_id?

Clarifications

  • If more than one error occurs, only the first error should be addressed by printing an error message. This is the same for all future commands.

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1000 passenger 30
Carriage: 'N1000' attached!
Enter command: a N1000 buffet 60
ERROR: a carriage with id: 'N1000' already exists in this train
Enter command: a N0151 passenger -100
ERROR: Capacity should be between 1 and 999
Enter command: a N0152 passenger 1000
ERROR: Capacity should be between 1 and 999
Enter command: a N0153 not_a_type 10
ERROR: Invalid carriage type
Enter command: a N0153 passenger 10
Carriage: 'N0153' attached!
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1000 passenger 30
Carriage: 'N1000' attached!
Enter command: a N1000 buffet 60
ERROR: a carriage with id: 'N1000' already exists in this train
Enter command: a N0151 passenger -100
ERROR: Capacity should be between 1 and 999
Enter command: a N0152 passenger 1000
ERROR: Capacity should be between 1 and 999
Enter command: a N0153 not_a_type 10
ERROR: Invalid carriage type
Enter command: p
 ---------\/--------- 
|       N1000        |
|    (PASSENGER)     |
| Occupancy:   0/30  |
 ---------||--------- 
Enter command: a N0153 passenger 10
Carriage: 'N0153' attached!
Enter command: p
 ---------\/--------- 
|       N1000        |
|    (PASSENGER)     |
| Occupancy:   0/30  |
 ---------||--------- 
 ---------\/--------- 
|       N0153        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

Testing and Submission

Remember to do your own testing

Are you finished with this stage? If so, you should make sure to do the following:

  • Run 1511 style, and clean up any issues a human may have reading your code. Don't forget -- 20% of your mark in the assignment is based on style and readability!
  • Autotest for this stage of the assignment by running the autotest-stage command as shown below.
  • Remember -- give early, and give often. Only your last submission counts, but why not be safe and submit right now?
1511 style cs_cs.c
1511 autotest-stage 01 cs_cs
give cs1511 ass2_cs_cs cs_cs.c

Stage 2

For Stage 2 of this assignment, you will use some more advanced linked list knowledge to modify linked list nodes, and perform calculations based on information stored over several nodes.

Specifically, this will include:

  • Inserting a carriage into any position in the train.
  • Modifying the number of passengers in carriages.
  • Calculating train occupancy and free space.

Stage 2.1 - Insert train carriages

Your bosses at Transport NSW are not happy with only being able to add carriages to the end of the train.
They demand that you give them a way to insert carriages anywhere.

Command: Insert carriage into the train

i [n] [carriage_id] [type] [capacity]

Description

The i command is similar to a (append carriage), except that it takes in an additional integer argument n which is the position in the train at which to insert the new carriage.

It should create a new struct carriage containing the carriage_id, type and capacity, and insert it into the train at position n.

For example:

  • If n == 0,
    then the new carriage should be inserted at the head of the train.
  • If n == 1,
    then the new carriage should be inserted after the first carriage in the train.
  • If n == N,
    then the new carriage should be inserted after the Nth carriage in the train.
  • If n is greater than or equal to the length of the train,
    then the new carriage should be inserted at the tail of the linked list.

So for a train of length 3, different values of n would indicate the following insertion positions:

diagram of linked list insertion positions
Insertion positions

If we then ran the command i 1 N1005 first_class 5 on this train like so:

Enter command: i 1 N1005 first_class 5
Carriage: 'N1005' inserted!

It would now look like:

After inserting at position 1

After successful insertion, your program should print the following message:
Carriage: '[carriage_id]' inserted!

Provided helper functions

See stage 1.3 - Add carriages for information on how to scan the carriage_id and type:
Provided helper functions

Errors

Just like command a: append carriage, there are restrictions on the carriages that can be inserted into the train.
Specifically, if one of the following conditions is met, then the carriage should not be inserted, and an error message should be printed out instead.

  • If n < 0 , the following error should be printed:
    ERROR: n must be at least 0
  • If type is INVALID_TYPE , the following error should be printed:
    ERROR: Invalid carriage type
  • If capacity <= 0 or capacity > 999, the following error should be printed:
    ERROR: Capacity should be between 1 and 999
  • If carriage_id already exists within the list, the following error should be printed:
    ERROR: a carriage with id: '[carriage_id]' already exists in this train

Assumptions

  • carriage_id will always be 5 or less characters long (with a null terminator at the end).
  • type will be entered as a lowercase string and automatically converted to the correct enum carriage_type for you by the scan_type function, when that function is used.

Clarifications

  • If n is 0, the new carriage should be inserted at the head of the linked list.
  • If n is greater or equal to the length of the linked list, then the new carriage should be inserted at the end of the linked list.

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: i 0 N1010 passenger 10
Carriage: 'N1010' inserted!
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: i 0 N1001 passenger 20
Carriage: 'N1001' inserted!
Enter command: p
 ---------\/--------- 
|       N1001        |
|    (PASSENGER)     |
| Occupancy:   0/20  |
 ---------||--------- 
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: i 0 N1010 passenger 10
Carriage: 'N1010' inserted!
Enter command: i 0 N1011 passenger 20
Carriage: 'N1011' inserted!
Enter command: i 1 N1013 first_class 20
Carriage: 'N1013' inserted!
Enter command: p
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/20  |
 ---------||--------- 
 ---------\/--------- 
|       N1013        |
|   (FIRST CLASS)    |
| Occupancy:   0/20  |
 ---------||--------- 
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: i 3 N1014 first_class 20
Carriage: 'N1014' inserted!
Enter command: p
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/20  |
 ---------||--------- 
 ---------\/--------- 
|       N1013        |
|   (FIRST CLASS)    |
| Occupancy:   0/20  |
 ---------||--------- 
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1014        |
|   (FIRST CLASS)    |
| Occupancy:   0/20  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: i 5 N1010 passenger 10
Carriage: 'N1010' inserted!
Enter command: i 0 N1011 passenger 20
Carriage: 'N1011' inserted!
Enter command: i 100 N1013 first_class 20
Carriage: 'N1013' inserted!
Enter command: p
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/20  |
 ---------||--------- 
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1013        |
|   (FIRST CLASS)    |
| Occupancy:   0/20  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: i -1 N1010 passenger 10
ERROR: n must be at least 0
Enter command: i 0 N1010 passenger 10
Carriage: 'N1010' inserted!
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: i 0 N1010 first_class 10
ERROR: a carriage with id: 'N1010' already exists in this train
Enter command: i 0 N1011 not_a_type 10
ERROR: Invalid carriage type
Enter command: i 0 N1012 passenger 0
ERROR: Capacity should be between 1 and 999
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

Stage 2.2 - Add passengers

Now that we have built a train, it's time to start seating some passengers onboard.

If you've ever gotten onto a really crowded train carriage, you'll know that sometimes it's best to keep moving down the train until you find some more space.
So in this section, you'll have to implement this behaviour!

If too many passengers attempt to get onto the same carriage, some will then have to move into a different carriage to find a seat.

Command: Seat Passengers

s [carriage_id] [n]

Description

The s command takes in a string carriage_id and an integer number of passengers n.

The s command should attempt to find the carriage with an id matching carriage_id, and then add the n passengers to it, increasing its occupancy by one for each passenger.

However, a single carriage can only seat passengers up to its capacity,
(meaning that for a given carriage, carriage->occupancy can never be greater than carriage->capacity).

If all n passengers cannot fit into the specified carriage, then any remaining passengers should overflow into the following carriage (i.e. carriage->next), and attempt to find a seat there instead.

If there's not enough capacity in the following carriage, they should keep overflowing down the train until either all passengers have been seated, or the end of the train is reached.

Depending on the outcome of the s command, your program will have to print some of the following messages:

  • If passengers are successfully seated in a carriage, the following message should be printed:
    [num_seated] passengers added to [carriage_id]
    This should occur for each carriage that received passengers

  • If the end of the train was reached and there are still passengers that have not been seated, the following message should be printed:
    [remaining] passengers could not be seated

For example, if we start with the following train:

visual representation of a linked list train
A train before passengers have been seated

And then run s N1010 20, we'd end up with:

Enter command: s N1010 20
10 passengers added to N1010
5 passengers added to N1011
5 passengers added to N1012

A train after passengers have been seated

Provided helper functions

See stage 1.3 - Add carriages for information on how to scan the carriage_id:
Provided helper functions

Errors

If one of the following errors occur, then no passengers should be seated, and an error message should be printed instead.

  • If n <= 0, the following error should be printed:
    ERROR: n must be a positive integer
  • If no carriage containing carriage_id exists, the following error should be printed:
    ERROR: No carriage exists with id: '[carriage_id]'

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1001 passenger 10
Carriage: 'N1001' attached!
Enter command: s N1001 5
5 passengers added to N1001
Enter command: p
 ---------\/--------- 
|       N1001        |
|    (PASSENGER)     |
| Occupancy:   5/10  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1000 passenger 10
Carriage: 'N1000' attached!
Enter command: a N1001 passenger 10
Carriage: 'N1001' attached!
Enter command: a N1002 passenger 5
Carriage: 'N1002' attached!
Enter command: a N1003 passenger 30
Carriage: 'N1003' attached!
Enter command: s N1001 25
10 passengers added to N1001
5 passengers added to N1002
10 passengers added to N1003
Enter command: p
 ---------\/--------- 
|       N1000        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1001        |
|    (PASSENGER)     |
| Occupancy:  10/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1002        |
|    (PASSENGER)     |
| Occupancy:   5/5   |
 ---------||--------- 
 ---------\/--------- 
|       N1003        |
|    (PASSENGER)     |
| Occupancy:  10/30  |
 ---------||--------- 
Enter command: s N1001 20
20 passengers added to N1003
Enter command: p
 ---------\/--------- 
|       N1000        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1001        |
|    (PASSENGER)     |
| Occupancy:  10/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1002        |
|    (PASSENGER)     |
| Occupancy:   5/5   |
 ---------||--------- 
 ---------\/--------- 
|       N1003        |
|    (PASSENGER)     |
| Occupancy:  30/30  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1000 passenger 10
Carriage: 'N1000' attached!
Enter command: a N1001 passenger 5
Carriage: 'N1001' attached!
Enter command: a N1002 passenger 30
Carriage: 'N1002' attached!
Enter command: s N1000 50
10 passengers added to N1000
5 passengers added to N1001
30 passengers added to N1002
5 passengers could not be seated
Enter command: p
 ---------\/--------- 
|       N1000        |
|    (PASSENGER)     |
| Occupancy:  10/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1001        |
|    (PASSENGER)     |
| Occupancy:   5/5   |
 ---------||--------- 
 ---------\/--------- 
|       N1002        |
|    (PASSENGER)     |
| Occupancy:  30/30  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: s N1001 -40
ERROR: n must be a positive integer
Enter command: s N1001 40
ERROR: No carriage exists with id: 'N1001'
Enter command: [CTRL+D]
Goodbye

Stage 2.3 - Remove passengers

Command: Disembark (remove) passengers

d [carriage_id] [n]

Description

The d command should attempt to find the carriage with an id matching carriage_id and remove n passengers from it, by reducing its occupancy.

It should then print: [n] passengers removed from [carriage_id].

Provided helper functions

See stage 1.3 - Add carriages for information on how to scan the carriage_id:
Provided helper functions

Errors

If one of following errors occur, then no passengers should be removed, and an error message should be printed instead.

  • If n is not a positive integer, the following error should be printed:
    ERROR: n must be a positive integer
  • If carriage_id could not be found, the following error should be printed:
    ERROR: No carriage exists with id: '[carriage_id]'
  • If n is greater than the occupancy of the carriage, the following error should be printed:
    ERROR: Cannot remove [n] passengers from [carriage_id]

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: s N1010 10
10 passengers added to N1010
Enter command: d N1010 5
5 passengers removed from N1010
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   5/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: a N1012 passenger 5
Carriage: 'N1012' attached!
Enter command: a N1013 passenger 30
Carriage: 'N1013' attached!
Enter command: s N1011 25
10 passengers added to N1011
5 passengers added to N1012
10 passengers added to N1013
Enter command: d N1011 25
ERROR: Cannot remove 25 passengers from N1011
Enter command: d N1013 30
ERROR: Cannot remove 30 passengers from N1013
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: d N1010 -25
ERROR: n must be a positive integer
Enter command: d N1010 5
ERROR: No carriage exists with id: 'N1010'
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: s N1010 5
5 passengers added to N1010
Enter command: d N1010 6
ERROR: Cannot remove 6 passengers from N1010
Enter command: [CTRL+D]
Goodbye

Stage 2.4 - Count passengers

This stage involves implementing 2 very similar commands. You can implement them in any order.

Command: Count total passengers

T

Description

Print out the total number of passengers on the train, and print out the remaining unoccupied capacity of the whole train in the following format:

Total occupancy: [total_occupancy]
Unoccupied capacity: [unoccupied_capacity]

T Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: T
Total occupancy: 0
Unoccupied capacity: 0
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: a N1012 buffet 20
Carriage: 'N1012' attached!
Enter command: T
Total occupancy: 0
Unoccupied capacity: 40
Enter command: s N1010 30
10 passengers added to N1010
10 passengers added to N1011
10 passengers added to N1012
Enter command: T
Total occupancy: 30
Unoccupied capacity: 10
Enter command: [CTRL+D]
Goodbye

Command: Count passengers in range

c [start_id] [end_id]

Description

The c command should attempt to find the carriage with an id matching start_id, and the carriage with an id matching end_id. It should then count the number of passengers, and the unoccupied capacity from carriage: start_id to carriage: end_id , inclusive.

It should print out the final result in the following format:

Occupancy: [occupancy]
Unoccupied: [unoccupied_capacity]

Provided helper functions

See stage 1.3 - Add carriages for information on how to scan the carriage ids: start_id and end_id:
Provided helper functions

Errors

  • If either start_id or end_id do not exist, the following error should be printed:
    ERROR: No carriage exists with id: '[carriage_id]'
  • If start_id does not come before end_id in the train, the following error should be printed:
    ERROR: Carriages are in the wrong order

Clarifications

  • start_id can be the same as end_id.
  • end_id should be included in the range.

c Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: a N1012 buffet 20
Carriage: 'N1012' attached!
Enter command: s N1010 25
10 passengers added to N1010
10 passengers added to N1011
5 passengers added to N1012
Enter command: c N1010 N1010
Occupancy: 10
Unoccupied: 0
Enter command: c N1010 N1012
Occupancy: 25
Unoccupied: 15
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: a N1012 buffet 20
Carriage: 'N1012' attached!
Enter command: s N1010 25
10 passengers added to N1010
10 passengers added to N1011
5 passengers added to N1012
Enter command: c N1010 N1012
Occupancy: 25
Unoccupied: 15
Enter command: T
Total occupancy: 25
Unoccupied capacity: 15
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: c N1010 N1011
ERROR: No carriage exists with id: 'N1010'
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: c N1010 N1011
ERROR: No carriage exists with id: 'N1011'
Enter command: i 0 N1011 passenger 10
Carriage: 'N1011' inserted!
Enter command: c N1010 N1011
ERROR: Carriages are in the wrong order
Enter command: [CTRL+D]
Goodbye

Stage 2.5 - Move passengers

Command

m [source_id] [destination_id] [n]

Description

The m command should attempt to move n passengers from carriages source_id to destination_id. Similar to Stage 2.2, if all n passengers cannot fit into destination_id, then the remaining passengers should overflow down the train.

However, if performing the move would result in any passengers left unseated, then the move command should not be performed and the train should be left unchanged.

  • For each carriage that receives passengers, the following should be printed:
    [num_seated] passengers moved from [source_id] to [destination_id]

Errors

If one of the following errors occur, then passengers should not be moved, and an error message should be printed instead.

  • If n is less than or equal to 0, the following error should be printed:
    ERROR: n must be a positive integer

  • if source_id does not exist within the train, the following error should be printed:
    ERROR: No carriage exists with id: '[source_id]'

  • If n is greater than the occupancy of source_id, the following error should be printed:
    ERROR: Cannot remove [n] passengers from [carriage_id]

  • If destination_id does not exist within the train, the following error should be printed
    ERROR: No carriage exists with id: '[destination_id]'

  • If moving passengers would result in some passengers being unable to be seated, the following error should be printed:
    ERROR: not enough space to move passengers

Clarifications

  • This should function almost the same as a disembark command (d [source_id] [n] ), followed immediately by a seat command (s [destination_id] [n] ), except that if the seat command would leave some passengers unseated because there's not enough room in the rest of the train, no passengers should be moved/removed.
  • destination_id could appear in the list before source_id.
  • Passengers should always overflow towards the tail of the list, just like the s command in Stage 2.2.

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: s N1010 10
10 passengers added to N1010
Enter command: m N1010 N1011 8
8 passengers moved from N1010 to N1011 
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   2/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   8/10  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: a N1012 passenger 5
Carriage: 'N1012' attached!
Enter command: a N1013 passenger 30
Carriage: 'N1013' attached!
Enter command: s N1011 40
10 passengers added to N1011
5 passengers added to N1012
25 passengers added to N1013
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:  10/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1012        |
|    (PASSENGER)     |
| Occupancy:   5/5   |
 ---------||--------- 
 ---------\/--------- 
|       N1013        |
|    (PASSENGER)     |
| Occupancy:  25/30  |
 ---------||--------- 
Enter command: m N1011 N1011 10
10 passengers moved from N1011 to N1011 
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:  10/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1012        |
|    (PASSENGER)     |
| Occupancy:   5/5   |
 ---------||--------- 
 ---------\/--------- 
|       N1013        |
|    (PASSENGER)     |
| Occupancy:  25/30  |
 ---------||--------- 
Enter command: m N1013 N1010 25
10 passengers moved from N1013 to N1010 
15 passengers moved from N1013 to N1013 
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:  10/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:  10/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1012        |
|    (PASSENGER)     |
| Occupancy:   5/5   |
 ---------||--------- 
 ---------\/--------- 
|       N1013        |
|    (PASSENGER)     |
| Occupancy:  15/30  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: m N1010 N1011 -5
ERROR: n must be a positive integer
Enter command: m N1010 N1011 5
ERROR: No carriage exists with id: 'N1010'
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: m N1010 N1011 5
ERROR: Cannot remove 5 passengers from N1010
Enter command: s N1010 10
10 passengers added to N1010
Enter command: a N1011 passenger 5
Carriage: 'N1011' attached!
Enter command: m N1010 N1011 10
ERROR: not enough space to move passengers
Enter command: [CTRL+D]
Goodbye

Testing and Submission

Remember to do your own testing!

Are you finished with this stage? If so, you should make sure to do the following:

  • Run 1511 style, and clean up any issues a human may have reading your code. Don't forget -- 10% of your mark in the assignment is based on style!
  • Autotest for this stage of the assignment by running the autotest-stage command as shown below.
  • Remember -- give early, and give often. Only your last submission counts, but why not be safe and submit right now?
1511 style cs_cs.c
1511 autotest-stage 02 cs_cs
give cs1511 ass2_cs_cs cs_cs.c

Stage 3

In Stage 3, you'll be creating and then manipulating 2D linked lists in the form of a list of trains, each of which contains a list of carriages.
You will also be managing your memory usage by freeing memory and preventing memory leaks.

Specifically, this will include:

  • Modifying your code from previous stages to store all carriages inside a struct train.
  • Adding commands to create new empty trains, and then switch between different trains.
  • Removing and freeing carriages and trains.

By the end of this stage, your linked list will look something like:

Stage 3: A list of multiple trains

Stage 3.1 - Create multiple trains

Adding the new struct type

First, you should copy the following struct definition into your code:

struct train {
    struct carriage *carriages;
    // Feel free to add more fields here if you want.
    struct train *next;
};

Purpose:

  • Wraps around the linked list of carriages and represents a single train, which can be connected to other trains in a list.
  • Stores the head pointer of the list of carriages, plus any other information you choose.

Fields:

  • struct carriage *carriages
    • The head pointer to a linked list of carriages.
  • struct train *next
    • A pointer to the next train in the linked list of trains.

You will be using this struct to store the linked list of struct carriages and to connect multiple trains together in a list.

To start with, you will have to malloc and initialise a struct train at the start of your program. Then, you should modify your code to use the carriages field inside this struct as your linked list head pointer.

You will also have to implement a way of keeping track of the currently selected train, to which all commands from previous stages will apply to.

For example, instead of:

int main(void) {
    // Before stage 3.1
    // We used a struct carriage pointer to keep track of our linked list
    struct carriage *head = NULL;

    /* Some other code */
    head = insert_carriage(/*Args to create carriage*/);
}

Your code might now look something like:

int main(void) {

    // After stage 3.1,
    // we'll use this pointer to keep track of our list of all trains.
    struct train *trains = NULL;
    
    // we need to create one train when our program starts to 
    // store all our carriages in
    struct train *trains = /* Code to create a struct train>*/

    // We also need another pointer to keep track of 
    // which train we have selected.
    struct train *selected = trains;
    
    /* Some other code */

    // we can now use this as our head pointer.
    selected->carriage = insert_carriage(/*Args to insert carriage*/);
}

At this point your linked list should look like:

A list of carriages inside a struct train

Once you've modified your code to use the struct train, you can go on to implement the 3 new commands for this stage:

Commands

  • N: New empty train.

    Description:
    Malloc an additional train with no carriages which can be used with the previous commands. This train should be inserted before the currently selected train, in the linked list of trains.
    The currently selected train should remain the same.

  • >: Select next train

    Description:
    Set the selected train to be the next train in the linked list. All commands from previous stages should now apply to the newly selected train. If there is no train after the selected train, then the selected train should stay the same (the currently selected pointer should never go past the end of the list of trains).

  • <: Select previous train

    Description:
    Set the selected train to be the previous train in the linked list. All commands from previous stages should now use the newly selected train. If there is no train before the selected train, then the selected train should stay the same (the currently selected pointer should never go past the start of the list of trains).

Errors: N\A

Clarifications

  • You do not need to check that carriage id's are unique across different trains. For example, a carriage_id in train 1 could be the same as a carriage_id in train 2.
  • All commands from the previous stages should be applied to the currently selected train.

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: a N1012 passenger 10
Carriage: 'N1012' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1012        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: N
Enter command: <
Enter command: p
This train is empty!
Enter command: >
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1012        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: 

[CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: N
Enter command: <
Enter command: p
This train is empty!
Enter command: a N1010 buffet 50
Carriage: 'N1010' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/50  |
 ---------||--------- 
Enter command: >
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: <
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: >
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: N
Enter command: <
Enter command: <
Enter command: p
This train is empty!
Enter command: >
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

Stage 3.2 - Print trains

Command

P

Description

This command should print the list of trains from head to tail, displaying the total capacity, total occupancy and number of carriages of each.

Provided helper functions

A function has been provided for this stage:

void print_train_summary(
    int is_selected, 
    int n, 
    int capacity, 
    int occupancy, 
    int num_carriages
);

Parameters:

  • int is_selected: Either

    • 1 If this struct train is the currently selected train.
    • 0, Otherwise.

    (You might want to define some constants for this).

  • int n: The position of the given train in the list of trains, starting from 0.

  • int capacity: The total capacity of the given train.

  • int occupancy: The total occupancy of the given train.

  • int num_carriages: The number of carriages in the given train.

This function prints out the required information in the correct format.
Your job is to calculate the correct values to pass to this function.

Assumptions

  • There will always be at least one train in the list of trains.

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: P
--->Train #0
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: N
Enter command: N
Enter command: N
Enter command: P
    Train #0
        Carriages:   0
        Capacity :   0/0  
    ----------------------
    Train #1
        Carriages:   0
        Capacity :   0/0  
    ----------------------
    Train #2
        Carriages:   0
        Capacity :   0/0  
    ----------------------
--->Train #3
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: >
Enter command: P
    Train #0
        Carriages:   0
        Capacity :   0/0  
    ----------------------
    Train #1
        Carriages:   0
        Capacity :   0/0  
    ----------------------
    Train #2
        Carriages:   0
        Capacity :   0/0  
    ----------------------
--->Train #3
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: <
Enter command: <
Enter command: <
Enter command: P
--->Train #0
        Carriages:   0
        Capacity :   0/0  
    ----------------------
    Train #1
        Carriages:   0
        Capacity :   0/0  
    ----------------------
    Train #2
        Carriages:   0
        Capacity :   0/0  
    ----------------------
    Train #3
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1001 passenger 10
Carriage: 'N1001' attached!
Enter command: a N1002 passenger 20
Carriage: 'N1002' attached!
Enter command: s N1002 5
5 passengers added to N1002
Enter command: N
Enter command: N
Enter command: P
    Train #0
        Carriages:   0
        Capacity :   0/0  
    ----------------------
    Train #1
        Carriages:   0
        Capacity :   0/0  
    ----------------------
--->Train #2
        Carriages:   2
        Capacity :   5/30 
    ----------------------
Enter command: <
Enter command: a N1002 passenger 20
Carriage: 'N1002' attached!
Enter command: P
    Train #0
        Carriages:   0
        Capacity :   0/0  
    ----------------------
--->Train #1
        Carriages:   1
        Capacity :   0/20 
    ----------------------
    Train #2
        Carriages:   2
        Capacity :   5/30 
    ----------------------
Enter command: [CTRL+D]
Goodbye

Stage 3.3 - Remove Carriage

Command

r [carriage_id]

Description

The r command should attempt to find the carriage with an id matching carriage_id and remove it from the selected train, freeing all associated memory.

Provided helper function

See stage 1.3 - Add carriages for information on how to scan the carriage_id:
Provided helper functions.

Errors

  • If no carriage containing carriage_id exists in the selected train, then the following error should be printed:
    ERROR: No carriage exists with id: '[carriage_id]'

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 buffet 11
Carriage: 'N1011' attached!
Enter command: a N1012 buffet 11
Carriage: 'N1012' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|      (BUFFET)      |
| Occupancy:   0/11  |
 ---------||--------- 
 ---------\/--------- 
|       N1012        |
|      (BUFFET)      |
| Occupancy:   0/11  |
 ---------||--------- 
Enter command: r N1011
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1012        |
|      (BUFFET)      |
| Occupancy:   0/11  |
 ---------||--------- 
Enter command: r N1012
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: r N1010
Enter command: p
This train is empty!
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: r N1011
ERROR: No carriage exists with id: 'N1011'
Enter command: [CTRL+D]
Goodbye

Stage 3.4 - Remove Train

Command

R

Description

The R command should remove and free the selected train, and all its carriages. The previous train in the list should be set to be the new selected train.

However, if the selected train is at the head of the list, then the next train in the list should become the new selected train instead.

If the selected train is the only train in the linked list, then it should instead be replaced by an empty train with no carriages, to allow further commands to be run on it.

Additionally, when the program ends, all malloc'd memory should be freed, and there should be no memory leaks.

Clarifications

  • From now on, you should check for memory leaks by compiling with dcc --leak-check ....
  • Autotests and marking tests from this stage onwards onwards will check for memory leaks.

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: P
--->Train #0
        Carriages:   2
        Capacity :   0/20 
    ----------------------
Enter command: R
Enter command: p
This train is empty!
Enter command: P
--->Train #0
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: N
Enter command: <
Enter command: a N1010 passenger 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: N
Enter command: P
    Train #0
        Carriages:   0
        Capacity :   0/0  
    ----------------------
--->Train #1
        Carriages:   2
        Capacity :   0/20 
    ----------------------
    Train #2
        Carriages:   2
        Capacity :   0/20 
    ----------------------
Enter command: R
Enter command: P
--->Train #0
        Carriages:   0
        Capacity :   0/0  
    ----------------------
    Train #1
        Carriages:   2
        Capacity :   0/20 
    ----------------------
Enter command: R
Enter command: P
--->Train #0
        Carriages:   2
        Capacity :   0/20 
    ----------------------
Enter command: R
Enter command: P
--->Train #0
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: [CTRL+D]
Goodbye

Testing and Submission

Remember to do your own testing

Are you finished with this stage? If so, you should make sure to do the following:

  • Run 1511 style, and clean up any issues a human may have reading your code. Don't forget -- 20% of your mark in the assignment is based on style and readability!
  • Autotest for this stage of the assignment by running the autotest-stage command as shown below.
  • Remember -- give early, and give often. Only your last submission counts, but why not be safe and submit right now?
1511 style cs_cs.c
1511 autotest-stage 03 cs_cs
give cs1511 ass2_cs_cs cs_cs.c

Stage 4

This stage is for students who want to challenge themselves, and solve more complicated linked lists and programming problems, such as:

  • Merging different trains together
  • Splitting a single train into 2 or more smaller trains.

Stage 4.1 - Merge trains

Command

M

Description

Merge the currently selected train with the next train in the list into one larger train. The next train is the train immediately following the selected train (i.e. selected->next).

If all of the carriage_id's in both trains are unique, then all the carriages of the next train should be appended to the end of the selected train, and the next train should be removed from the list of trains.

Merge with no overlapping carriage_ids:

Before M command

After M command

However, if a carriage in the selected train and a carriage in the next train share the same carriage_id, then the second carriage should be merged into the first to create a larger carriage containing the passengers and capacity of both.
The type of this new carriage should be the same as the type of carriage from the selected train.

For example: If we have the following carriage from the first and second trains:

Diagram of a struct carriage, containing a carriage_id of N1138 and the type PASSENGER.
Carriage from selected train
Diagram of a struct carriage, containing a carriage_id of N1138 and the type FIRST_CLASS.
Carriage from the next train

Then they should be merged into the following carriage:

Diagram of a struct carriage, containing a carriage_id of N1138, the type passenger, and the combined occupancy and capacity of the previous 2 structs.
Merged carriage

This is to prevent having multiple carriages in the same train with the same IDs.

Merge with overlapping carriage_ids:

Before M command

After M command

Clarifications

  • If there is no train after the currently selected train, then the M command should do nothing.

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 buffet 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: N
Enter command: <
Enter command: a N1012 restroom 10
Carriage: 'N1012' attached!
Enter command: a N1013 passenger 10
Carriage: 'N1013' attached!
Enter command: p
 ---------\/--------- 
|       N1012        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1013        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: P
--->Train #0
        Carriages:   2
        Capacity :   0/20 
    ----------------------
    Train #1
        Carriages:   2
        Capacity :   0/20 
    ----------------------
Enter command: M
Enter command: p
 ---------\/--------- 
|       N1012        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1013        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: P
--->Train #0
        Carriages:   4
        Capacity :   0/40 
    ----------------------
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 buffet 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: M
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: N
Enter command: <
Enter command: a N1010 buffet 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: >
Enter command: a N1000 restroom 10
Carriage: 'N1000' attached!
Enter command: a N1010 restroom 15
Carriage: 'N1010' attached!
Enter command: a N1001 first_class 20
Carriage: 'N1001' attached!
Enter command: a N1011 passenger 5
Carriage: 'N1011' attached!
Enter command: p
 ---------\/--------- 
|       N1000        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1010        |
|     (RESTROOM)     |
| Occupancy:   0/15  |
 ---------||--------- 
 ---------\/--------- 
|       N1001        |
|   (FIRST CLASS)    |
| Occupancy:   0/20  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/5   |
 ---------||--------- 
Enter command: P
    Train #0
        Carriages:   2
        Capacity :   0/20 
    ----------------------
--->Train #1
        Carriages:   4
        Capacity :   0/50 
    ----------------------
Enter command: <
Enter command: M
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/25  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/15  |
 ---------||--------- 
 ---------\/--------- 
|       N1000        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1001        |
|   (FIRST CLASS)    |
| Occupancy:   0/20  |
 ---------||--------- 
Enter command: P
--->Train #0
        Carriages:   4
        Capacity :   0/70 
    ----------------------
Enter command: [CTRL+D]
Goodbye

Stage 4.2 - Split trains

Command

S [n]

Description

The S command takes in one integer argument, n.

The s command should first scan in n carriage_ids, using the provided scan_id function. These carriage_ids should correspond with carriages in the selected train.

It should then replace the selected train with n + 1 smaller trains, each of which contains a section of the carriages from the selected train, effectively splitting it apart into multiple smaller trains.

These splits should occur before each of the specified carriages, and each of these new trains should be inserted into the list of train in place of the selected train so that their carriages are in same order as they were in the original train.

Finally, once the selected train has been broken apart and replaced, the first of these new trains should be set as the currently selected train.

For example, if we started with the following train:

Before S command

And then ran the following S Command:

Enter command: S 2
Enter ids:
N1002
N1004

This would cause the following splits to occur:

Highlight splits at "N1002" and "N1004"

After running S 2, With ids: N1002, N1004

Handling Invalid Carriage IDs

Every scanned carriage_id that does not correspond to a carriage in the selected train should be skipped over, each resulting in one fewer split to the selected train.

Additionally the following message should be printed each time that one of these invalid carriage_ids is scanned in:
No carriage exists with id: '[carriage_id]'. Skipping

Errors

  • If n <= 0 , then the following message should be printed:
    ERROR: n must be a positive integer

Clarifications

  • The carriage_ids may not be scanned in the same order that they appear inside the train.
  • Multiple of the same carriage_ids could be entered into the same S command, causing multiple splits before the same carriage.
    If you're not sure about this behaviour, you may want to experiment with the reference implementation: 1511 cs_cs.
  • After the S command, the currently selected train should be the first of the sub trains.
  • If a carriage_id corresponds to the first carriage in the currently selected train, then the train should be split such that the first train contains no carriages.
  • You cannot assume a maximum value for n. n could be any positive int.

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: N
Enter command: <
Enter command: a N1010 buffet 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: a N1002 restroom 10
Carriage: 'N1002' attached!
Enter command: a N1003 restroom 10
Carriage: 'N1003' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1002        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1003        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: P
--->Train #0
        Carriages:   4
        Capacity :   0/40 
    ----------------------
    Train #1
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: S 1
Enter ids:
N1002
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: >
Enter command: p
 ---------\/--------- 
|       N1002        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1003        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: P
    Train #0
        Carriages:   2
        Capacity :   0/20 
    ----------------------
--->Train #1
        Carriages:   2
        Capacity :   0/20 
    ----------------------
    Train #2
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: N
Enter command: <
Enter command: a N1010 buffet 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: a N1012 restroom 10
Carriage: 'N1012' attached!
Enter command: a N1013 restroom 10
Carriage: 'N1013' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1012        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1013        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: P
--->Train #0
        Carriages:   4
        Capacity :   0/40 
    ----------------------
    Train #1
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: S 3
Enter ids:
N1013
N1010
N1011
Enter command: P
--->Train #0
        Carriages:   0
        Capacity :   0/0  
    ----------------------
    Train #1
        Carriages:   1
        Capacity :   0/10 
    ----------------------
    Train #2
        Carriages:   2
        Capacity :   0/20 
    ----------------------
    Train #3
        Carriages:   1
        Capacity :   0/10 
    ----------------------
    Train #4
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: p
This train is empty!
Enter command: >
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: >
Enter command: p
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1012        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: >
Enter command: p
 ---------\/--------- 
|       N1013        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: N
Enter command: <
Enter command: a N1010 buffet 10
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 10
Carriage: 'N1011' attached!
Enter command: a N1012 restroom 10
Carriage: 'N1012' attached!
Enter command: a N1013 restroom 10
Carriage: 'N1013' attached!
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1012        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1013        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: P
--->Train #0
        Carriages:   4
        Capacity :   0/40 
    ----------------------
    Train #1
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: S 3
Enter ids:
N9999
No carriage exists with id: 'N9999'. Skipping
N8888
No carriage exists with id: 'N8888'. Skipping
N1013
Enter command: P
--->Train #0
        Carriages:   3
        Capacity :   0/30 
    ----------------------
    Train #1
        Carriages:   1
        Capacity :   0/10 
    ----------------------
    Train #2
        Carriages:   0
        Capacity :   0/0  
    ----------------------
Enter command: p
 ---------\/--------- 
|       N1010        |
|      (BUFFET)      |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1011        |
|    (PASSENGER)     |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1012        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: >
Enter command: p
[CTRL+D]
 ---------\/--------- 
|       N1013        |
|     (RESTROOM)     |
| Occupancy:   0/10  |
 ---------||--------- 
Enter command: Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: S 0
ERROR: n must be a positive integer
Enter command: [CTRL+D]
Goodbye

Testing and Submission

Remember to do your own testing

Are you finished with this stage? If so, you should make sure to do the following:

  • Run 1511 style, and clean up any issues a human may have reading your code. Don't forget -- 20% of your mark in the assignment is based on style and readability!
  • Autotest for this stage of the assignment by running the autotest-stage command as shown below.
  • Remember -- give early, and give often. Only your last submission counts, but why not be safe and submit right now?
1511 style cs_cs.c
1511 autotest-stage 04 cs_cs
give cs1511 ass2_cs_cs cs_cs.c

Extra Challenge

You will be implementing some additional commands which require more complex problem solving.

Challenge 1.1 - Passenger Happiness and Special Carriages

In order to make our trains better for the passengers, we need a way to estimate how they will affect the happiness of passengers.

You've probably experienced how uncomfortable a crowded train carriage can be, so we'll estimate that crowded carriages lead to less happy passengers.

Which means that carriages with higher capacities and smaller occupancies must contain the happiest passengers!

On top of this, passengers will surely be happier with access to a buffet or a restroom, so we'll have to factor that into our calculations too.

This stage will involve implementing 2 commands.

Command: Print the happiness of passengers in a carriage.

h [carriage_id]

Description

The h command should attempt to find the carriage with an id matching carriage_id in the selected train, and then calculate the happiness of the passengers in that carriage.

The base passenger happiness ranges from 0.00 - 100.00, depending on the occupancy and capacity of their carriage.

This base happiness can be calculated using the formula:
passenger_happiness = (capacity - occupancy) / capacity * 100

Example diagram:

visual representation of linked list train showing occupancy and passenger happiness
Challenge 1.1: Showing passenger happiness

This base happiness can then be modified by the effects of the Special carriages.

See the section: Special Carriages, for more information.

The result of this command should be printed in the following format:
[carriage_id] happiness: [happiness (2dp)]

Errors

  • If no carriage containing carriage_id exists, then the following error should be printed:
    ERROR: No carriage exists with id: '[carriage_id]'

h Basic Examples, with no special carriages.

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 100
Carriage: 'N1010' attached!
Enter command: s N1010 60
60 passengers added to N1010
Enter command: h N1010
N1010 happiness: 40.00
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 100
Carriage: 'N1010' attached!
Enter command: h N1010
N1010 happiness: 100.00
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: h N1010
ERROR: No carriage exists with id: 'N1010'
Enter command: [CTRL+D]
Goodbye

Command

H

Description

Print the average happiness of all passenger on the selected train.

If the train has no passengers, then this average happiness should default to 100.00.

The average happiness of the train ranges from 0.00 - 100.00, and can be calculated using the formula
average_happiness =
(The sum of every passengers happiness)/(The total number of passengers).

The result of this command should be printed in the following format:
Average happiness: [average_happiness (2dp)]

Clarifications

  • Adding/removing/moving passengers can affect happiness.

H Examples with no special carriages

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 30
Carriage: 'N1010' attached!
Enter command: a N1011 passenger 70
Carriage: 'N1011' attached!
Enter command: s N1010 60
30 passengers added to N1010
30 passengers added to N1011
Enter command: H
Average happiness: 28.57
Enter command: [CTRL+D]
Goodbye

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: H
Average happiness: 100.00
Enter command: a N1010 passenger 30
Carriage: 'N1010' attached!
Enter command: H
Average happiness: 100.00
Enter command: [CTRL+D]
Goodbye

Special carriages:

When calculating a passengers happiness, you will have to take into account the effects of the 3 special types of carriages: BUFFET, RESTROOM, FIRST_CLASS. These act mostly as regular train carriages but can impact the happiness of the passengers in the surrounding carriages. Specifically:

  • BUFFET carriages increase the happiness of their own passengers by 40 points, and increase the happiness of passengers in connected carriages by 30 points. Bonuses do not stack with other BUFFET carriages.

  • RESTROOM carriages decrease the happiness of their own passengers by 30 points, and increase the happiness of passengers in connected carriages by 1.5x. Bonuses do not stack with other RESTROOM carriages.

  • FIRST_CLASS carriages increase the happiness of their own passengers by 100 points (effectively setting them to the maximum), but block access to buffet or restroom carriages, meaning that passengers on one side of a FIRST_CLASS carriage cannot gain benefit from BUFFET or restroom carriage on the other side.

Passenger happiness after bonuses should still be capped between 0 and 100.

By default, the whole train is considered connected, meaning that a single BUFFET carriage will increase the happiness of every passenger on the train. However, placing a FIRST_CLASS carriage can block some passengers from accessing the BUFFET carriage, meaning they do not gain any benefit.

If a single carriage is connected to multiple carriages of the same type, they only receive the bonus once. This means that even if a carriage is connected to two BUFFET carriages, it's passengers still only receive the happiness increase once (+30), and the BUFFET carriages don't affect each other's happiness. However, bonuses from different types of carriages can stack.

For example, the following train has no FIRST_CLASS carriages, meaning that it is fully connected and any BUFFET or RESTROOM connection bonuses will apply to every carriage in the train:

Fully connected train with BUFFET carriage

But if we instead replaced one of the carriages with a FIRST_CLASS carriage, then some carriages won't be able to reach the BUFFET. This means that they won't receive any happiness bonus from that BUFFET carriage.

N1001 separated from BUFFET by a FIRST_CLASS carriage

Special carriage Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1010 passenger 30
Carriage: 'N1010' attached!
Enter command: a N0001 buffet 30
Carriage: 'N0001' attached!
Enter command: a R0001 restroom 30
Carriage: 'R0001' attached!
Enter command: s N1010 45
30 passengers added to N1010
15 passengers added to N0001
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:  30/30  |
 ---------||--------- 
 ---------\/--------- 
|       N0001        |
|      (BUFFET)      |
| Occupancy:  15/30  |
 ---------||--------- 
 ---------\/--------- 
|       R0001        |
|     (RESTROOM)     |
| Occupancy:   0/30  |
 ---------||--------- 
Enter command: h N1010
N1010 happiness: 45.00
Enter command: H
Average happiness: 63.33
Enter command: i 1 F0001 first_class 10
Carriage: 'F0001' inserted!
Enter command: p
 ---------\/--------- 
|       N1010        |
|    (PASSENGER)     |
| Occupancy:  30/30  |
 ---------||--------- 
 ---------\/--------- 
|       F0001        |
|   (FIRST CLASS)    |
| Occupancy:   0/10  |
 ---------||--------- 
 ---------\/--------- 
|       N0001        |
|      (BUFFET)      |
| Occupancy:  15/30  |
 ---------||--------- 
 ---------\/--------- 
|       R0001        |
|     (RESTROOM)     |
| Occupancy:   0/30  |
 ---------||--------- 
Enter command: h N1010
N1010 happiness: 0.00
Enter command: H
Average happiness: 33.33
Enter command: [CTRL+D]
Goodbye

Challenge 1.2 - Optimise Happiness

Command

O

Description

This command should rearrange the passengers in the selected train to maximise their average happiness. To accomplish this, you may move passengers anywhere in the train (carriage capacity allowing), but you cannot add or remove passengers from the train entirely.

If multiple configurations of passengers result in the same maximum happiness, this command should result in the configuration which prioritises placing passengers towards the head of the train.

For example:

A linked list
Configuration 1: average happiness: 44.55
A linked list
(Incorrect) Configuration 2: average happiness: 44.55

In the above diagrams, both configurations result in the same average happiness. The first configuration is considered correct, however, as it prioritises placing passengers toward the start of the train.

Provided helper function

One helper function has bee provided for this stage:

  • int compare_double(double n1, double n2)
    • Takes in two floating point (double) values: n1 and n2.
    • Returns:
      0: Uf the two values are considered equal.
      A negative integer: If n1 is less than n2,
      A positive integer: If n2 is less than n1.

You should use this function if you need to test whether one double value is greater/lesser/equal to another.

This is to avoid inconsistencies from double imprecision.

Errors: N/A

Examples

dcc cs_cs.c -o cs_cs
./cs_cs
Welcome to Carriage Simulator
All aboard!
Enter command: a N1001 passenger 10
Carriage: 'N1001' attached!
Enter command: a N1002 passenger 10
Carriage: 'N1002' attached!
Enter command: s N1001 11
10 passengers added to N1001
1 passengers added to N1002
Enter command: p
 ---------\/--------- 
|       N1001        |
|    (PASSENGER)     |
| Occupancy:  10/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1002        |
|    (PASSENGER)     |
| Occupancy:   1/10  |
 ---------||--------- 
Enter command: O
Enter command: p
 ---------\/--------- 
|       N1001        |
|    (PASSENGER)     |
| Occupancy:   6/10  |
 ---------||--------- 
 ---------\/--------- 
|       N1002        |
|    (PASSENGER)     |
| Occupancy:   5/10  |
 ---------||--------- 
Enter command: H
Average happiness: 44.55
Enter command: [CTRL+D]
Goodbye




Assessment

Assignment Conditions

  • Joint work is not permitted on this assignment.

    This is an individual assignment.

    The work you submit must be entirely your own work. Submission of any work even partly written by any other person is not permitted.

    The only exception being if you use small amounts (< 10 lines) of general purpose code (not specific to the assignment) obtained from a site such as Stack Overflow or other publicly available resources. You should attribute the source of this code clearly in an accompanying comment.

    Assignment submissions will be examined, both automatically and manually for work written by others.

    Do not request help from anyone other than the teaching staff of COMP1511.

    Do not post your assignment code to the course forum - the teaching staff can view assignment code you have recently autotested or submitted with give.

    Rationale: this assignment is an individual piece of work. It is designed to develop the skills needed to produce an entire working program. Using code written by or taken from other people will stop you learning these skills.

  • The use of code-synthesis tools, such as GitHub Copilot, is not permitted on this assignment.

    Rationale: this assignment is intended to develop your understanding of basic concepts. Using synthesis tools will stop you learning these fundamental concepts.

  • Sharing, publishing, distributing your assignment work is not permitted.

    Do not provide or show your assignment work to any other person, other than the teaching staff of COMP1511. For example, do not share your work with friends.

    Do not publish your assignment code via the internet. For example, do not place your assignment in a public GitHub repository.

    Rationale: by publishing or sharing your work you are facilitating other students to use your work, which is not permitted. If they submit your work, you may become involved in an academic integrity investigation.

  • Sharing, publishing, distributing your assignment work after the completion of COMP1511 is not permitted.

    For example, do not place your assignment in a public GitHub repository after COMP1511 is over.

    Rationale:COMP1511 sometimes reuses assignment themes, using similar concepts and content. If students in future terms can find your code and use it, which is not permitted, you may become involved in an academic integrity investigation.

Violation of the above conditions may result in an academic integrity investigation with possible penalties, up to and including a mark of 0 in COMP1511 and exclusion from UNSW.

Relevant scholarship authorities will be informed if students holding scholarships are involved in an incident of plagiarism or other misconduct. If you knowingly provide or show your assignment work to another person for any reason, and work derived from it is submitted - you may be penalised, even if the work was submitted without your knowledge or consent. This may apply even if your work is submitted by a third party unknown to you.

If you have not shared your assignment, you will not be penalised if your work is taken without your consent or knowledge.

For more information, read the UNSW Student Code , or contact the course account. The following penalties apply to your total mark for plagiarism:

0 for the assignment Knowingly providing your work to anyone and it is subsequently submitted (by anyone).
0 for the assignment Submitting any other person's work. This includes joint work.
0 FL for COMP1511 Paying another person to complete work. Submitting another person's work without their consent.

Submission of Work

You should submit intermediate versions of your assignment. Every time you autotest or submit, a copy will be saved as a backup. You can find those backups  here , by logging in, and choosing the yellow button next to ass2_cs_cs.

Every time you work on the assignment and make some progress, you should copy your work to your CSE account and submit it using the give  command below.

It is fine if intermediate versions do not compile or otherwise fail submission tests.

Only the final submitted version of your assignment will be marked.

You submit your work like this:

  give cs1511 ass2_cs_cs cs_cs.c

The only files you should modify is cs_cs.c. It is not possible to add new files, or modify the other files we have given you. If you believe you need to modify those files, you have misunderstood the specification. You should not modify your copies of those files in any way.

Assessment Scheme

This assignment will contribute 25% to your final mark.

80% of the marks for this assignment will be based on the performance of the code you write in cs_cs.c.

10% of the marks for this assignment will be given from the automatic style checker that will be run on your code. The utility 1511 style can help with this! 10% of the marks for this assignment will come from manual marking of the readability of the C you have written. The manual marking will involve checking your code for clarity, and readability, which includes the use of functions and efficient use of loops and if statements.

Marks for your performance will be allocated roughly according to the below scheme.

Stage Command(s) Example Meaning Marks
1.1 n/a n/a Implementing the create_carriage function. 5%
1.2 ? ? Implementing the command loop and the Help command 5%
1.3 a [carriage_id] [type] [capacity] a N1001 passenger 20 Adds a carriage to the end of the train 10%
1.4 p p Prints all carriages in the train 5%
1.5 n/a n/a Implementing error handling for the Add Carriage command 10%
End stage 1 35%
2.1 i [n] [carriage_id] [type] [capacity] i 10 P1111 passenger 40 Inserts a carriage into the train at a given position 5%
2.2 s [carriage_id] [n] s B5000 20 Seats passengers starting from a specified carriage 10%
2.3 d [carriage_id] [n] d N0001 20 Disembarks passengers from a specified carriage 5%
2.4
T

c [start_id] [end_id]
T

c N0001 N0005
Counts the total number of passengers in the train

Counts the number of passengers in a range of carriages
5%
2.5 m [source_id] [destination_id] [n] m N0005 N0001 10 Moves passengers from one carriage to another 5%
End stage 2 65%
3.1
N

<

>
n/a
Creates a new empty train

Selects the previous train

Selects the next train
5%
3.2 P P Prints all the trains in the list 5%
3.3 r [carriage_id] r N1001 Removes a carriage from the current train 5%
3.4 R R Removes the current train from the list, and frees all memory on program exit 5%
End stage 3 85%
4.1 M M Merges two trains together 5%
4.2 S [n] S 5 Scans in carriage ids and performs multiple splits on the current train 10%
End stage 4 100%

Marks for your style will be allocated roughly according to the scheme below.

Style Marking Rubric

0 1 2 3 4
Formatting (/5)
Indentation (/2) - Should use a consistent indentation scheme. Multiple instances throughout code of inconsistent/bad indentation Code is mostly correctly indented Code is consistently indented throughout the program
Whitespace (/1) - Should use consistent whitespace (for example, 3 + 3 not 3+ 3) Many whitespace errors No whitespace errors
Vertical Whitespace (/1) - Should use consistent whitespace (for example, vertical whitespace between sections of code) Code has no consideration for use of vertical whitespace Code consistently uses reasonable vertical whitespace
Line Length (/1) - Lines should be max. 80 characters long Many lines over 80 characters No lines over 80 characters
Documentation (/5)
Comments (incl. header comment) (/3) - Comments have been used throughout the code above code sections and functions to explain their purpose. A header comment (with name, zID and a program description) has been included No comments provided throughout code Few comments provided throughout code Comments are provided as needed, but some details or explanations may be missing causing the code to be difficult to follow Comments have been used throughout the code above code sections and functions to explain their purpose. A header comment (with name, zID and a program description) has been included
Function/variable/constant naming (/2) - Functions/variables/constants names all follow naming conventions in style guide and help in understanding the code Functions/variables/constants names do not follow naming conventions in style guide and help in understanding the code Functions/variables/constants names somewhat follow naming conventions in style guide and help in understanding the code Functions/variables/constants names all follow naming conventions in style guide and help in understanding the code
Organisation (/5)
Function Usage (/4) - Code has been decomposed into appropriate functions separating functionalities No functions are present, code is one main function Some functions are present, but functions are all more than 50 lines Some functions are present, and all functions are approximately 50 lines long Most code has been moved to sensible/thoought out functions, but they are mostly more than 50 lines (incl. main function) All code has been meaningfully decomposed into functions of approx 50 lines (incl. main function)
Function Prototypes (/1) - Function Prototypes have been used to declare functions above main Functions are used but have not been prototyped All functions have a prototype above the main function or no functions are used
Elegance (/5)
Overdeep nesting (/2) - You should not have too many levels of nesting in your code (nesting which is 5 or more levels deep) Many instances of overdeep nesting <= 3 instances of overdeep nesting No instances of overdeep nesting
Code Repetition (/2) - Potential repetition of code has been dealt with via the use of functions or loops Many instances of repeated code sections <= 3 instances of repeated code sections Potential repetition of code has been dealt with via the use of functions or loops
Constant Usage (/1) - Any magic numbers are #defined None of the constants used throughout program are #defined All constants used are #defined and are used consistently in the code
Illegal elements
Illegal elements - Presence of illegal elements including: Global Variables, Static Variables, Labels or Goto Statements CAP MARK AT 16/20

Note that the following penalties apply to your total mark for plagiarism:

0 for the assignment Knowingly providing your work to anyone and it is subsequently submitted (by anyone).
0 for the assignment Submitting any other person's work. This includes joint work.
0 FL for COMP1511 Paying another person to complete work. Submitting another person's work without their consent.
If you choose to disregard this advice, you **must** still follow the style guide.

You also may be unable to get help from course staff if you use features not taught in COMP1511. Features that the Style Guide strongly discourages or bans will be penalised during marking. You can find the style marking rubric above. Please note that this assignment must be completed using only Linked Lists . Do not use arrays in this assignment.

Due Date

This assignment is due 21 April 2023 20:00:00.

Change Log

Version 1.0
(2023-30-03 11:00)
  • Assignment Released
Version 1.01
(2023-09-04 11:15)
  • Fix inconsistency in Challenge 1.1. Specify that multiplicative bonuses should be applied after other bonuses, not before.
Version 1.02
(2023-11-04 10:10)
  • Correction: All memory should be freed from Stage 3.4 onwards.
Version 1.03
(2023-15-04 21:20)
  • Stage 4.2: 'Skipping' should be printed out when an invalid id is entered.