0% found this document useful (0 votes)
15 views11 pages

G1 Assignment W12

Uploaded by

3315657503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

G1 Assignment W12

Uploaded by

3315657503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

请认真阅读并理解伪代码程序的解释并且使用 Python 语言重写伪代码的程序最后将所有程序存放在一个

homework.py 文件中上传到 FireFly 上。


Question 1 (Totalling)
DECLARE StudentMark: ARRAY[1:5] OF INTEGER
// » the name of the array is StudentMark // » the first index value is 1 // » the last index value is 5 // » and the data
type is Integer
DECLARE Total :INTEGER
DECLARE Counter:INTEGER
DECLARE ClassSize:INTEGER
// Declare the variable of Total, Counter and ClassSize to the data type as integer
ClassSize<-5 // assignment the value 5 to the variable ClassSize
Counter<-0
StudentMark[1]<-20
StudentMark[2]<-30
StudentMark[3]<-50
StudentMark[4]<-60
StudentMark[5]<-80
// assignment the value of student mark to each element by the index.
Total <- 0
// assignment the value 0 to the variable Total // use the for loop structure with the format as for ... next the loop times
depenes of the
// range area designed for the variable Counter
FOR Counter <- 1 TO ClassSize
Total <- Total + StudentMark[Counter]
// the statements inside the for loop will add all the elements in the StudentsMark array // by the index changed by Counter.
NEXT Counter
OUTPUT Total
Question 2 Counting

DECLARE StudentMark: ARRAY[1:5] OF INTEGER
• // // » the name of the array is StudentMark// » the first index value is 1// » the last index value is 5 // // »
and the data type is Integer
• DECLARE Total :INTEGER
• DECLARE Counter:INTEGER
• DECLARE ClassSize:INTEGER
• DECLARE passnumber:INTEGER
• ClassSize<-5
• Counter<-0
• StudentMark[1]<-20
• StudentMark[2]<-30
• StudentMark[3]<-50
• StudentMark[4]<-60
• StudentMark[5]<-80
• Total <- 0
• // // assignment the value 0 to the variable Total// // use the for loop structure with the format as for ...
next the loop times depenes of the range area designed for the variable Counter
• FOR Counter <- 1 TO ClassSize
• // the statements in side is the for loop is if statements which check the // elements in the array by the index
of Counter if the value is bigger or equals to
• // 60 then the passnumber will add 1 by it self.
• IF StudentMark[Counter]>=60
• THEN
• passnumber<-passnumber+1
• ENDIF // if statements end with endif
• NEXT Counter // for statements need the next to continue
Question 3 Maximum, minimum and average
• DECLARE StudentMark: ARRAY[1:5] OF INTEGER
• DECLARE MaximumMark:INTEGER
• DECLARE MinimumMark:INTEGER
• DECLARE Counter:INTEGER
• DECLARE ClassSize:INTEGER
• StudentMark[1]<-20 StudentMark[2]<-30 StudentMark[3]<-50 StudentMark[4]<-60StudentMark[5]<-80
• ClassSize<-5 MaximumMark <- 0 MinimumMark <- 100
• FOR Counter <- 1 TO ClassSize
• // Counter will become 1 to 2,3,4,5 to tranverse all the elements index in the array // The first if
statement will check the element value by the index of the Counter
• // if the value bigger than the MaximumMark it will assignment the value to the MaximumMark
• IF StudentMark[Counter] > MaximumMark
• THEN
• MaximumMark <- StudentMark[Counter]
• ENDIF
• // The second if statement will check the element value by the index of the Counter
• // if the value smaller than the MinimumMark it will assignment the value to the MinimumMark
• IF StudentMark[Counter] < MinimumMark
• THEN
• MinimumMark <- StudentMark[Counter]
• ENDIF
• NEXT Counter
• OUTPUT "MaximumMark is", MaximumMark
• OUTPUT "MinimumMark is", MinimumMark
Question 4 Maximum, minimum and average(Method 2)
• DECLARE StudentMark: ARRAY[1:5] OF INTEGER DECLARE MaximunMark:INTEGER. DECLARE MinimumMark:INTEGER
DECLARE Counter:INTEGER DECLARE ClassSize:INTEGER
• ClassSize<-5 StudentMark[1]<-20 StudentMark[2]<-30 StudentMark[3]<-50 StudentMark[4]<-60
StudentMark[5]<-80
• MaximunMark<-0 MinimumMark<-0 MaximunMark <- StudentMark[1] MinimumMark <- StudentMark[1]
• //The second strategy to find the Max number in the array is to assignment the first element
• //to the variable MaximunMark
• FOR Counter <- 2 TO ClassSize
• //the for loop will tranverse the elements from 2 to 5 in order to compare all the follow elements
• IF StudentMark[Counter] > MaximunMark
• // if the current StudentMark [Counter] is bigger than the variable MaximunMark then
• THEN
• // assignment the value to the MaximunMark
• MaximunMark <- StudentMark[Counter]
• ENDIF
• // the second if statements is used to find the MinimunMark and the principle is the same as the
• // firs if statements
• IF StudentMark[Counter] < MinimumMark
• THEN
• MinimumMark <- StudentMark[Counter]
• ENDIF
• NEXT Counter
• OUTPUT "MaximumMark is", MaximunMark
Q5 Linear Search
• DECLARE Name: STRING DECLARE Found:BOOLEAN. DECLARE Counter:INTEGER. DECLARE ClassSize:INTEGER.
DECLARE StudentName: ARRAY[1:5] OF STRING. DECLARE foundindex:INTEGER
• ClassSize<-5
• StudentName[1]<-“Alice” StudentName[2]<-“Linda” StudentName[3]<-“Peter” StudentName[4]<-“Aeo”
StudentName[5]<-"Tommy"
• //assignment the students' name to each elements in the array StudntName
• OUTPUT "Please input the name for search"
• INPUT Name
• Found <- FALSE Counter <- 1
• REPEAT
• IF Name = StudentName[Counter] //The if statement will check the Name of user input is weather
same as the name of //StudenName[Counter]
• THEN
• Found <- TRUE // if the they are the same the variable of Found will be changed to TRUE
• foundindex<-Counter // update the foundindex
• ELSE // the else will lead the Counter plus 1 for itself
• Counter <- Counter + 1
• ENDIF
• UNTIL Found OR (Counter > ClassSize)//there are two conditions to end this loop one is the Found =
True// the second is searched all the class member by the variable Counter

