Lab #6: Strings, Recursion and Structures
CS1010 AY2017/8 Semester 1
Date of release: 25 October 2017, Wednesday, 5pm.
Submission deadline: 8 November 2017, Wednesday, 5pm.
School of Computing, National University of Singapore

0 Introduction

Important: Please read Lab Guidelines before you continue.

This is your final lab! *Hooray!* ☺☺☺

This lab consists of 3 exercises. You are required to submit 2 exercises. If you submit 3 exercises, the best 2 out of 3 exercises will be used to determine your attempt mark.

The main objective of this lab is on the use of characters and strings, recursion, and structures to solve problems.

The maximum number of submissions for each exercise is 10.

If you have any questions on the task statements, you may post your queries on the relevant IVLE discussion forum. However, do not post your programs (partial or complete) on the forum before the deadline!

Important notes applicable to all exercises here:

1 Exercise 1: Prerequisites

1.1 Learning objectives

1.2 Task

National Programming Academy (NPA) provides several modules for its students. All module codes are formatted as strings of length 6, with 2 characters followed by 4 digits. For instance, these are some examples of module codes: CX1010, BT1234, GG1001, IZ4239.

In NPA, a module A is a prerequisite of another module B if and only if

  1. The first two characters of modules A and B are the same,
  2. The first digit of module A is less than the first digit of B, and
  3. The other digits of module A are not greater than the corresponding digits of B at the same position.

For example,

But

You are to write a program prerequisites.c to read a positive integer indicating the number of modules, and a list of module codes. The program also reads a particular module code and if that module code exists, it displays all the prerequisites of that module in the same order as the entry of module codes.

You may assume that there are at least 2 and at most 10 module codes.

1.3 Sample runs

