Run-time Error 2

Can anyone explain to me why I am getting a run-time error when I run this program?



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
#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 passenger = 0;
	while(passenger <= 98)
	{
		while(seats[(passenger +1)] == -1)
		{
			seats[(passenger + 1)] = passenger + 1;
			passenger++;
		}
		int new_random_seat = rand() % (100 - passenger);
		if(new_random_seat == 0 && seats[0] == unoccupiedSeat)
			seats[new_random_seat] = (passenger + 1);
		else
		{
			new_random_seat = (new_random_seat + passenger + 1);
			seats[new_random_seat] = (passenger + 1);
		}
		passenger++;
	}
	cout << passenger << 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 << 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 << endl;
}
The pop-up box states

Run -time check failure #2 - Stack around the variable 'seats' was corrupted.
1
2
3
4
5
6
7
        else
        {
            //problem here - new_random_seat goes over 99 which will be outside
            //the seats array boundary.
            new_random_seat = (new_random_seat + passenger + 1);
            seats[new_random_seat] = (passenger + 1);
        }
Topic archived. No new replies allowed.