the question is to be solved using loops,decisions and arrays!!


QUESTION:

Statement of question:

“AIRLINE RESERVATION"

Write a program to assign seats on a 10-seater plane. Your program should first ask a user to enter 1 for First Class and 2 for Economy class. First 5 seats are economy and last 5 seats are first class. Whenever a seat is reserved the program should display output that which seat ( from 1 to 10) was occupied and in which class.

USE A SINGLE SCPRITED ARRAY TO REPRESENT THE SEATING CHART. INITITALISE ALL ELEMETS TO 0 TO INDICATE THE SEATS ARE EMPTY AND WHEN RESERVED ASSIGN SEAT NUMBER TO 1.
Your program should never assign same seat twice!!!!
If first or economy class is full then your program should suggest the user the other class.
If both classes are full then your program should inform user that flight is full and suggest him to try on next flight.


now pls point out errors in my program one by one

#include <iostream.h>
#include <conio.h>
int main()
{
int arr[10]={0,0,0,0,0,0,0,0,0,0};
int a,i;

do{
cout<<"\nPlease Enter 1 for 'First Class' or 2 for 'Economy Class':";
cin>>a;

if ( a==1)
for ( i=0 ; i<=4 ; i++ )
	{
   if ( arr[i]== 0)
   	{arr[i]=1;
   	break;}
   }


else if ( a==2)

for ( i=5 ; i<=9 ; i++)
	{
   if ( arr[i]==0)
   {arr[i]=1;
   break;}
   }

else
cout<<"Input error!"<<endl;

for ( i=1 ; i<=10 ; i++)
cout<<arr[i]<<" ";
} while 
(arr[0]+arr[1]+arr[2]+arr[3]+arr[4]+arr[5]+arr[6]+arr[7]+arr[8]+arr[9]!=10);


getch();
return 0;}

Dani AI

Generated

Two things were causing trouble here: an out‑of‑bounds array access (which produced the stray “2” via undefined behavior) and a mismatch between the problem statement and the prompt/mapping you implemented. Good catch, — printing with for (i=1; i<=10; i++) goes past the valid indices; use for (i=0; i<10; ++i) instead.

Clarify the mapping first. The statement says “first 5 seats are economy and last 5 seats are first class,” but your prompt asks the user to enter 1 for First Class and 2 for Economy. That means 1 should allocate seats 6–10 (indices 5–9) and 2 should allocate seats 1–5 (indices 0–4). Pick one mapping and make the prompt match it — inconsistent prompts are why users and code get confused. Also avoid repeatedly summing ten elements; keep a small counter or a helper that checks a range for a free seat. Finally, the “class full” messages should suggest the other class (not suggest the same class again).

A simple, clear implementation (different from the examples already posted) — finds the first free seat in the chosen class, offers the alternate class if needed, and avoids out‑of‑bounds access:

#include <iostream>
#include <array>

int find_free(const std::array<int,10>& s, int lo, int hi){
    for (int i = lo; i <= hi; ++i) if (s[i] == 0) return i;
    return -1;
}

int main(){
    std::array<int,10> seats{}; // 0 = free, 1 = occupied
    const int ECON_LO = 0, ECON_HI = 4, FIRST_LO = 5, FIRST_HI = 9;
    while (true) {
        int filled = 0; for (int v : seats) filled += v;
        if (filled == 10) { std::cout << "All seats reserved. Next flight in 3 hours.\n"; break; }

        std::cout << "Enter 1 for First Class (seats 6-10) or 2 for Economy (seats 1-5): ";
        int choice; if (!(std::cin >> choice)) break;

        int idx = (choice == 1) ? find_free(seats, FIRST_LO, FIRST_HI)
                                : (choice == 2) ? find_free(seats, ECON_LO, ECON_HI)
                                                : -2;
        if (idx >= 0) { seats[idx] = 1; std::cout << "Assigned seat " << (idx+1) << "\n"; continue; }
        if (idx == -2) { std::cout << "Invalid choice.\n"; continue; }

        int alt = (choice == 1) ? find_free(seats, ECON_LO, ECON_HI)
                                : find_free(seats, FIRST_LO, FIRST_HI);
        if (alt >= 0) {
            std::cout << "Requested class full. Assign in the other class? (y/n): ";
            char c; std::cin >> c;
            if (c == 'y' || c == 'Y') { seats[alt] = 1; std::cout << "Assigned seat " << (alt+1) << "\n"; continue; }
        }
        std::cout << "No seats available in requested or alternate class. Next flight leaves in 3 hours.\n";
        break;
    }
}

Testing tips: exercise edge cases (fill one class, then the other), check invalid input, and avoid conio.h/getch() for portable code. Replace repeated sums with counters or helpers, and always print seats using i = 0 .. 9 while showing user‑facing seat numbers as i+1.

Recommended Answers

All 4 Replies

for ( i=1 ; i<=10 ; i++ )
         cout<<arr[i]<<" ";

You went beyond array bounds. You should do this:

for ( i=0 ; i<10 ; i++ )
         cout<<arr[i]<<" ";

Thanx For Solvingproblem No.1 Dave!!!!

OK PEOPLE QUESTION SOLVED!!!!!!............thread closed!!!!!!1

heres the final answer: :D

#include <iostream.h>
#include <conio.h>
int main()
{
int arr[]={0,0,0,0,0,0,0,0,0,0};      //array initialised to zeros
int a,i;                             // loop variabe,i and class input,a


do{ cout<<"\nPlease Enter 1 for 'First Class' or 2 for 'Economy Class':";cin>>a;

if ( a==1)
for ( i=0 ; i<=4 ; i++ )
	{
   if ( arr[i]== 0)
   	{arr[i]=1;
   	break;}
   else if( arr[0]+arr[1]+arr[2]+arr[3]+arr[4]==5) 
           {cout<<"Class full, please choose '2' for Economy\n";break;}
   }


else if ( a==2)

for ( i=5 ; i<=9 ; i++)
	{
   if ( arr[i]==0)
   {arr[i]=1;
   break;}
   else if (arr[5]+arr[6]+arr[7]+arr[8]+arr[9]==5) 
        {cout<<"Class full, please choose '2' for Economy\n";break;}
   }

else
cout<<"Input error!"<<endl;

for ( i=0 ; i<10 ; i++)
cout<<arr[i]<<" ";
} while
(arr[0]+arr[1]+arr[2]+arr[3]+arr[4]+arr[5]+arr[6]+arr[7]+arr[8]+arr[9]!= 10);


cout<<"\nAll seats reserved. Next flight leaves in 3 hours";

getch();
return 0;}

ALL SUGGESTIONS 4 IMPROVEMENT OF SYNTAX, LOGIC etc R WELCOME!!!
:o

............................................................................

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.