-1

These are the question an my codes are attached to this form!

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.

availableNonSmoking – returns true if there are any available seats in the non smoking section of the plane. Returns false otherwise.
availableSmoking - returns true if there are any available seats in the smoking section of the plane. Returns false otherwise.

bool availableSmoking (){ 
int count = 0;
for(int i = 0; i < 5; i++)
if(plane[i] != 0)
    count++;
return count < 5;
}

Example of how to use this function in your program: if(r.availableSmoking())
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 < 100; ++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.

Edited by pyTony: fixed formatting

Attachments
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <algorithm>


using std::cout;
using std::cin;
using std::endl; 

class Bucket
{
public:
	void bucketSort(unsigned int[], unsigned int[]);
};

		
int main()
{
  int vals[100];
  int n=100;
  
  
   srand(time(0));
  for (int i=0; i<n; i++) vals[i]=(int)((double) n*rand())/RAND_MAX;


  void Bucket::bucketSort(unsigned int b[], unsigned int c[]);
  
  
  return 0;
}

int m;
int s;
int e;
int k;
int p;

void Bucket::bucketSort(unsigned int b[], unsigned int c[])
{
	  int bucket [m];

	  for (unsigned int s=0; s<m; ++s)
		  bucket[s]=0;

	  for (unsigned int e=0; e<c; ++e)
		  ++bucket [b[e]];

	  for(unsigned int e=0; s=0; s<m; ++s)
		  for(unsigned int k = bucket[s]; p>0; --k)
			  b[e++]=s;
  }
#include <iostream.h>
	

void bucketSort(int[], const int);

int main()
{
	 
	const int  entry=7;
	int digit;

	cout<<"Enter 7 numbers below \n";

	int number[entry]={0};

	for(int num=0; num<entry; num++)
	{
		cout<<"Enter numbers: ";
		cin>>digit;
		number[num]=digit;
	}

	bucketSort(number, entry);

	return 0;
}

void bucketSort(int number[], const int entries)
{
	int largest=0;
	for(int s=0;s<entries;++s)
	{
		
		if(number[s]>largest)
			largest=number[s];
	
	}
	for(s=10;s<largest*10;s*=10)
	{
	
		int i=0;
		int s2=0,s3=0;
		int B[10][7]={{0},{0}};
		for(int d=0;d<entries;++d)
		{
		
			s2=s/10;
			s3=(number[d]%s)/s2;
			B[s3][d]=number[d];
			
		
		}
		for(int g=0;g<10;++g)
		{
	
			for(d=0;d<entries;++d)
			{
		
				
				if(B[g][d]!=0)
				{
					
					number[i]=B[g][d];
					cout<<number[i]<<"  ";
					++i;
				}	
			}	
		}cout<<"\n";

	
	}
}
#include "Reservations.h"

Reservations::Reservations()
{		
	plane[0]=0;
	seat=10;
}

Reservations::getSeats(int seat)
{
	return seat;
}

int Reservations::reserveSmoking(int seatnum)
{

	cout<<"Please select from one(1) to five(5): ";
	cin>>seatnum;

	while(seatnum < 0 || seatnum > 5)
	{
		cout<<"Please select from one(1) to five(5): ";
		cin>>seatnum;
	}


	reduceAvailableSeats ();

	{
		if(plane[seatnum] == 0)

			plane[seatnum]=1;	
			return seatnum;
		else

			return -1;

	}
	
}

int Reservations::reserveRandomSmoking(int availseat)
{
	srand(time(0));
    availseat = 1 + rand() % 5;
	
	if(plane[availseat] == 0)
		return availseat;
	else
		return 0;
}

int Reservations::reserveNonSmoking(int nonseat)
{
 

	cout<<"Please select from six(6) to ten(10): ";
	cin>>nonseat;

	while(nonseat < 6 || nonseat > 10)
	{
		cout<<"Please select from six(6) to ten(10): ";
		cin>>nonseat;
	}

	reduceAvailableSeats ();

	if(plane[nonseat] == 0)
	{
		plane[nonseat]=1;
		return nonseat;
	}
	
	else
		return -1;
}

int Reservations::reserveRandomNonSmoking(int availseat)
{
	srand(time(0));
	availseat = 6 + rand() % 5;
	
	if(plane[availseat] == 0)
		return availseat;
	else
		return 0;
}

void Reservations::reduceAvailableSeats ()
{
	seat--;
	
	
}
#include <iostream>
#include <time.h> 
using std::cout;
using std::cin;
using std::endl;



class Reservations
{
public:
	Reservations();		
	int getSeats(int);
	int reserveSmoking(int);
	int reserveRandomSmoking(int);
	int reserveNonSmoking(int);
	int reserveRandomNonSmoking(int);
private:
	void reduceAvailableSeats();
			int seat;		
	int plane [10];
};
#include "Reservations.h"

