The following can help explain the concept of algorithms in relation to programming.
Problem: Calculate the average of three given numbers.
Let the given numbers be a, b, c
Step 1: Set aside memory for all values (a, b, c,)
Step 2: Assign the values to a memory location (the given numbers)
Step 3: Calculate the average of the values using the formula (a+b+c)/3
Step 4: Display the answer in the proper format, in this case the average.
The problem statement is programmatically solved with the use of four steps in the algorithm.
Problem:Find the gain or loss of an item, as the case may be, and compute the gain or loss percentage, given the cost price and the selling price.
Gain = S.P. (sell price) - C.P (cost price).
Gain % = (Gain * 100)/C.P.
Loss = C.P. - S.P.
Loss % = (Loss * 100)/C.P.
(Percentage gain or loss is calculated on the C.P.)
Algorithm:
Step 1: Obtain the values of S.P. and C.P. Set aside memory for S.P. and C.P., gain percent and loss percent.
Step 2: Assign values to memory location.
Step 3: Perform mathematical computations.
Step 4: Display results in the proper format.
If C.P. = S.P, then
Declare "There is neither gain nor loss in the transaction"
Stop
If S.P. > C.P., then
Calculate Gain = S.P - C.P
Calculate Gain % = (Gain * 100)/C.P
Write "Answer: Gain is $" followed by value of Gain
Write "Answer: Gain % is" followed by value of Gain %
Stop
Else
Calculate Loss = C.P. - S.P
Calculate Loss % = (Loss * 100)/C.P.
Write "Answer: Loss is $" followed by value of Loss
Write "Answer: Loss % is" followed by values of Loss %
Stop
The algorithm is executed in order, starting with Step 1:
In step 1, we set aside memory locations for S.P. and C.P
In step 2, we assign the numerical values of S.P. and C.P. to the memory locations.
In step 3, the answer to the phrase If C.P. - S.P. could be yes or no. If the answer is yes, then only the instructions following the word "then" are executed.
In step 3, if the answer to the phrase S.P. > C.P. is yes, then steps to a to e before else are executed. If the answer to S.P. > C.P. is no, implying that C.P. >= S.P., steps a to e after else are executed.
Step 4 is the last in the process. The result of the computations is displayed and the algorithm is terminated after its execution.
Words such as "IF", "THEN", and "ELSE are used to emphasize their distinctive nature in executing the algorithm.
Flowcharts
When thinking of a Flowchart think of a visual representation of how a process works, depicting the sequence of steps at a minimum. A flowchart offers many advantages including the following.
Helps locate the key elements of a process. It draws clears lines between the end of one process and the start of another.
Brings clarity and improvement to a process.
Helps identify appropriate areas for improvement in a program.
The following shows a format of a sample flowchart that identifies the starting and ending points of a sequential process with decision points.
Pseudocode, aka false code, is an informal language for relating problem solving steps. The steps used in pseudocode are simple and can be implemented with ease into any programming language. Pseudocode uses a combination of English words, mathematical notations, and a set of capitalized keywords such as BEING, END, READ, PRINT, FREAD, FPRINT, IF, ELSE, ELSEIF, ENDIF, WHILE, DO and ENDWHILE.
Pseudocode consists of:
Fundamental control structures such as Sequence, Selection and Repetition.
Three other types of statements, which include Input, Output and Assignment.
Subroutines or small programs that can be added to modularize pseudocode. The subroutines are also written using the pseudocode notation.
Here is an example of pseudocode.
Begin procedure
Input Widget Price
Taxes = 0.06 x Widget Price
Amount Due = Widget Price + TaxesOutput Taxes and Amount Due
End procedure
The following shows the flowchart of gain and loss for a business.End procedure
Flowcharts depict different types of actions and steps in a process through special shapes. The sequence of steps and the relationships between these steps is shown using arrows. The following is a list of the different shapes in a flow chart and what they represent.
No comments:
Post a Comment