Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set up a queue of students, add and remove students...

Status
Not open for further replies.

karisma

Vendor
Nov 17, 2002
2
0
0
US
HI, it's me again, hope u lot are ok! Thanx for helping me on my previous question. Here is another problem.

This is the sceanario: I pay grants of £1000 to all students who apply, if I have no money in the bank, then students are put on a queue until money is available (when ther is money available they are paid first come first serve). For each student the name (max 20 char) and a number of 7 digits must be stored. The program has to permit adding a student to the queue and a facility to credit the bank account. When money is credited to the account students are removed from the queue until either all the money is used up or the queu is empty.

I have tried to write the program in Fortan 90, can anyone please check it for me and just tell me if I have done it wrong or right.


!this is where the variables of the module are declared
IMPLICIT NONE
TYPE students !this is of type student
INTEGER::answer
CHARACTER(LEN=20)::name !the name can be no longer than 20 characters

CHARACTER(LEN=20)::kNumber !the knumber can be no longer than 20 characters

TYPE(students),POINTER::nextStudent !this pointer points to the next student

TYPE(students),POINTER::previousStudent !this pointer points to the previous student

END TYPE !type ends here

TYPE(students),POINTER::queueBegin,queueEnd,temp,tempNew
!type student has a pointer to queue begin, queue end
!temp and temp new

CONTAINS
!-----------------------------------------------------------
SUBROUTINE setUpQueue !the start of the first subroutine it will ask for the name and knumber of the first student it will not return any information, just simply store it

!initialise the queue
ALLOCATE(queueBegin)

!Get details of first student
PRINT*,"What is the first student's name and k-number"
READ*, queueBegin%name,queueBegin%kNumber
!stores the first students name and knumber at the
!beggining of the queue

queueEnd=>queueBegin
!this sets the begining of the queue and the end of
!the queue to the first student

END SUBROUTINE setUpQueue !the end of the first subroutine
!-----------------------------------------------------------
SUBROUTINE addStudent !the start of the second subroutine
!this subroutine will not return any value
!it will just store the name and knumber of the student

ALLOCATE(temp)

PRINT*,"What is the student's name and k-number"
READ*, temp%name,temp%kNumber
!will store it in name and knumber which temp represents
!of the second student

!make the new student point to the end of the queue
temp%nextStudent=>queueEnd

!point the end of the queue at the new student
queueEnd=>temp

nullify(temp)
!this will stop temp pointing anywhere

!tempNew=>queueEnd


END SUBROUTINE addStudent !end of the second subroutine
!------------------------------------------------------------------------------------------------
SUBROUTINE startQueue !start of the third subroutine
!this is the main part of the program
!it will return no information and doesn't input information
!it simply sets the pointers to different pices of data
!and where abouts in the queue it should be

temp=>queueEnd
!temp is pointing to the end of the queue

DO WHILE(ASSOCIATED(temp%nextStudent%nextStudent))
temp=temp%nextStudent
END DO !temp 2nd item in queue

!DO WHILE(ASSOCIATED(tempNew%nextStudent%nextStudent))
!tempNew=tempNew%nextStudent
!END DO !tempNew 2nd item in queue

NULLIFY(temp%nextStudent)
!stop second item pointing to the old first item

DEALLOCATE(queueBegin)
!DEALLOCATE(queueEnd)

queueBegin=>temp
!the begining of the queue is pointing to temp

!queueEnd=>tempNew

temp=>queueEnd
!temp is pointing to the end of the queue

queueEnd=>temp
!the end of the queue is pointing to temp

END SUBROUTINE startQueue !end of the third subroutine
!------------------------------------------------------------------------------------------------
SUBROUTINE oldHead !start of the fourth subroutine
!this will print to the screen who the old head of the queue was

PRINT*, "Head of the queue was ",temp%nextStudent%name

END SUBROUTINE oldHead !end of the fourth subroutine
!------------------------------------------------------------------------------------------------
SUBROUTINE newHead !start of the fifth subroutine
!this will print to the screen who the new head of the queue is

queueBegin=>temp
PRINT*, "New head is ",queueBegin%name

!QueueEnd=>tempnew

DEALLOCATE(temp)
!this frees temp up

!temp=>queueEnd

!queueEnd=>temp

END SUBROUTINE newHead !end of fifth subroutine
!------------------------------------------------------------------------------------------------
SUBROUTINE displayQueue !start of sixth subroutine
!this will print to the screen who the first and last student in the queue are


PRINT*, "THE FIRST STUDENT IS ",queueBegin%Name
PRINT*, "THE LAST STUDENT IS ",queueEnd%Name


END SUBROUTINE displayQueue !end of sixth subroutine
!------------------------------------------------------------------------------------------------

END MODULE list !this is the end of the module





PROGRAM queue !start of the main program
USE list !this will use the variables declared from the module named list
CALL setUpQueue
!this calls the first subroutine

DO !DO loop initialised

PRINT*, "Would you like to add another student? 1=YES, 2=NO"
READ*, answer



IF (answer==1) THEN
!if the user choses 1 then they can add a new student

CALL addStudent
!this calls the second subroutine

ELSE
IF (answer==2) THEN
!if the user choses 2 then the queue will start
!and build up

CALL startQueue
!this calls the third subroutine

CALL oldHead
!this calls the fourth subroutine

CALL newHead
!this calls the fifth subroutine

CALL displayQueue
!this calls the suxth subroutine

ELSE
!if none of the above are chosen then a error message will be printed

PRINT*, "ERROR!!"

END IF !end of the IF condtions

END IF !end of the IF condtions

END DO !end of the DO loop


END PROGRAM queue !end of the program


I would appreciate any help, thankyou very much and HAPPY NEW YEAR.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top