Output: 24
(a)
INPUT bookTitle, newStatus
FOR i FROM 0 TO 99
IF TITLES[i] == bookTitle THEN
STATUS[i] = newStatus
ENDIF
ENDFOR
(b)
FOR i FROM 0 TO 99
IF STATUS[i] == "Available" THEN
OUTPUT TITLES[i] + " by " + AUTHORS[i]
ENDIF
ENDFOR
(c)
INPUT selectedAuthor
FOR i FROM 0 TO 99
IF AUTHORS[i] == selectedAuthor THEN
OUTPUT TITLES[i] + ": " + STATUS[i]
ENDIF
ENDFOR
(a)
FOR i FROM 0 TO 49
IF Salaries[i] >= 50000 THEN
BONUS[i] = Salaries[i] * 0.10
ELSE
BONUS[i] = Salaries[i] * 0.05
ENDIF
ENDFOR
(b)
In parallel arrays, each index corresponds to the same employee across all arrays. For example, the data at index 0 in the Names[], Ages[], Salaries[], and BONUS[] arrays all belong to the first employee, ensuring that the information is correctly matched.
(c)
FOR i FROM 0 TO 49
IF Ages[i] < 30 AND Salaries[i] > 40000 THEN
OUTPUT Names[i] + ", Age: " + Ages[i]
ENDIF
ENDFOR
(d)
INPUT maxAge, minSalary
FOR i FROM 0 TO 49
IF Ages[i] <= maxAge AND Salaries[i] >= minSalary THEN
OUTPUT Names[i] + ", Salary: " + Salaries[i]
ENDIF
ENDFOR
(a)
maxPopulation = 0
cityWithMaxPopulation = ""
FOR i FROM 0 TO 99
IF Population[i] > maxPopulation THEN
maxPopulation = Population[i]
cityWithMaxPopulation = Cities[i]
ENDIF
ENDFOR
OUTPUT cityWithMaxPopulation
(b)
highEmissionCities = []
FOR i FROM 0 TO 99
perCapitaEmission = CO2Emissions[i] / Population[i]
IF perCapitaEmission > 1000 THEN
APPEND Cities[i] TO highEmissionCities
ENDIF
ENDFOR
OUTPUT highEmissionCities
(c)
totalFines = 0
FOR i FROM 0 TO 99
perCapitaEmission = CO2Emissions[i] / Population[i]
IF perCapitaEmission > 999 AND perCapitaEmission < 2000 THEN
fine = 100 * Population[i]
ELSE IF perCapitaEmission > 1999 AND perCapitaEmission < 3000 THEN
fine = 125 * Population[i]
ELSE IF perCapitaEmission > 2999 THEN
fine = 150 * Population[i]
ENDIF
totalFines = totalFines + fine
ENDFOR
OUTPUT totalFines
So:
Output: 10, 20
A | B | C | B > C | B mod 4 = 0 | Output |
---|---|---|---|---|---|
1 | 10 | 5 | True | ||
4 | 8 | True | |||
6 | 6 | ||||
True | |||||
7 | 6 | False | 7 | ||
10 | 4 | False |
(a)
INPUT playerName, pointsScored
FOR i FROM 0 TO 14
IF NAMES[i] == playerName THEN
SCORES[i] = SCORES[i] + pointsScored
ENDIF
ENDFOR
(b)
FOR i FROM 0 TO 14
IF POSITIONS[i] == "Guard" AND SCORES[i] > 100 THEN
OUTPUT NAMES[i] + " - " + POSITIONS[i]
ENDIF
ENDFOR
a) That's a function that calls itself.
b)
So:
(a)
sum = 0
for row from 0 TO 7
for col from 0 TO 5
sum = sum + X[row][col]
end for
end for
return "Sum of all integers: " + sum
(b)
sum = 0
count = 0
for row from 0 TO 7
for col from 0 TO 5
sum = sum + X[row][col]
count = count + 1
end for
end for
average = sum / count
return "Average of all integers: " + average
(a)
MIN = WEIGHTS.getNext()
WEIGHTS.resetNext()
loop while WEIGHTS.hasNext()
TEMP = WEIGHTS.getNext()
if TEMP < MIN then
MIN = TEMP
end if
end loop
return MIN
(b)
WEIGHTS.resetNext()
TOTAL = 0
COUNT = 0
loop while WEIGHTS.hasNext()
TEMP = WEIGHTS.getNext()
TOTAL = TOTAL + TEMP
COUNT = COUNT + 1
end loop
AVERAGE = TOTAL / COUNT
return AVERAGE
(a)
test(InvoiceID)
N = length(PAID)
for i from 0 to N-1
IF PAID[i] == InvoiceID then
return "Paid"
end if
end for
return "Not paid"
end test
(b)
N = length(PAID)
FLAG = true
loop while FLAG = true
FLAG = false
for i from 0 to N-2
if PAID[i] > PAID[i + 1] then
TEMP = PAID[i]
PAID[i] = PAID[i + 1]
PAID[i + 1] = TEMP
FLAG = true
end if
end loop
end loop
loop for Count from 0 to 7
input NUMBER
if NUMBER mod 2 = 0 then
if NUMBER mod 4 = 0 then
output NUMBER
end if
end if
end loop
TITLES[]
, AUTHORS[]
, and STATUS[]
. The status of the books changes based on library activities.Title | Author | Status |
---|---|---|
The Great Gatsby | F. Scott Fitzgerald | Available |
To Kill a Mockingbird | Harper Lee | Checked Out |
1984 | George Orwell | Available |
STATUS[]
array based on the book titles that have been checked out or returned. Assume you are given the book title and the new status (either "Available" or "Checked Out").Names |
---|
John Smith |
Jane Doe |
Emily Johnson |
Ages |
---|
28 |
34 |
25 |
Salaries |
---|
55,000 |
48,000 |
60,000 |
BONUS[]
.
X = 2
Y = 10
loop while Y > X
X = X + 2
output(Y - X)
Y = Y - 2
end loop
City | Population | Total CO2 Emissions (metric kg) |
---|---|---|
New York | 8,336,817 | 60,000,000 |
Los Angeles | 3,979,576 | 40,000,000 |
Chicago | 2,693,976 | 30,000,000 |
func(X)
if X > 1
then
return func(X - 1) + func(X - 2)
else
return X
end if
end func
loop for Index from 0 to 7
input NUMBER
if NUMBER mod 2 = 0 then
if NUMBER mod 5 = 0 then
output NUMBER
end if
end if
end loop
A = 1
B = 10
C = 5
loop while B > C
A = A + 3
B = B - 2
if B mod 4 = 0 then
C = C + 1
end if
output(A + B - C)
end loop
NAMES[]
, POSITIONS[]
, and SCORES[]
. Each player's score changes as the season progresses.SCORES[]
array when a player scores some points in a new game. Assume you are given the player's name and the points they scored in a game. Add these points to their current season score.
fibonacci(n)
if n <= 1
then
return n
else
return fibonacci(n - 1) + fibonacci(n - 2)
end if
end fibonacci
2 | 3 | 7 | 0 | 1 | 9 |
1 | 5 | 9 | 4 | 3 | 2 |
9 | 0 | 4 | 6 | 2 | 4 |
. | . | . | . | . | . |
. | . | . | . | . | . |
7 | 7 | 6 | 9 | 0 | 3 |
230 | 310 | 17 | 315 | 29 | 111 | 821 | 109 |