Problem Statement: Write an algorithm to average three numbers.
Given: Variables 'a', 'b', and 'c', which all hold the numbers to average.
Starting Point
Here is our starting point. You will probably recognize that the format looks like an object method.
The problem statement said we are given three numbers, and we should compute and return the average of these nums.
ave(a, b, c) {
//ALGO HERE
}
Adding Comment
We want to write a nice comment at the top of our algo.
This helps us write the algo.
ave(a, b, c) {
//Compute the average of the three method params//Return the result
}
Writing Algorithm in Human Language
Here is algorithm written in human language (English).
This "human language" approach is a good way to start an algo.
ave(a, b, c) {
//Compute the average of the three method params//Return the result.
Compute sum of 'a' plus 'b' plus 'c' and assign to 'sum'
Assign 3 to 'count'
Divide 'sum' by 'count' and assign to 'result'
Return 'result'
}
Writing Algorithm in Pseudocode
Using the original problem statement combined with our human language algorithm, we have written the pseudocode here.
function ave(a, b, c) {
//Compute the average of the three method params//Return the result.let sum = a + b + c
let count = 3
let result = sum / count
return result
}
//run-it
println(ave(2, 3, 5))
Pseudocode Breakdown
let sum = a + b + c
let sum = a + b + c -- says to assign the sum of (a + b + c) to the var "sum"
let count = 3
let count = 3 -- says to assign "3" to the var "count" ("3" because we have three nums)
let result = sum / count
let result = sum / count -- says to assign "sum / count" to the var "result"
return result
return result -- says to return the var "result" to the caller of this method or function -- "return" is a commonly used keyword in algos and code
Data Structures And Algorithms (DSA)
(Chapter 102 - Introduction to Pseudocode)