[prog] Re: C++

Jacinta Richardson jarich at perltraining.com.au
Wed Jan 21 12:39:46 EST 2004


> here's the newest version - -I think it's getting closer but still 
> doesn't work quite correctly. here's the code, at the bottom of the 
> page I included the sample question I'm working since I changed
> my strategy away from using switch

How about we step back from the code for a bit and think about the
solution to the problem...

What variables are we going to need?  I've come up with a few extras for
you, you may or may not want to consider them.

	* section - which section the person will travel in
	* seats - our list of seats on the airplane 
	* people - number of people on the plane

	* FC_start - the starting place for first class seats.  probably 0
	* in_FC - how many people are sitting in FC at the moment
	* EC_start - the starting place for economy seats.  probably 5
	* in_EC - how many people are sitting in EC at the moment

Okay, once we have these variables we need to consider how to execute the
program.  We probably want some sort of looping structure so that we can
do this for every passenger.  We then need to determine what section the
person should go into (based on their wishes as well as our limitations)
and then issue them with the boarding pass.

-------
While the plane is not full (people < 10)

	Ask whether the person wants first class or economy
	
	If they want first class

		Check that we actually have seats left in FC
		(in_FC must be less than EC_start)

		If we don't have seats left in FC  (since the plane is not
		full, we don't need to worry about checking if there are
		still seats in economy)
			Ask if we can change their section.

			If they don't want their section changed
				Tell them to come back for the next plane
				and process the next customer.
			Otherwise
				Change their section
			Endif
		Endif

	Otherwise if they want economy class

		Check that we actually have seats in economy class
		If economy is full
			Ask if we can change their section

			If they don't want their section changed
				Tell them to come back for the next plane
				Process the next customer
			Otherwise
				Change their section
			Endif
		Endif
	Otherwise 
		They entered invalid data
	Endif

	Now we know that we can put them on the plane so:
	Increment the number of people on the plane

	If they're travelling in first class
		Tell them their seat assignment is in_FC+FC_start
		Add 1 to number of people in FC (in_FC++)
		Add person to plane
	Otherwise
		Tell them their seat assignment is in_EC+EC_start
		Add 1 to number of people in EC
		Add person to plane
	EndIf
EndWhile

At this point the plane is full, so note that the next plane leaves in 3
hours.
-------

Now there are probably other ways to solve this problem, but I think
you'll find that you 
	a) have to use a loop (or write out the same code 10 times)
	b) should determine their section first considering limitations

You appear to be attempting b) but not even considering a)

You're also adding people to your plane twice.  That is, you check that
there are seats left in FC (for example), add the person to the plane and
then at the end check that if the person is in FC you add the person to
the plane again.  If you follow my peudocode above you will ONLY add
people to the plane once you've handled their section.  What is more when
handling their section you'll ONLY do something if that section is full.

I believe you'll find it very difficult to change the execution flow of my
suggestion to allow actual seat assignments in the section handling.  This
is because if they change their section you have no good way to handle
that without duplicating even more code.

I hope this helps.

I have a working example from this pseduocode, so once you've got your own
working, let me know if you want to see how I handled it.

All the very best,

	Jacinta


--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001         |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |



More information about the Programming mailing list