I'm trying to write a program that assigns seats in either first class(seats 1 through 5) or economy class(seats 6 through 10) using the random function. The array seats[ SIZE ] is supposed to hold a value of 0 or 1 to indicate whether it is already taken or not. My problem is when the random seat is already taken I want the function Assignseat() to run itself again recursively to try and find another seat. For some reason it won't run itself recursively. Any ideas?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
int assignSeat( int [], int );
int economyCounter = 0;
int firstCounter = 0;
int main()
{
srand( time(NULL) );
int seats[ SIZE ] = { 0 };
int input;
while (input != -1) {
printf("Enter 1 for first class, 2 for economy class or -1 to quit: ");
scanf("%d", &input);
/*
if (input == 1 || input == 2)
assignSeat( seats, input );
*/
if (input == 1) {
if (firstCounter < 5)
printf("You have seat %d in first class\n\n", assignSeat( seats, input ) );
else
printf("There are no more first class seats\n");
}
else if (input == 2)
printf("You have seat %d in economy class\n\n", assignSeat( seats, input ) );
else if (input == -1)
continue;
else
printf("You must enter 1, 2, or -1\n");
}
return 0;
}
int assignSeat( int seatNumber[], int class )
{
int i;
/*
if (class == 1) {
i = rand() % 5 + 1;
if (seatNumber[ i ] != 1) {
seatNumber[ i ] = 1;
++firstCounter;
printf("counter: %d\n", firstCounter);
}
else if (firstCounter < 5)
assignSeat( seatNumber, class );
}
*/
if (class == 1) {
i = rand() % 6 + 1;
if (seatNumber[ i ] == 0) {
seatNumber[ i ] = 1;
++firstCounter;
}
else
assignSeat( seatNumber, 1 );
}
else if (class == 2) {
i = rand() % 6 + 5;
if (seatNumber[ i ] != 1)
seatNumber[ i ] = 1;
else
assignSeat( seatNumber, 2 );
}
return i;
}