Sample run using interactive input (user's input shown in blue; output shown in bold purple). Note that the first two lines (in green below) are commands issued to compile and run your program on UNIX.

Sample run #1:

$ gcc -Wall prerequisites.c -o prerequisites
$ prerequisites
Enter number of modules: 4
Enter 4 modules: 
CX2010
CX3110
IZ2010
CX1010
Choose a module: CX3110
Prerequisites for CX3110: CX2010 CX1010 

Sample run #2:

Enter number of modules: 4
Enter 4 modules: 
CX2010
CX3110
IZ2010
CX1010
Choose a module: CX1020
No such module CX1020 

Sample run #3:

Enter number of modules: 7
Enter 7 modules: 
CX2101
CX4000
CX3311
GG1211
CX2112
CX3300
CX3221
Choose a module: CX3311
Prerequisites for CX3311: CX2101 

Sample run #4:

Enter number of modules: 3
Enter 3 modules: 
IZ1111
IZ2222
IZ3333
Choose a module: IZ1111
No prerequisites for IZ1111 

1.4 Skeleton program and Test data

1.5 Important notes

1.6 Estimated development time

The time here is an estimate of how much time we expect you to spend on this exercise. If you need to spend way more time than this, it is an indication that some help might be needed.

2 Exercise 2: The Driver's Problem

2.1 Learning objectives

2.2 Task

Bill owns a car and wishes to drive on a long straight road for a total distance of D kilometres. Along the road, there are N gas stations. Each gas station i is at distance di from the start point and has fi decalitres of fuel available.

(The above symbols such as D and N are used for convenience in this write-up. In your program, you should use descriptive variable names for them.)

To make the problem simpler, you may assume that the initial fuel in the car is always 100 decalitres. One decalitre of fuel provides enough fuel to cover a distance of one kilometre.

Write a program driver.c to read the total distance, the number of gas stations, and for each gas station, its distance from the start and the amount of fuel available. All values are positive integers. You may assume that there are at most 20 gas stations and no two gas stations are at the same distance from the start. Also, the gas stations are entered in increasing order of their distance from the start. If Bill stops by a gas station, he will take all the available fuel in that station.

Your program is to compute the total number of possible routes Bill can take to complete his journey.

For example, refer to the following diagram.

In this example, Bill wishes to cover 200km. There are 3 gas stations:

In this case, there are two possible routes for Bill:

Therefore the answer is 2.

2.3 Sample runs

Sample run using interactive input (user's input shown in blue; output shown in bold purple). Note that the first two lines (in green below) are commands issued to compile and run your program on UNIX.

Sample run #1:

$ gcc -Wall driver.c -o driver
$ driver
Enter total distance: 200
Enter number of gas stations: 3
Enter distance and amount of fuel for each gas station:
90 20
110 100
190 40
Total distance = 200
Number of gas stations = 3
Gas stations' distances:   90  110  190
Gas stations' fuel amt :   20  100   40
Possible number of routes = 2 

Sample run #2:

Enter total distance: 300
Enter number of gas stations: 3
Enter distance and amount of fuel for each gas station:
90 20
110 100
190 40
Total distance = 300
Number of gas stations = 3
Gas stations' distances:   90  110  190
Gas stations' fuel amt :   20  100   40
Possible number of routes = 0 

2.4 Skeleton program and Test data

2.5 Important notes

2.6 Estimated development time

The time here is an estimate of how much time we expect you to spend on this exercise. If you need to spend way more time than this, it is an indication that some help might be needed.

3 Exercise 3: Elevators

3.1 Learning objectives

3.2 Task statement

Brusco is a big fan of a vintage PC game called SimTower. He is particular fascinated by how the game simulates the elevators which transport the residents of the tower to different floors.

After learning more about structures in CS1010, he realized that they can be used to model almost everything in real-life, including elevators. Therefore, he decided to write a a simple program to simulate the running of elevators.

To keep things simple, he has decided to model elevators with the following three pieces of information:

In addition, he has also set the following rules on the running of the elevators:

For example, if the sequence of floors is 7->9->8->6->3->5->..., then the movement of the elevator is as follows:

Last but not least, Brusco has also decided that, at the end of the simulation, all information (i.e., floor/passenger/usage) of all the elevators, as well as the elevator number of the most used elevator (see Section 2.5 on how to break ties), should be printed.

Brusco has managed to follow the first two stages of the problem-solving process to come up with a skeleton for his program; however, since he is not very familar with the syntax of structures yet, there is still quite a big chunk of his program which is yet to be completed.

You are to help Brusco complete the program elevator.c. This program takes in an integer i, which represent the number of elevators to be simulated, as well as i strings of digits, which represent the sequences of floors. The program then simulates the running of the elevators and prints the required outputs in the end.

3.3 Sample runs

Sample runs using interactive input (user's input shown in blue; output shown in bold purple). Note that the first two lines (in green below) are commands issued to compile and run your program on UNIX.

Sample run #1:

$ gcc -Wall elevator.c -o elevator
$ elevator
Enter number of elevators: 1
Enter sequence for elevator 1: 24653
Elevator 1:
Floor: 3
Number of passengers: 4
Usage: 8
Most used elevator: 1 

Sample run #2:

Enter number of elevators: 1
Enter sequence for elevator 1: 798635
Elevator 1:
Floor: 5
Number of passengers: 5
Usage: 15
Most used elevator: 1

Sample run #3:

Enter number of elevators: 2
Enter sequence for elevator 1: 24653
Enter sequence for elevator 2: 798635
Elevator 1:
Floor: 3
Number of passengers: 4
Usage: 8
Elevator 2:
Floor: 5
Number of passengers: 5
Usage: 15
Most used elevator: 2

3.4 Skeleton program and Test data

3.5 Important notes

3.6 Estimated development time

The time here is an estimate of how much time we expect you to spend on this exercise. If you need to spend way more time than this, it is an indication that some help might be needed.

4 Deadline

The deadline for submitting all programs is 8 November 2017, Wednesday, 5pm. Late submission will NOT be accepted.




Last updated: 20 October 2017