IF Found=TRUE//if Found is true print the found name and found index
• THEN
• OUTPUT Name, " found at position ",foundindex, " in the list."
• ELSE
• OUTPUT Name, " not found."
Q6 Linear Search + counting
• DECLARE ChoiceCount:INTEGER
• DECLARE foodtype :ARRAY[1:5] OF STRING
• DECLARE index:INTEGER
• //input the food name in the relevant position in the array
• foodtype[1]<-"ice cream"
• foodtype[2]<-"Soda"
• foodtype[3]<-"ice cream"
• foodtype[4]<-"Kola"
• foodtype[5]<-"ice cream"
• index<-0
• ChoiceCount<-0
• //initialize the index value to be 0 and choiceCount to be 0
• FOR index <-1 TO 5
• //check the elements in the array foodtype and find by the index if it is "ice cream"
• // then we will add the ChoiceCount by one
• IF foodtype[index]= "ice cream" THEN
• ChoiceCount<-ChoiceCount+1
• ENDIF
• NEXT index
• OUTPUT "Found ice cream for number",ChoiceCount
Q7 Bubble Sort

DECLARE Temperature: ARRAY [1:10] OF INTEGER DECLARE First:INTEGER. DECLARE Last:INTEGER DECLARE Swap:BOOLEAN
DECLARE Index:INTEGER DECLARE Temp:INTEGER
• //identify the variable First last to be integer and Swap to be boolean
• First<-1 Last<-10 Temperature[1]<-10 Temperature[2]<-9 Temperature[3]<-8 Temperature[4]<-7 Temperature[5]<-6
Temperature[6]<-5 Temperature[7]<-4 Temperature[8]<-3 Temperature[9]<-2 Temperature[10]<-1
• //assignment the desecond order value in the array from bigger to small//Temperature [1]=10 Temperature
[2]=9 .........Temperature[10]<-1
• REPEAT
• Swap <- FALSE
• //Every time initialize the Swap boolean variable to be FALSE
• FOR Index <- First TO (Last - 1)
• //inside for loop will loop by the index number from First to (Last-1) =9 the second // lastest index of the
element in the array.
• IF Temperature[Index] > Temperature[Index + 1] THEN
• //everytime the index number will increase form First =1 to the last-1 //(Temperature[Index] compare with
Temperature[Index + 1] if it is bigger then
• Temp <- Temperature[Index]
• Temperature[Index] <- Temperature[Index + 1]
• Temperature[Index + 1] <- Temp
• //exchange the two variables
• Swap <- TRUE//assignment the Swap to be TRUE
• ENDIF
• NEXT Index
• Last <- Last – 1 //every time we minus the last value by one will reduce the times of loop
• OUTPUT Temperature
• UNTIL (NOT Swap) OR Last = 1 //if Swap = False or exchange until the last to 1
Q 8 Range Check
• DECLARE StudentMark:INTEGER
• OUTPUT "Please enter the student's mark "
• REPEAT
• //This repeat will loop until the StudentMark is bigger or equals to 0 and
smaller or equals to 100
• INPUT StudentMark
• IF StudentMark < 0 OR StudentMark > 100
• //This if statements will check the StudentMark is out of [0,100] area
• THEN
• OUTPUT "The student's mark should be in the range 0 to 100, please
re-enter the mark "
• ENDIF
• UNTIL StudentMark >= 0 AND StudentMark <= 100
• OUTPUT "Your input of the ",StudentMark,"is accepted"
Q 9 Length Check
• DECLARE Password:STRING
• OUTPUT "Please enter your password of eight characters "
• REPEAT
• INPUT Password
• IF LENGTH(Password) <> 8
• THEN
• OUTPUT "Your password must be exactly eightcharacters,
please re-enter "
• ENDIF
• UNTIL LENGTH(Password) = 8
Question 10 Length Check
• DECLARE FamilyName:STRING
• OUTPUT "Please enter your family name "
• REPEAT
• INPUT FamilyName
• IF LENGTH(FamilyName) > 30 OR LENGTH(FamilyName) < 2
• //LENGTH(String)means check the length of the string
variable
• THEN
• OUTPUT "Too short or too long,please re-enter "
• ENDIF
• UNTIL LENGTH(FamilyName) <= 30 AND LENGTH(FamilyName) >= 2
Question 11 Presence check

• DECLARE EmailAddress:STRING
• OUTPUT "Please enter your email address "
• REPEAT
• INPUT EmailAddress
• IF EmailAddress = "»
• //If you have enter nothing and press enter then
• //it will print out *=Required need to input some char
• THEN
• OUTPUT "*=Required "
• ENDIF
• UNTIL EmailAddress <> ""

You might also like