Generating Random Number

I recieved this assignment for my beginner's c++ course and I am having trouble writing this program. Specifically, I am having trouble with the part that I have written in bold. If anyone could help that would be great!

Sorry about the length :(


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 what I have written so far.
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
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;

//Function Prototypes
void display_seating(int []);



int main()
{
	
	unsigned seed = time(0);
	srand(seed);
	const int unoccupiedSeat = -1, num_passengers = 100;
	int seats[num_passengers];
	int passengers[num_passengers];
	
	//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, first_pass_seat = rand()%100;
	seats[first_pass_seat] = first_passenger;









	display_seating(seats);
	cout << endl;
	return 0;
}


void display_seating(int seats[])
{	
	/*
					------------------------
					  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;
}
There is a couple of ways I can think of off the top of my head.

You can create an array that removes unavailable numbers and use random values and array length to find a seat.

You can store the seats as pointers and if a seat is taken take the pointer and swap it with another pointer either top or bottom filling and then using random numbers to find the available pointer.
I think the core of this problem is how to generate a random 100-number series varying from 0 to 99.

I don't know if there is a made function that can do this or not. This is my program calculating this, but I can see no efficiency in it, in other words, it looks like almost a dead loop though it isn't.

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

#define max 100

int main()
{
    int s[max];
    unsigned seed=time(0);
    s[0]=rand(seed)%max;
    for(int i=1;i<max;i++)
    {
        int k=0;
        while (k!=i)
        {
            seed=time(0);
            s[i]=rand(seed)%max;
            for(int j=0;j<i+1;j++)
            {
                if (s[i]!=s[j]) k++;
                std::cout<<j;
            }
        }
    }
    for(int n=0;n<max;n++)
    {
        std::cout<<s[n]<<" ";
    }
    std::cin.get();
    return 0;
}


--- --- ---

Actually I have a question here, rand(seed) doesn't work for me. Then it shows me this:

_CRTIMP int __cdecl __MINGW_NOTHROW rand (void); in stdlib.h.
error: too many arguments to function 'int rand()'

Could you please tell me how to fix this?
Last edited on
closed account (z05DSL3A)
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
@cbeginplusplus
How else could you think of doing this? I am not allowed to use pointers because I technically I have not learned them in my class yet.
closed account (D80DSL3A)
The problem suggests using 2 arrays. Let these be:
1
2
int seatAssigns[100] = {5,11,17,23,...};// the full list is given in the problem
int seatsAvailable[100];// initialize all of these = -1 as suggested to indicate the seats are available. 


Suppose that first boob takes seat #52 instead of his assigned seat #5 (as in the example given).
Assign seat #52 as taken with seatsAvailable[52] = 0;

Use an int variable to count the #of people who didn't get their assigned seat. If the boob counts then int badSeat = 1; at this point (or 0 if the boob doesn't count as not getting his seat).

Note that boarding goes fine until the passenger assigned to seat #52 boards. In the example lineup this is passenger #24. We know there is a problem because:
seatsAvailable[ seatAssign[24] ] = seatsAvailable[52] == 0 because it was taken by passenger 0
What seat shall passenger #24 take? Either passenger 0's seat (#5) or any seat assigned to passengers who have not yet boarded. The available seats are seatAssign[0] and seatAssign[25] through seatAssign[99]. There are 24 seats taken so there are 100-24 = 76 free. Generate a random # from 0 to 75 with seatIdx = rand()%76.
If seatIdx == 0 then passenger #24 takes seatAssign[0] (the boobs assigned seat) - and the whole mess is straightened out- otherwise passenger #24 takes seat# = seatAssign[ 24 + seatIdx ]. Either way, passenger #24 didn't get his assigned seat so badSeat++;

I think this reasoning will work. You will need to generalize it and follow it through the entire boarding process. You want to keep track of cases when the last and/or next to last passengers don't get their seats (for the 3 questions).

Best luck.
@Grey Wolf
Got it. Thank you very much.

@fun2code
You must have misunderstood the problem.
int seatAssigns[100] = {5,11,17,23,...};// the full list is given in the problem
The number series given in this problem is not the seat number sequence of the boarders. Look closer, it's the assigned seat number sequence in the plane.

To solve this problem, first you must generate different random seat number sequences of the boarders which is a serial varying from 0 to 99. Just as what I said before, I don't know if there is a made function that can do this or not.

I have another solution here to accomplish this, but it still has a problem.

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
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>

#define max 100

int se[max];
int i=0;
int randseries(int n_min,int n_max);

int main()
{
    randseries(0,max-1);
    for(int j=0;j<max;j++)
    {
        printf("%d",se[j]);
        printf(" . ");
    }
    std::cin.get();
}

int randseries(int n_min,int n_max)
{
    int di=n_max-n_min+1;
    int init;
    srand(time(NULL));
    init=rand()%di+n_min;
    se[i]=init;
    i++;
    if ((i<max)&&(n_min<n_max))
    {
        if (init==n_min)
        {
            randseries(n_min+1,n_max);
        }
        else
        {
            if (init==n_max)
            {
                randseries(n_min,n_max-1);
            }
            else
            {
             randseries(n_min,init-1);
             randseries(init+1,n_max);
            }
        }
    }
    return 0;
}


The problem comes from the codes in bold which results in a specific rule of the random sequence. Anybody any ideas about it?
Last edited on
Topic archived. No new replies allowed.