I tried them but i cant seem to get them to compile so it would be really nice if you can try them and let me know where i went wrong! here are the question:
(Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system. You have been asked to program the new system. You are to write a program to assign seats on each flight of the airline's only plane (capacity: 10 seats). Follow the instructions below:
Create a class called Reservation. This class will have a data member that is an array of type int, of size 10. This member should be called plane. You should also have a data member called seats which is an int. Your class should contain the following public member functions:
Constructor - should initialize your array to 0, seats to 10.
getSeats – returns the current number of available seats.
reserveSmoking – takes an int argument and attempts to reserve a seat in the smoking section that corresponds to the value of the argument supplied. Please note that only the first 5 seats in the plane can be used for smoking customers. The other 5 seats cannot be used. The function returns the value of the seat only if a reservation was possible at that location requested. Otherwise, the function returns -1.
reserveRandomSmoking – reserves a seat anywhere in the smoking section (first 5 seats) as long as there is an available seat. Returns 0 if there are no available seats anywhere in the smoking section. Otherwise, it returns the location of reserved seat.
reserveNonSmoking – takes an int argument and attempts to reserve a seat in the non-smoking section that corresponds to the value of the argument supplied. Please note that only the last 5 seats in the plane can be used for non-smoking customers. The other 5 seats cannot be used. The function returns the value of the seat only if a reservation was possible at that location requested. Otherwise, the function returns -1.
reserveRandomNonSmoking – reserves a seat anywhere in the non-smoking section (last 5 seats) as long as there is an available seat. Returns 0 if there are no available seats anywhere in the non-smoking section. Otherwise, it returns the location of reserved seat.
private:
reduceAvailableSeats – this function should be called every time a reservation is successfully made in either the smoking or non-smoking section of the plane. It should reduce the value of seats by 1 but also ensure that its value never goes below 0.
Now that you have built your Reservation class, create a Reservation object inside of function main. Then, using this newly built object, do what is asked of you below:
Your program should display the following menu of alternatives Please type 1 for "Smoking" and Please type 2 for "Non-Smoking". If the person types 1, your program should assign a seat in the smoking section. If the person types 2, your program should assign a seat in the non-smoking section. Your program should print a boarding pass indicating the person's seat number and whether it is in the smoking or non-smoking section of the plane.
Your program should, of course, never assign a seat that has already been assigned. When either section is full, your program should ask the person if it is acceptable to be placed in the other section (if there is available seating). If yes, then make the appropriate seat assignment. If no, then print the message "Next flight leaves in 3 hours".
2.) This problem is adapted from the one provided in Deitel & Deitel C++ How to Program, 5th ed., question number 7.30. (Bucket Sort) A bucket sort begins with a one-dimensional array of positive integers to be sorted and a two-dimensional array of integers with rows subscripted from 0 to 9 and columns subscripted from 0 to n1, where n is the number of values in the array to be sorted. Each row of the two-dimensional array is referred to as a bucket. Write a function bucketSort that takes an integer array and the array size as arguments and performs as follows:
a. Place each value of the one-dimensional array into a row of the bucket array based on the value's ones digit. For example, 97 is placed in row 7, 3 is placed in row 3 and 100 is placed in row 0. This is called a "distribution pass."
b. Loop through the bucket array row by row, and copy the values back to the original array. This is called a "gathering pass." The new order of the preceding values in the one-dimensional array is 100, 3 and 97.
c. Repeat this process for each subsequent digit position (tens, hundreds, thousands, etc.).
On the second pass, 100 is placed in row 0, 3 is placed in row 0 (because 3 has no tens digit) and 97 is placed in row 9. After the gathering pass, the order of the values in the one-dimensional array is 100, 3 and 97. On the third pass, 100 is placed in row 1, 3 is placed in row zero and 97 is placed in row zero (after the 3). After the last gathering pass, the original array is now in sorted order.
Note that the two-dimensional array of buckets is 10 times the size of the integer array being sorted. This sorting technique provides better performance than a insertion sort, but requires much more memory. The insertion sort requires space for only one additional element of data. This is an example of the space time trade-off: The bucket sort uses more memory than the insertion sort, but performs better. This version of the bucket sort requires copying all the data back to the original array on each pass. Another possibility is to create a second two-dimensional bucket array and repeatedly swap the data between the two bucket arrays.
3.) This problem is adapted from the one provided in Deitel & Deitel C++ How to Program, 5th ed., question number 7.23. (Turtle Graphics) The Logo language, which is popular among elementary school children, made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a C++ program. The turtle holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about freely without writing anything. In this problem, you will simulate the operation of the turtle and create a computerized sketchpad as well.
Use a 20-by-20 array floor that is initialized to zeros. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0, 0) of the floor with its pen up. The set of turtle commands your program must process are shown in Fig. 7.33.
Figure 7.33. Turtle graphics commands.
(This item is displayed on page 396 in the print version)
Command Meaning
1 Pen up
2 Pen down
3 Turn right
4 Turn left
5,10 Move forward 10 spaces (or a number other than 10)
6 Print the 20-by-20 array
9 End of data (sentinel)
Suppose that the turtle is somewhere near the center of the floor. The following "program" would draw and print a 12-by-12 square and end with the pen in the up position:
2
5,12
3
5,12
3
5,12
3
5,12
1
6
9
As the turtle moves with the pen down, set the appropriate elements of array floor to 1's. When the 6 command (print) is given, wherever there is a 1 in the array, display an asterisk or some other character you choose. Wherever there is a zero, display a blank. Follow the instructions below in order to build this program.
Create a class called TurtleGraphics. This class will have the following public member functions:
TurtleGraphics()
void penUp();
void penDown();
void turnRight();
void turnLeft();
void movePen(int distance);
void printGraphic();
bool getPenStatus();
Your private data members will be the following:
A two-dimensional 20x20 integer array called floor. Integer variables xPos and yPos that represent your current row column position respectively. An integer variable direction and a Boolean penDown.
The constructor of this class should initialize the data members as follows: xPos, yPos, direction and floor must all be set to 0. Data member penDown should be set to false.
The data member direction can point to 4 different locations, north, east, south and west. Assume that when it is at 0, it is pointing east. When it is at 1, it is pointing south, when it is 2, it is pointing west and when it is 3, it is pointing north. Since your xPos and yPos start off with the value 0, it means that you are currently located at row 0, column 0. If you chose to put the pen down, then every time you move, you should change the value of the cell you enter with the value 1 (remember, the floor is currently set to have all its cells 0). If you chose to keep your pen up, you can still move around the floor, but each cell that you enter will not be changed.
You need to implement all the functions declared above. For example, in function turnRight, you must increment the value of direction every time this function is called. But why should you increment direction? Well, remember, you are pretending as if you have a pen in your hand and you will be drawing on a screen. However, you must be able to say which direction to move the pen when you write (whether up, down, left or right). So, how do you know in which direction you are currently pointing? Data member direction tells you. Even though direction is an integer, each of its values tells you which direction it is pointing. So, let’s say that direction is currently 0, this means you are currently pointing eastward. Then, by calling turnRight, it should become 1. And if you call turnRight again, direction will become 2. And let’s say that while it is 2, you call turnLeft. This will reduce the value of direction by 1 and thus change its value back to 1, meaning, you are now pointing south (remember, 0 means east, 1 means south, 2 means west and 3 means north). The pen starts off in an upright position, so if you move around the floor, nothing is changed. When I say nothing is changed, that means each cell that you enter into while moving around the floor does not get its contents changed. To change whether the pen is upright or down, you must call function penUp or penDown respectively. These functions should simply change the value of data member penDown to true or false. False means the pen is up and true means it is down. Function movePen should take accept an int which represents the distance to move. You already know what your current direction is, you know whether the pen is up or down, and you have your floor. So, based on the distance parameter, you should then update your xPos and yPos. By update, I mean, you need to move from where you are, touching every cell until you arrive at your new location. If the pen was set to be down, then as you move, you should change each cell’s value that you enter to 1, otherwise, leave it unchanged.
Member function printGraphic simply prints the two dimensional array, floor, to the screen. You should know how to print an array by now. The only catch is that you need to print an asterisk in place of every 1 that it finds in each cell. Otherwise, it will simply print a blank space.
Now create your driver program. Your driver program will have two functions, main and the other called getCommands. I have kindly provided you with the code for getCommands. Ensure that you do not merely copy it, but truly understand how it works and how it is used in the context of this problem.
void getCommands( int commands[][ 2 ] )
{
int tempCommand;
cout << "Enter command (9 to end input): ";
cin >> tempCommand;
for ( int i = 0; tempCommand != 9 && i < MAXCOMMANDS; ++i ) {
commands[ i ][ 0 ] = tempCommand;
if ( tempCommand == 5 ) {
cin.ignore(); // skip comma
cin >> commands[ i ][ 1 ];
}
cout << "Enter command (9 to end input): ";
cin >> tempCommand;
}
commands[ i ][ 0 ] = 9; // last command
}
Now that you have getCommands, let us focus on function main. Inside of main, you are to create an array to store the commands that the user wants to execute. Actually, you could have also asked the user each command that they want to execute and then run it and then ask again. However, it is much better to simply ask for all the instructions from the start, load it into an array of commands, and then execute each command afterwards. To figure out the size, type, etc. of this array that should store the commands, look at how it is being used inside function getCommands. If you notice, the purpose of function getCommands is such that you can pass an array to it (your command array) and then it will proceed to get as many commands from the user as desired. When the user is finished entering his/her commands, the command array should be loaded with all instructions. At this point, execution should return back to main. So, all that is left for you to do now is use some sort of loop to traverse the array that contains all the commands and execute each one that you read. That is, every time you loop through the array, read a new command, check to see if it is the last command, if not, pass the command to a switch statement which in turn will call the appropriate function from the TurtleGraphics object you created earlier.