To create an algorithm that assists a school in calculating its canteen expenses summing up to 12 cedis, we'll outline a clear step-by-step process. The final output will involve displaying the result in a constant variable, let's name that constant P
. Below is a structured algorithm that works in seven simple steps.
Algorithm
-
START
Begin the algorithm. -
Initialize Variables
Set a constant variableP = 12
.
Additionally, you may want to initialize a variablesum = 0
to hold the accumulated expenses. -
Input Item Prices
Prompt the user to enter the prices of items sold in the canteen until the total reaches or exceeds 12 cedis.
Use a loop to continue prompting for prices. -
Update Sum
For each entered price, add it tosum
.
Ifsum
exceeds 12, alert the user that they have exceeded the budget. -
Check Total
Ifsum
equals 12, confirm to the user that they have met the target of 12 cedis.
Ifsum
is less than 12, inform the user how much more they need to reach 12 cedis. -
Display Result
SetP = sum
ifsum
equals 12, and prepare to display the result.
Display the message: “Total Canteen Expenses: P cedis”. -
STOP
End the algorithm.
Pseudocode
1. START
2. P = 12
3. sum = 0
4. WHILE sum < P DO
a. PROMPT "Enter the price of the item:"
b. INPUT price
c. sum = sum + price
d. IF sum > P THEN
i. DISPLAY "Exceeded the budget!"
e. ENDIF
ENDWHILE
5. IF sum == P THEN
a. DISPLAY "You have met the target of 12 cedis."
ELSE
a. DISPLAY "You need " + (P - sum) + " more cedis to reach 12 cedis."
ENDIF
6. DISPLAY "Total Canteen Expenses: " + sum + " cedis."
7. STOP
Explanation
- Start: The algorithm initiates at step 1.
- Initialization: A constant
P
is used to define the target sum (12 cedis). - Input Loop: The algorithm continues to accept item prices until the total reaches or exceeds 12 cedis.
- Updating the Sum: Each time a price is entered, it's added to the
sum
. If the total exceeds 12, a warning is issued. - Check and Display: The final decision checks if the
sum
is equal to or less than 12 and displays the appropriate messages. - End: The process concludes gracefully.
This algorithm can be implemented in any programming language to facilitate the canteen calculations effectively.