2.4 Practice Problems
To truly get a grasp of writing pseudocode and understanding flowcharts, it's essential to engage in practical exercises. Here are a few practice problems to help you solidify your understanding:
Problem 1
You are tasked to design an algorithm that prints out all the numbers from 1 to 100 that are divisible by 7.
Pseudocode solution:
Algorithm: Print Numbers Divisible by 7Input: NoneOutput: Numbers from 1 to 100 that are divisible by 7 1. For each number N from 1 to 100, do2. If N is divisible by 7 then3. Print N4. End If5. End ForProblem 2
Design an algorithm that checks whether a given word is a palindrome or not. A palindrome is a word that reads the same backward as forward, e.g., "level".
Pseudocode solution:
Algorithm: Check PalindromeInput: A word - WordOutput: Whether the word is a palindrome 1. Initialize Start to 02. Initialize End to the length of Word - 13. While Start is less than End, do4. If the character at position Start in Word is not equal to the character at position End in Word then5. Return "Not a palindrome"6. End If7. Increment Start by 18. Decrement End by 19. End While10. Return "Palindrome"Problem 3
Design an algorithm that, given a list of numbers, finds the highest number in the list.
Pseudocode solution:
Algorithm: Find Highest NumberInput: A list of numbers - NumbersOutput: The highest number in the list 1. Initialize Highest to the first number in Numbers2. For each Number in Numbers, do3. If Number is greater than Highest then4. Set Highest to Number5. End If6. End For7. Return HighestWe recommend trying to draw flowcharts for these algorithms as well. This practice will solidify your understanding of how algorithms are represented graphically and will make you more comfortable with both forms of algorithm representation.
Remember, practice is the key to learning. By working through these problems and creating your own, you'll enhance your ability to translate real-world problems into algorithms.