Error Message
Feb 28, 2011 at 12:26pm UTC
Every time I try to run the following program, an error message occurs saying Program5.exe has stopped working. I was wondering if anyone would be able to tell me why this keeps happening.
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 96 97 98 99 100 101 102 103
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;
//Function Prototypes
void display_seating(int [], int );
int main()
{
unsigned seed = time(0);
srand(seed);
const int unoccupiedSeat = -1, num_passengers = 100;
int seats[num_passengers];
int passengers[num_passengers];
int seat_stop = 1;
//for loop to initialize all seats to the special value of -1.
for (int i = 0; i < num_passengers; i++)
seats[i] = unoccupiedSeat;
//for loop to initialize passengers 0 to 99
for (int i = 0; i < num_passengers; i++)
passengers[i] = i;
//Seat the first passenger on the plane into a random seat.
const int first_passenger = 0;
int first_pass_seat = rand()%100;
seats[first_pass_seat] = passengers[first_passenger];
//Seats passengers in their assigned seats up to the first passenger's random seat.
while (seats[99] == -1)
{
while (seats[seat_stop] == -1)
{
seats[seat_stop] = passengers[seat_stop];
seat_stop++;
}
int new_seat = rand()%(100 - seat_stop);
if (new_seat == 0 && seats[0] == -1)
{
seats[new_seat] = passengers[seat_stop];
seat_stop++;
}
else
{
new_seat += seat_stop;
seats[new_seat] = passengers[seat_stop];
seat_stop++;
}
}
int last_person = 0, penultimate = 0, typical = 0;
if (seats[99] == 99)
last_person++;
else if (seats [98] == 98)
penultimate++;
for (int i = 0; i < num_passengers; i++)
if (seats[i] == passengers[i])
typical++;
display_seating(seats, num_passengers);
cout << endl;
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 << seats[i] << " " ;
cout << endl;
for (int i = 4; i <= 94; i+=6)
cout << seats[i] << " " ;
cout << endl;
for (int i = 3; i <= 99; i+=6)
cout << seats[i] << " " ;
cout << endl;
for (int i = 2; i <= 98; i+=6)
cout << seats[i] << " " ;
cout << endl;
for (int i = 1; i <= 97; i+=6)
cout << seats[i] << " " ;
cout << endl;
for (int i = 0; i <= 96; i+=6)
cout << seats[i] << " " ;
cout << endl;
}
Feb 28, 2011 at 12:42pm UTC
1 2 3 4 5
int seat_stop = 1;
//...
int new_seat = rand()%(100 - seat_stop);
//...
seat_stop++;
At some point
seat_stop will be 100 and you will get a divide by zero error when calculating
new_seat .
Topic archived. No new replies allowed.