Practical Work n°2 - Solutions
Exercise 1
Ask the user for the age of a child and display their category.
Categories:
- "Chick": 6-7 years old
- "Pupil": 8-9 years old
- "Minimal": 10-11 years old
- "Cadet": 12+ years old
Algorithm:
Start
Prompt user for age
Read age
If age >= 6 and age <= 7
Display "Chick"
Else if age >= 8 and age <= 9
Display "Pupil"
Else if age >= 10 and age <= 11
Display "Minimal"
Else if age >= 12
Display "Cadet"
Else
Display "Too young for category"
End
Exercise 2
Photocopy billing:
- First 10 copies: 0.10EUR each
- Next 10 copies: 0.09EUR each
- Remaining: 0.08EUR each
Algorithm:
Start
Prompt user for number of copies
Read n
total = 0
If n <= 10
total = n * 0.10
Else if n <= 20
total = 10 * 0.10 + (n - 10) * 0.09
Else
total = 10 * 0.10 + 10 * 0.09 + (n - 20) * 0.08
Display total cost
End
Exercise 3
Tax rules:
- Men over 20 years old
- Women aged 18 to 35
Algorithm:
Start
Prompt for age
Read age
Prompt for gender (M/F)
Read gender
If gender == 'M' and age > 20
Display "Taxable"
Else if gender == 'F' and age >= 18 and age <= 35
Display "Taxable"
Else
Display "Not taxable"
End
Exercise 4
Bakery open:
- 7-13h and 16-20h
- Closed Monday afternoon and Tuesday all day
Algorithm:
Start
Prompt for hour (0-23)
Prompt for day (1=Mon,...,7=Sun)
If (hour >= 7 and hour < 13) or (hour >= 16 and hour < 20)
If day == 2 or (day == 1 and hour >= 13)
Display "Closed"
Else
Display "Open"
Else
Display "Closed"
End
Exercise 5
Declare an array of 7 numerical values and set them to 0.
Algorithm:
Start
Create array[7]
For i = 0 to 6
array[i] = 0
Display array
End
Exercise 6
Declare an array containing 6 lowercase letters of Latin alphabet.
Algorithm:
Start
array = ['a', 'b', 'c', 'd', 'e', 'f']
Display array
End
Exercise 7
Declare array of 9 notes entered by user.
Algorithm:
Start
Create array[9]
For i = 0 to 8
Prompt user for note
Read array[i]
Display array
End
Exercise 8
Create array from sum of two equal-length arrays.
Algorithm:
Start
Create A[5], B[5], C[5]
For i = 0 to 4
Read A[i] and B[i]
C[i] = A[i] + B[i]
Display C
End
Exercise 9
Sum = A[0]*B[0] + A[1]*B[1] + ...
Algorithm:
Start
Create A[6], B[6]
For i = 0 to 5
Read A[i], B[i]
sum = 0
For i = 0 to 5
sum += A[i] * B[i]
Display sum
End
Exercise 10
Reverse an array.
Algorithm:
Start
Read array of size n
For i = 0 to n/2
Swap array[i] with array[n-1-i]
Display array
End
Exercise 11
Remove an element at a given index.
Algorithm:
Start
Read array
Prompt user for index to delete
For i = index to n-2
array[i] = array[i+1]
Reduce array size by 1
Display new array
End
Exercise 12
Divide A by B without using DIV or MOD.
Algorithm:
Start
Read A, B
quotient = 0
While A >= B
A=A-B
quotient += 1
remainder = A
Display quotient and remainder
End
Exercise 13
Count positives per column and product of negatives per row.
Algorithm:
Start
Read matrix A[n][m]
For each column j
Count positives in column j
For each row i
Compute product of negatives in row i
Display results
End
Exercise 14
Swap diagonals in a square matrix.
Algorithm:
Start
Read matrix A[n][n]
For i = 0 to n-1
Swap A[i][i] with A[i][n-1-i]
Display matrix
End
Exercise 15
Check if a string is a palindrome.
Algorithm:
Start
Read string s
If s == reverse(s)
Display "Palindrome"
Else
Display "Not palindrome"
End