I have been working on this code for the past few days and I am to the point where I am ready to give up. I don't understand why this is not working. The build succeeds through the compiler but the program will not run.
I was given this problem and my solution is pasted below but I cannot figure out what I am doing wrong within the code.
An important application of computers is simulation. When it is
impossible or impractical to perform an event in the real world, a com-
puter simulation of the event is often the best option. For this assign-
ment you will simulate a particular scenario of passengers boarding a
plane.
Imagine a plane with 100 seats and 100 passengers waiting to board
the plane. Every passenger has a boarding pass with a reserved seat
number. The first passenger to board the plane drops their boarding
pass. Rather than pick it up, he continues onto the plane and sits
in a randomly selected seat. Every person boarding after him uses
the following algorithm to decide where to sit: if their assigned seat
is available, they sit in it; if their assigned seat is taken then they
randomly choose one of the remaining available seats.
The purpose of the simulation is to help us answer three questions:
(1) How often does the last person on board get to sit in their assigned
seat? (2) How often does the penultimate (next-to-last) person get to
sit in their assigned seat? (3)What percentage of people typically get to
sit in their own seat? To obtain reasonable answers to these questions
you should run the simulation 10000 times and average the results.
Give answers to all three questions as percentages. Finally, display
the seating obtained from the final run of your simulation. Assume
the plane has 17 rows with 6 seats in each row except for the last row
which only has 4 seats. Assume the seats and passengers are numbered
0 through 99. The first row contains seats 0 through 5 running right
to left as you look back from the front of the plane. If everyone sits in
their assigned seat the seating would look like this:
05 11 17 23 29 35 41 47 53 59 65 71 77 83 89 95
04 10 16 22 28 34 40 46 52 58 64 70 76 82 88 94
03 09 15 21 27 33 39 45 51 57 63 69 75 81 87 93 99
02 08 14 20 26 32 38 44 50 56 62 68 74 80 86 92 98
01 07 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97
00 06 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
An important aspect of your solution will be the representation of seats
on the plane, and, in particular, keeping track of the remaining avail-
able (unoccupied) seats at any time. Use an integer array to represent
the seats and use a special value (-1 is a good choice) to indicate that
a seat is unoccupied. The ith element of the array represents the ith
seat. Use integer values to represent passengers. For example, if the
first passenger (0) sits in seat 52, then the 52nd element of your array
should be assigned the value 0. In the proper seating given above the
ith element of the array contains the value i.
When a passenger is forced to sit in a seat other than their assigned
seat you must find them a random unoccupied seat. You must not
generate random seat numbers (between 0 and 99) until you find one
that is unoccupied. Instead, you should design a way to generate one
random number to select the passengers seat. (There are a couple ways
of doing this.)
Be sure practice good coding style. Do not use literal constants (like
100 or 100000) in your programming - use defined constants instead.
Use appropriate data structures (arrays) and looping constructs. Use
functions where appropriate to keep the size of your main program
to a reasonable size.
This is my solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;
//Function Prototypes
void display_seating(int [], int);
int main()
{
unsigned seed = time(0);
const int unoccupiedSeat = -1, num_passengers = 100, first_passenger = 0;
int seats[num_passengers];
//Initialize all of the seats to the special value of -1 to show that they are unoccupied.
for(int i = 0; i < num_passengers; i++)
seats[i] = unoccupiedSeat;
//Seat the first passenger in a randomly assigned seat.
int random_seat = rand()%100;
seats[random_seat] = first_passenger;
int lowest = unoccupiedSeat;
int i = 1;
while(lowest == unoccupiedSeat)
{
for(; i < random_seat; i++)
if (seats[i] == -1)
seats[i] = i;
random_seat = rand() % (100 - i);
if(random_seat == 0 && seats[0] == unoccupiedSeat)
{
seats[random_seat] = i;
random_seat = 100;
}
else
{
random_seat += i;
seats[random_seat] = i;
}
int real_low = seats[0];
for (int counter = 1; counter < num_passengers; counter++)
{
if (seats[counter] < real_low)
real_low = seats[counter];
}
lowest = real_low;
}
cout << i << endl << endl;
display_seating(seats, num_passengers);
return 0;
}
void display_seating(int seats[], int num_passengers)
{
/*
------------------------
Function Description
------------------------
This function displays the final seating of the plane.
*/
for(int i = 5; i <= 95; i+=6)
cout << setw(2) << seats[i] << " ";
cout << endl;
for(int i = 4; i <= 94; i+=6)
cout << setw(2) << seats[i] << " ";
cout << endl;
for(int i = 3; i <= 99; i+=6)
cout << setw(2) << seats[i] << " ";
cout << endl;
for(int i = 2; i <= 98; i+=6)
cout << setw(2) << seats[i] << " ";
cout << endl;
for(int i = 1; i <= 97; i+=6)
cout << setw(2) << seats[i] << " ";
cout << endl;
for(int i = 0; i <= 96; i+=6)
cout << setw(2) << seats[i] << " ";
cout << endl << endl;
}
|