0% found this document useful (0 votes)
61 views

Data Structures and Algorithm Related Question - Th...

The document discusses a question about writing a C++ program to handle truck movements in and out of a garage where only the last truck entered can exit. The program should support commands to put trucks on the road, enter the garage, exit the garage, and display trucks in the garage or on the road, with an error message if trying to exit a non-last truck.

Uploaded by

Unlimited Echo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Data Structures and Algorithm Related Question - Th...

The document discusses a question about writing a C++ program to handle truck movements in and out of a garage where only the last truck entered can exit. The program should support commands to put trucks on the road, enter the garage, exit the garage, and display trucks in the garage or on the road, with an error message if trying to exit a non-last truck.

Uploaded by

Unlimited Echo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

6/20/22, 10:34 PM Data Structures And Algorithm Related Question: Th... | Chegg.

com

  Home Study tools


 My courses
 My books My folder Career Life Sign in More 

Find solutions for your homework Search

home / study / engineering / computer science / computer science questions and answers / data structures and algorithm related question: there is a …

Question: Data Structures and Algorithm Related


Question:
There is a gara…

Data Structures and Algorithm Related


Question:
There is a garage where the access road can accommodate any
number of trucks

at one

time. The garage is built in such a way that only the last truck
entered can be moved

out. Each of the trucks is identified by a positive integer (a


truck_id). Write a C++ program

to handle truck moves, allowing for the following commands:

a) On_road (truck_id);

b) Enter_garage (truck_ id);

c) Exit_garage (truck_id);

d) Show_trucks (garage or road);

If an attempt is made to get out a truck which is not the closest


to the garage entry, the

error message Truck x not near the garage door.

Expert Answer

Anonymous answered this


Was this answer helpful? 0 0
11,195 answers

(Answer):-

#include <iostream>

using namespace std;

class Truck

private:

int id;

public :

Truck(int id)

this->id=id;

Truck()

this->id=0;

int getid()

return id;

};

Truck garage[100]; // an array is used as a stack to implement the functionality of garage

int n=100, top=-1;

int onRoad(Truck t_id) // will tell if the truck t_id is at the gate of the garage or not

if(garage[top].getid()==t_id.getid())

return 1;

else

return 0;

void enter_Garage(Truck t) {

if(top>=n-1)

cout<<"\nNo more space in the Garage"<<endl;

else {

top++;

garage[top]=t;

void exit_Garage(Truck t) {

if(top<=-1)

cout<<"\nNo Trucks in the Garage"<<endl;

else {

if(onRoad(t)==1)

{ cout<<"\nThe Truck with id " <<t.getid()<<" moved out "<<endl;

top--;

else

cout<<"\nTruck "<<t.getid()<<" not near Garage door"<<endl;

void show_Trucks() {

if(top>=0) {

cout<<"\nTrucks in the Garage are :";

for(int i=top; i>=0; i--)

cout<<"\nTruck "<<garage[i].getid()<<" ";

cout<<endl;

} else

cout<<"\nGarage is empty";

int main() {

Truck t1(2);

Truck t2(4);

Truck t3(6);

Truck t4;

enter_Garage(t1);

enter_Garage(t2);

enter_Garage(t3);

enter_Garage(t4);

show_Trucks();

exit_Garage(t1);

exit_Garage(t4);

show_Trucks();

This can be implemented by using Stack data structure in


C++.

An image of the output has been given below for better


understanding of the student.

Comment


Questions viewed by other students

Q: 6. Calculate the Grashof condition of the fourbar mechanisms defined below. Draw clear diagrams of the linkages and
describe the motions of each inversion. a. 2 4.5 7 9 b. 2 4.0 6 8 C. 2 3.5 7 9

A: See answer 100% (2 ratings)

Q: Write the C++ Program/code for:


a) Suppose you are working in an organization. You are given a
bundle of files
containing 2500 files with serial numbers starting from 1 to 2500.
You have to search
for a file with the serial number
886. You have to search them by
dividing the piles
into two parts. Good news is that the files are already sorted in
ascending order. Write
C++ code for...

A: See answer

COMPANY LEGAL & POLICIES CHEGG PRODUCTS AND SERVICES CHEGG NETWORK CUSTOMER SERVICE

About Chegg Advertising Choices Cheap Textbooks Chegg Math Solver EasyBib Customer Service
Chegg For Good Cookie Notice Chegg Coupon Mobile Apps Internships.com Give Us Feedback
College Marketing General Policies Chegg Play Sell Textbooks Thinkful Manage Subscription
Corporate Development Intellectual Property Rights Chegg Study Help Solutions Manual
Investor Relations Terms of Use College Textbooks Study 101
Jobs Global Privacy Policy eTextbooks Textbook Rental
Join Our Affiliate Program DO NOT SELL MY INFO Flashcards Used Textbooks
Media Center Honor Code Learn Digital Access Codes
Site Map Honor Shield Uversity Chegg Life
Chegg Writing

© 2003-2022 Chegg Inc. All rights reserved.

https://www.chegg.com/homework-help/questions-and-answers/data-structures-algorithm-related-question-garage-access-road-accommodate-number-trucks-on-q92205241?trackid=f940a43955b6&strackid=f9b92c8fa929 1/1

You might also like