Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C or C++ Train simulation Program Write a program that simulates trains at a switching station. At the beginning of the simulation, there are n

C or C++ Train simulation Program

Write a program that simulates trains at a switching station. At the beginning of the simulation, there are n trains. Each starts with c cars. Each car has a fixed size s that limits how much it can carry. The sizes of the cars in each train are 1 ... c, from the front to the back of the train. Trains are loaded with more valuable materials toward the back of the train, so the value of a train is the sum of p*s, where p is the position of each car (starting at 1) and s is its size. So the starting value of each train is 1*1 + 2*2 + ... + c*c = c(c+1)(2c+1)/6.

At each step, the simulation tosses an n-sided die, which indicates which train is the donor of this step. The simulation then tosses tosses a 2-sided die, indicating whether to remove a car from the front (if the die shows 1) or the back of the donor train (if the die shows 2). The simulation then tosses an n-sided die, indicating which train accepts the donated car. It is possible for the donor to get its own car back. The accepting train puts the donated car at its front. When a car moves to a new position, it retains its original size, but the values of the train it left and the train it joins do change, because cars are now in a different position. The simulation ends when any train becomes empty or t steps, whichever comes first.

2. What to do Write a program called trains that simulates the switching station. This program must take n, c, and t as command-line parameters. The program must fail gently if any of these parameters is ridiculous, such as n = 0 or c = -5. The program must read a stream of integers from standard input as its source of randomness; this feature allows us to test the program in a deterministic way. After each step, the program should print (to standard output) which train donates a car (front or back) to which recipient and the value of each train after the step. When the simulation finishes (either because a train has become empty or the number of steps has reached its limit), the program prints which train has the largest value (if there is a tie, select the earliest train in the collection).

The program must use a doubly-linked list to implement the trains You must not use a library routine (such as C++ std::list) for linked lists; you must write your own routines. This restriction is frustrating, but the point of the exercise is to learn how these lists work. You may use array, struct, and class elements, but not C++ vectors. Try to make the program modular, with separate routines for the main loop, for reading the next random number, for evaluating the value of a train, and for donating and accepting a car.

The output looks like this:

image text in transcribed

turn 1: train 4 sends a car to train 1, from front train 1: value: 113 train 2: value: 91 train 3: value: 91 train 4: value: 70 train 5: value: 91 turn 2: train 5 sends a car to train 2, from front train 1: value: 113 train 2: value: 113 train 3: value: 91 train 4: value: 70 train 5: value: 70 turn 3: train 1 sends a car to train 1, from front train 1: value: 113 train 2: value: 113 train 3: value: 91 train 4: value: 70 train 5: value: 70 turn 4: train 1 sends a car to train 5, from end train 1: value: 71 train 2: value: 113 train 3: value: 91 train 4: value: 70 train 5: value: 96 turn 5: train 3 sends a car to train 2, from front train 1: value: 71 train 2: value: 136 train 3: value: 70 train 4: value: 70 train 5: value: 96 turn 6: train 2 sends a car to train 3, from front turn 1: train 4 sends a car to train 1, from front train 1: value: 113 train 2: value: 91 train 3: value: 91 train 4: value: 70 train 5: value: 91 turn 2: train 5 sends a car to train 2, from front train 1: value: 113 train 2: value: 113 train 3: value: 91 train 4: value: 70 train 5: value: 70 turn 3: train 1 sends a car to train 1, from front train 1: value: 113 train 2: value: 113 train 3: value: 91 train 4: value: 70 train 5: value: 70 turn 4: train 1 sends a car to train 5, from end train 1: value: 71 train 2: value: 113 train 3: value: 91 train 4: value: 70 train 5: value: 96 turn 5: train 3 sends a car to train 2, from front train 1: value: 71 train 2: value: 136 train 3: value: 70 train 4: value: 70 train 5: value: 96 turn 6: train 2 sends a car to train 3, from front

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Greed And Fear Understanding Behavioral Finance And The Psychology Of Investing

Authors: Hersh Shefrin

1st Edition

0195161211, 978-0195161212

Students also viewed these Databases questions

Question

What would the person you most admire do?

Answered: 1 week ago