Pseudocode Examples

Pseudocode to Average of a List of Numbers2 min read

Here is a pseudocode for calculating the average of a list of numbers:

This pseudocode prompts the user to enter a series of numbers and stores them in an array. It continues to do so until the user enters -1. It then calculates the average of the numbers by dividing the sum of the numbers by the number of elements in the array. Finally, it prints the result to the console.




Here is a detailed explanation of the code:

The code declares two variables: sum and count. sum will be used to store the sum of the numbers, and count will be used to store the number of elements in the array.

The code also declares an array numbers to store the input numbers.

The code initializes sum to 0 and count to 0.

The code prompts the user to enter a number and stores it in the first element of the numbers array (INPUT numbers[count]).

The code then enters a loop that continues as long as the current element is not -1 (WHILE numbers[count] != -1 DO). Inside the loop:

The code adds the current element to sum (sum = sum + numbers[count]).

The code increments count by 1 (count = count + 1).

The code prompts the user to enter another number and stores it in the next element of the numbers array (INPUT numbers[count]).

After the loop ends, the code checks if count is greater than 0 (IF count > 0 THEN). If it is, the code calculates the average by dividing sum by count and prints the result to the console. If count is not greater than 0, it means that no numbers were entered and the code prints a message to the console indicating this.

The code ends.

Leave a Comment