int main()
{
	Reservations reserve;

	char response = 'y';
	char ans;
	int choice;
	int seatNum;
	while (response != 'n' && response != 'N')
	while (toupper(response)!= 'N')
	{
		cout<<"Welcome to Tsai Airlines";
		cout<<"Would you like to reserve a seat in the smoking or non-smoking section of the airplane (s/n)?";
		cin>>ans;

		while(toupper(ans) != 'S' && toupper(ans) != 'N')
			{
			cout<<"Would you like to reserve a seat in the smoking or non-smoking section of the airplane (s/n)?";
			cin>>ans;

			}

		if(ans == 's')
		{
			cout<<"You have chosen the smoking section...Thank you!!!";
			cout<<"To select your own seat number, press 1";
			cout<<"To have the system select a seat for you, press 2";
			cin>>choice;

			while(choice != 1 && choice != 2)
			{

				cout<<"To select your own seat number, press 1";
				cout<<"To have the system select a seat for you, press 2";
				cin>>choice;
			}

			if(choice == 1)
			{
				cout<<"Please select your seat location (1-5): ";
				cin>>seatNum;
				
				while (reserve.reserveSmoking(seatNum) == -1)
				{

					cout<<"Seat location<<seatNum<< is currently occupied!";
					cout<<"To select your own seat number, press 1";
					cout<<"To have the system select a seat for you, press 2";
					cin>>choice;
					while(choice != 1 && choice != 2)
					{
						cout<<"To select your own seat number, press 1";
						cout<<"To have the system select a seat for you, press 2";
						cin>>choice;
					}
				}

			}

			else
			{
				cout<<"you have chosen in non smoking section";
			}

		}

	}

	return 0;
}
// Sean Gillett

#include <iostream>
#include <string>  
using std::string;
using std::cin;
using std::cout;
using std::endl;


#include "TurtleGraphics.h"	
                                  
int distance;

	TurtleGraphics::TurtleGraphics()
	{
		direction = 0;
		xPos = 0;
		yPos = 0;
		for ( int j = 0; j < 20; j++ )               
		{                                           
        // loop for columns of current row   
        for ( int k = 0; k < 20; k++ ) 
			floor [j][k] = 0;
		}
			
		penDown = false;
	}

	void TurtleGraphics::penUp()
	{
		penDown = false;

	}

	void TurtleGraphics::penDowns()
	{
		penDown = true;

	}
	
	void TurtleGraphics::turnRight()
	{
		direction++;

	}
	
	void TurtleGraphics::turnLeft()
	{

		direction--;
	}
	
	void TurtleGraphics::movePen(int distance)
	{
		int i = distance;

		if (penDown == true)
			floor [xPos] [yPos] = 1;

		for(i; i >= 0; i--)
		{
			if (direction == 0)
			{
				xPos++;
				if (penDown == true)
					floor [xPos] [yPos] = 1;
			}


			if (direction == 1)
			{
				yPos++;
				if (penDown == true)
					floor [xPos] [yPos] = 1;
			}

			if (direction == 2)
			{
				xPos--;
				if (penDown == true)
					floor [xPos] [yPos] = 1;
			}

			if (direction == 3)
			{
				yPos--;
				if (penDown == true)
					floor [xPos] [yPos] = 1;
			}
		}


	}
	
	void TurtleGraphics::printGraphic()
	{
	   // loop for array's rows                
     for ( int j = 0; j < 20; j++ )               
     {                                           
        // loop for columns of current row   
        for ( int k = 0; k < 20; k++ ) 
			
			if (floor[j][k] == 1)
				cout << " * " << ' ';
			else
				cout << floor[ j ][ k ] << ' ';           
                                                 
        cout << endl; 
     }

	}
// Sean Gillett

#include <iostream>
#include <string>  
using std::string;

                                  
class TurtleGraphics                                                     
{                                                                   
public: 
	TurtleGraphics();
	void penUp();
	void penDowns();
	void turnRight();
	void turnLeft();
	void movePen(int);
	void printGraphic();

private:  
	int xPos;
	int yPos;
	int floor [20] [20];
	int direction;
	bool penDown;

};
// Sean Gillett

#include <iostream>
#include <string>  
using std::string;
using std::cin;
using std::cout;
using std::endl;


#include "TurtleGraphics.h"

int commands[99][ 2 ];

int main ()
{
	void getCommands( int commands[99][ 2 ] );



	TurtleGraphics t;

	int commands [99] [2];
	getCommands(commands);
	int i =0;
	int value;

	while (i < 99)
	{
		value = commands [i][0];
		switch (value)
		{
		case 1: t.penUp();
		case 2: t.penDowns();
		case 3: t.turnRight();
		case 4: t.turnLeft();
		case 5: t.movePen (commands[i][1]);
		}
	}

		t.printGraphic();

	return 0;
}
                  

void getCommands( int commands[99][ 2 ] )
{
   int tempCommand;
   int MAXCOMMANDS = 99;

   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
}
2
Contributors
1
Reply
10
Views
9 Years
Discussion Span
Last Post by Salem
0

So what?

You need to ask much better questions.
http://www.catb.org/~esr/faqs/smart-questions.html

You've basically dumped your 3 assignments and 3 attempted solutions all into the same thread. Is it even your code? It looks a lot like boiler-plate given out by your tutor.

For starters, these should have been 3 separate threads.

Then you need to explain a bit about what the actual problem is you're facing. It's not enough to just dump your attempt and hope someone will spend their valuable time guessing what all the missing parts are, and give you a competely free lunch.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.