Sunday, May 17, 2015

Introduction to Loops - For Loop

There are several methods used for repetition within structured algorithms; the first that we will be learning is Loops.

The three types of Loops that we will be covering are:
For Loop (also known as Do Loop or Counter Controlled Loop)
While Loop
Repeat Until Loop

The For loop is used to repeat code for a specified number of times.  It falls under the control structure of repetition control structure.

First we take a look at the For Loop.  This is used when the number of repetition is known.  The syntax is as follows:





Example:
Problem Statement:  Calculate and output the sum of ten numbers.

  1. total = 0
  2. For x = 1 to 10
  3.      Print "Enter a number"
  4.      Input num
  5.      total = total +1
  6. Next
  7. Print "The sum of ten numbers entered is", total
  8. Input key

Line 1 is an example of initializing a variable.  That means that we are giving it a starting value.
Line 2 indicates that we will repeat lines 3-5 for ten times.
Line 3 is an output statement hosting a prompt.
Line 4 is an input statement whereby the value entered will be stored in the variable num.
Line 5 allows a tally to be kept each time a number is entered.
Line 6 terminates the loop.  The syntax states End For, however, the compiler uses the command Next.
Line 7 displays the total of the ten numbers with a suitable label.
Line 8 terminates the algorithm

Now, you try one:  Calculate and output the average current GPA of the students in your class based on their first, second and third form GPA.  Ask the user to enter the number of students in the class, store it in the variable Class; that variable becomes your terminating value.

Sunday, February 22, 2015

Conditional Algorithm

Conditional algorithms have selection statements in them.  Their control structure is known as Selection Control Structure.

The first illustration is that of the if-then construct.
The second illustration is that of the if-then-else construct.

Examples:
1. If grade is 70 or more, output Pass.
2. If grade is 70 or more, output Pass; otherwise output Fail.

Example Problem:
Write an algorithm that determines and outputs if the average of a student allows him to pass or fail; the passing mark is 70.

Solution #1: if-then construct
Print "Enter the average of a student"
input avg
if avg >= 70 then
print "Pass"
end if
input key

Solution #2: if-then-else construct
Print "Enter the average of a student"
input avg
if avg >= 70 then
print "Pass"
else
print "Fail"
end if
input key

Now you try one:
Write an algorithm that prompts the user to enter the name and cost of two items.  The algorithm should calculate and output the total cost.  If the total exceeds $25.00 then offer a discount of 10%; otherwise a discount of 5%; show the new total after discount.

Algorithms

There are four types of algorithms:  Simple, Conditional, Loops and Arrays

Simple Algorithms - basic calculations
Conditional Algorithms - selection statements

Algorithms are series of instructions that rigorously defines the solution to a problem.

The attributes of an algorithm are:

  1. Must be precise
  2. Must be ambiguous
  3. Must be finite
  4. Must be done in a logical order
Example of given problem:
Write an algorithm that prompts the user to enter two grades, and shows the average of the two grades.

Indicators that it is a simple algorithm:  basic calculation of entering data, performing calculation, and outputting results.

Solution:
Print "Enter a grade"
Input grade1
Print "Enter another grade"
Input grade2
Let total = grade1 + grade2
Let avg = total / 2
Print "The average of two grades is", avg
Input key

Sunday, February 8, 2015

Problem Solving Phase

Click on the link below for a short video about the five steps in the problem solving phase along with an important message.

Intro to Programming by Ms Young