1. Homepage
  2. Programming
  3. COMP1511 Programming Fundamentals - Assignment 2 - Carriage Simulator

COMP1511 Programming Fundamentals - Assignment 2 - Carriage Simulator

Engage in a Conversation
CCOMP1511Programming FundamentalsAustraliaUNSWAssignment 2 - Carriage SimulatorDPST1091

Assignment 2 - Carriage Simulator

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

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. CourseNana.COM

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. CourseNana.COM

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

CourseNana.COM

// 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;
};

CourseNana.COM

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] CourseNana.COM

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

    • The type of carriage as an enum.
    • Can be either PASSENGERBUFFETRESTROOM, or FIRST_CLASS.
  • int capacity CourseNana.COM

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

    • 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 CourseNana.COM

    • The pointer to the next carriage in the train.

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

The following enum definition is also provided for you: CourseNana.COM

CourseNana.COM

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

CourseNana.COM

Purpose:

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

Values:

  • PASSENGER CourseNana.COM

    • A regular passenger carriage.
  • BUFFET CourseNana.COM

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

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

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

    • 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. CourseNana.COM

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. CourseNana.COM

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

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

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

    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. CourseNana.COM

  • Stage 1.3 onwards - Implement the commands: CourseNana.COM

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

How To Get Started

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

  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. CourseNana.COM

  2. Start coding! CourseNana.COM

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. CourseNana.COM

To run the reference implementation, use the following command: CourseNana.COM

 1511 cs_cs

You might want to start by running the ? command: CourseNana.COM

  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. CourseNana.COM

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. CourseNana.COM

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

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. CourseNana.COM

On program start: CourseNana.COM

Welcome to Carriage Simulator
All aboard!

On program end: CourseNana.COM

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. CourseNana.COM

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: CourseNana.COM

  • 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). CourseNana.COM

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. CourseNana.COM

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

CourseNana.COM

CourseNana.COM

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. CourseNana.COM

Specifically, this will include: CourseNana.COM

  • 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: CourseNana.COM

CourseNana.COM

struct carriage *train:
struct carriage

next

0
occupancy:

20
capacity:

PASSENGER
type:
carriage_id:

"N1001"
struct carriage

next

0
occupancy:

50
capacity:

BUFFET
type:
carriage_id:

"N1002"
X
struct carriage

next

0
occupancy:

10
capacity:

FIRST_CLASS
type:
carriage_id:

"N1005"
Click To Expand Diagram
Stage 1: A linked list of carriages

CourseNana.COM

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. CourseNana.COM

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

CourseNana.COM

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; 
}

CourseNana.COM

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

  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. CourseNana.COM

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

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

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

CourseNana.COM

///////////////////////////// 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 /////////////////////////////

CourseNana.COM

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

If you run it, it should print out something like: CourseNana.COM

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. CourseNana.COM

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: CourseNana.COM

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

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

    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. CourseNana.COM

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. CourseNana.COM

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

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. CourseNana.COM

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

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. CourseNana.COM

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

CourseNana.COM

CourseNana.COM

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

Enter command: 

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

CourseNana.COM

Provided helper functions

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

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

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

CourseNana.COM

CourseNana.COM

CourseNana.COM

CourseNana.COM

CourseNana.COM

CourseNana.COM

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. CourseNana.COM

It should look something like this: CourseNana.COM

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. CourseNana.COM

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. CourseNana.COM

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

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

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

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

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. CourseNana.COM

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

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

Our linked list should now look like this: CourseNana.COM

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

If we then run the a command again: CourseNana.COM

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

then our linked list should now look like: CourseNana.COM

CourseNana.COM

struct carriage *train:
struct carriage

next

0
occupancy:

10
capacity:

PASSENGER
type:
carriage_id:

"N1001"
struct carriage

next

0
occupancy:

20
capacity:

BUFFET
type:
carriage_id:

"N1002"
X
Click To Expand Diagram
A train with two carriages

CourseNana.COM

CourseNana.COM

Provided helper functions

Two helper functions have been provided for this stage: CourseNana.COM

  • 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. CourseNana.COM

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

CourseNana.COM

...
// 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
...

CourseNana.COM

Errors

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

Assumptions

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

  • 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. CourseNana.COM

    Corresponding stringenum carriage_type
    “passenger”PASSENGER
    "first_class"FIRST_CLASS
    “buffet"BUFFET
    “restroom”RESTROOM
    any invalid stringINVALID_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. CourseNana.COM

Examples

CourseNana.COM

CourseNana.COM

CourseNana.COM

CourseNana.COM

Stage 1.4 - Printing out the train

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

Command: Print train

p

Description

The p command takes no arguments. CourseNana.COM

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

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

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

CourseNana.COM

Provided helper functions

One helper function has been provided for this stage: CourseNana.COM

  • 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. CourseNana.COM

Errors/Assumption/Clarifications: N/A

Examples

CourseNana.COM

CourseNana.COM

CourseNana.COM

CourseNana.COM

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! CourseNana.COM

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: CourseNana.COM

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?? CourseNana.COM

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. CourseNana.COM

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

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. CourseNana.COM

Error Conditions:

When running the a command, you scanned in the carriage_idtype and capacity for the new carriage. CourseNana.COM

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: CourseNana.COM

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

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

  • 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 CourseNana.COM

    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? CourseNana.COM

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

CourseNana.COM

CourseNana.COM

CourseNana.COM

CourseNana.COM

Testing and Submission

Remember to do your own testing CourseNana.COM

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

  • 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


Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
C代写,COMP1511代写,Programming Fundamentals代写,Australia代写,UNSW代写,Assignment 2 - Carriage Simulator代写,DPST1091代写,C代编,COMP1511代编,Programming Fundamentals代编,Australia代编,UNSW代编,Assignment 2 - Carriage Simulator代编,DPST1091代编,C代考,COMP1511代考,Programming Fundamentals代考,Australia代考,UNSW代考,Assignment 2 - Carriage Simulator代考,DPST1091代考,Chelp,COMP1511help,Programming Fundamentalshelp,Australiahelp,UNSWhelp,Assignment 2 - Carriage Simulatorhelp,DPST1091help,C作业代写,COMP1511作业代写,Programming Fundamentals作业代写,Australia作业代写,UNSW作业代写,Assignment 2 - Carriage Simulator作业代写,DPST1091作业代写,C编程代写,COMP1511编程代写,Programming Fundamentals编程代写,Australia编程代写,UNSW编程代写,Assignment 2 - Carriage Simulator编程代写,DPST1091编程代写,Cprogramming help,COMP1511programming help,Programming Fundamentalsprogramming help,Australiaprogramming help,UNSWprogramming help,Assignment 2 - Carriage Simulatorprogramming help,DPST1091programming help,Cassignment help,COMP1511assignment help,Programming Fundamentalsassignment help,Australiaassignment help,UNSWassignment help,Assignment 2 - Carriage Simulatorassignment help,DPST1091assignment help,Csolution,COMP1511solution,Programming Fundamentalssolution,Australiasolution,UNSWsolution,Assignment 2 - Carriage Simulatorsolution,DPST1091solution,