Need help with pseudocode assignment! about reading record and add them and print them
A file of student records contains name, gender (M or F), age (in year) and marital status (single or married) for each student. Design an algorithm that will read through the file and calculate the number of married men, single men, and married women. Print these numbers on a student summary report. If any single men are over 30 year of age. Print their names and ages on a separate eligible bachelors report.
Can anyone tell me if I am wrong in any line? thanks! hope you can help me!
Set marriedMen to 0
Set singleMen to 0
Set marriedWomen to 0
Set singleWomen to 0
READ name, sex, age, status
DOWHILE(NOT EOF)
IF (status = married) THEN //check if status is married, if yes then check next
IF (sex = ‘F’) THEN //check if sex is F, if yes then +1
marriedWomen = marriedWomen + 1
ELSE
IF (sex = ‘M’) THEN //under married, and sex is M then +1
marriedMen = marriedMen + 1
ENDIF
ENDIF
ENDIF
IF (status = single) THEN //check if status is single, if yes then check next
IF (sex = ‘F’) THEN //check if sex is F, if yes then +1 to singleWomen
singleWomen = singleWomen + 1
ELSE
IF (sex = ‘M’) THEN //un开发者_高级运维der single, and sex is M then +1
singleMen = singleMen + 1
IF (age > 30) THEN //under single, sex = M and age is over 30 then print the name, age
Print ‘Eligible bachelors Report’
Print ‘Name: ‘, name
Print ‘Age: ‘, age
ENDIF
ENDIF
ENDIF
ENDIF
READ next record
ENDDO
Print ‘Student Summary Report’
Print ‘Married Men: ‘, marriedMen
Print ‘Single Men: ‘, singleMen
Print ‘Married Women: ‘, marriedWomen
Print ‘Single Women: ‘, singleWomen
In the code below, I have done the following:
- Added a
boolean
to prevent you from printing yourEligible bachelors Report
header for every single line. - Indented your code so it is easier to read.
- Replaced your use of
‘
with'
because you obviously used a word processor such as MS-Word to write your code. (May I suggest NotePad++?)
I left your code mostly intact: I take it for granted that ElseIf
isn't a valid keyword in your pseudo code and that your professor might pass you a file with genders other than M
and F
.
Set marriedMen to 0
Set singleMen to 0
Set marriedWomen to 0
Set singleWomen to 0
Set hasPrintedHeader to False
READ name, sex, age, status
DOWHILE(NOT EOF)
IF (status = married) THEN //check if status is married, if yes then check next
IF (sex = 'F') THEN //check if sex is F, if yes then +1
marriedWomen = marriedWomen + 1
ELSE
IF (sex = 'M') THEN //under married, and sex is M then +1
marriedMen = marriedMen + 1
ENDIF
ENDIF
ENDIF
IF (status = single) THEN //check if status is single, if yes then check next
IF (sex = 'F') THEN //check if sex is F, if yes then +1 to singleWomen
singleWomen = singleWomen + 1
ELSE
IF (sex = 'M') THEN //under single, and sex is M then +1
singleMen = singleMen + 1
IF (age > 30) THEN //under single, sex = M and age is over 30 then print the name, age
IF (hasPrintedHeader = False) THEN
Print 'Eligible bachelors Report'
hasPrintedHeader = True
END IF
Print 'Name: ', name
Print 'Age: ', age
ENDIF
ENDIF
ENDIF
ENDIF
READ next record
ENDDO
Print 'Student Summary Report'
Print 'Married Men: ', marriedMen
Print 'Single Men: ', singleMen
Print 'Married Women: ', marriedWomen
Print 'Single Women: ', singleWomen
DrawTree(n, direction, length)
if n > 0 do
DrawTrunk(direction, length)
DrawTree(n-1, 3DRandomAngle(direction), length*Factor(n))
DrawTree(n-1, direction + random % 10, length*Factor(n))
DrawTree(n-1, 3DRandomAngle(direction), length*Factor(n))
else
DrawLeaf()
end if
end DrawTree
精彩评论