Maha

Maha

  • NA
  • 0
  • 326.3k

Passenger and Seats

May 19 2014 9:37 PM
Originally this program was developed by Vulpes which is attached. There are three possible events can take place in this program.

-Passengers > Seats
-Passengers = Seats
-Passengers < Seats

This program is accommodating all possible events.

o/p passengers numbers were in random order in the original program. By modifying the program o/p passenger numbers were made in ascending order. Please see whether the program is correct.

Also in the modified program return keyword is not retaining the execution window. I don't know why. Problem is highlighted.

using System;

namespace AirlineSeats_try_
{
class Program
{
static void Main(string[] args)
{
Console.Write("No of Passangers: ");
int passengers = int.Parse(Console.ReadLine());

Console.Write("No of Seats: ");
int seats = int.Parse(Console.ReadLine());

while (passengers > seats)
{
Console.WriteLine("No of Passangers should be less than the seats");
Console.WriteLine("Try again Passangers");
Console.Write("No of Passangers: ");
passengers = int.Parse(Console.ReadLine());

if (passengers <= seats)
break;
}
Random r = new Random();

int seatsPlusOne = seats + 1;

int[] s = new int[seatsPlusOne];

Console.WriteLine();

Console.WriteLine("{0, 3} {1, 6}", "Passangers", "Seats");

for (int x = 1; x <= passengers; x++)
{
while (true)
{
int randomSeat = r.Next(1, seatsPlusOne);

if (s[randomSeat] == 0)
{
s[randomSeat] = x;
Console.WriteLine("{0, 4} {1, 10}", x, randomSeat);
break;
}
}
}

Console.WriteLine();

if (passengers == seats)
{
Console.WriteLine("No empty seats");
//goto end;
return;
}
else
Console.WriteLine("No of empty seats: {0}", seats - passengers);

Console.WriteLine("\nEmpty seats No: ");

for (int randomSeat = 1; randomSeat < s.Length; randomSeat++)
{
if (s[randomSeat] == 0)
Console.WriteLine("{0}", randomSeat);
}
//end:
Console.Read();
}
}
}


Attachment: vulpes program.zip

Answers (1)