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


Thursday, February 5, 2015

Problem Solving Phase

The key to solving a problem is understanding what is being asked to do, logic how to do it, and get it done.



The Five (5) Steps in Problem Solving Phase
  1. Define the problem
  2. Find a solution
  3. Evaluate alternate solutions
  4. Choose the most efficient solution and write an algorithm
  5. Test algorithm for correctness
To define the problem, programmers use a defining diagram, commonly referred to as an IPO table. 
Defining Diagram - a three column table that separates a given problem into Input, Processing, and Output.

The IPO table does not solve the problem; it simply outline the procedural steps.  Calculations are not shown in the table.  

Example of IPO Table
 
Problem: Calculate and output the total cost of three items.


Input
Processing
Output
Cost1
Cost2
Cost3
1.   Read Cost1, Cost2, Cost3
2.   Calculate Total
3.   Print Total
Total

Input
We have assigned a name (variable name) to each of the cost values that will be entered.
we will learn more about variables at a later point.

Processing
Each processing step is numbered.  
The processing steps are what steps the computer will have to do to solve the problem.
The first processing step is always to (read) the variables that are inputted.
The final processing step is always to display (or print) was is to be outputted.

Output
Anything that needs to be displayed or the result of what is being asked to solve

You try one...
Prepare the defining diagram for the following problem:  Calculate and output the average of two quiz grades.  (hint...there are four processing steps)

Wednesday, January 28, 2015

Thursday, January 22, 2015

The Internet and the World Wide Web

The Internet and The World Wide Web
ž  Many people use the terms Internet and World Wide Web (aka. the Web) interchangeably, but in fact the two terms are not synonymous. The Internet and the Web are two separate but related things.
ž  The Internet is a massive network of networks, a networking infrastructure. It connects millions of computers together globally, forming a network in which any computer can communicate with any other computer as long as they are both connected to the Internet. Information that travels over the Internet does so via a variety of languages known as protocols.
ž  The World Wide Web is a way of accessing information over the medium of the Internet.
ž   It is an information-sharing model that is built on top of the Internet. The Web uses the HTTP protocol, only one of the languages spoken over the Internet, to transmit data.
ž  The Web also utilizes browsers, such as Internet Explorer or Firefox, to access Web documents called Web pages that are linked to each other via hyperlinks.
ž  The Web is just one of the ways that information can be disseminated over the Internet.
ž  The Internet, not the Web, is also used for:
ž   e-mail, which relies on SMTP (simple mail transfer protocol)
ž  Usenet (worldwide bulletin board system) news groups
ž  Instant messaging and FTP (file transfer protocol; works the same as HTTP).
ž  So the Web is just a portion of the Internet

Search Engines...
ž  A ‘search engine’ keeps indexed records of all the web pages that can be accessed via hot links.
ž  It is a software system that is designed to search for information on the WWW
ž  There are three major types of search engines:
¡  Crawler-based search engines
¡  Human-powered directories
¡  Meta-search engines
ž  Crawler-based search engines create their listings automatically by using a piece of software to ‘crawl’ or ‘spider’ the web and then index what it finds to build the search base. 
ž  They are good when you have a specific topic in mind and can be very efficient in finding relevant information in that situation. 
ž  Examples include:
¡  Google
¡  AllTheWeb
¡  AltaVista
ž  Human-powered directories depend on human editors to create their listing.  Typically, webmasters submit a short description to the directory for their websites, and these manually edited descriptions will form the search base.
ž  They are good when you are interested in a general topic of search.
ž  Not an efficient way to find information when a specific search topic is in mind.
ž  Examples include:
¡  Yahoo directory 
ž  (Yahoo also provides crawler-based results)
¡  Open Directory
¡  LookSmart
ž  Human-powered directories depend on human editors to create their listing.  Typically, webmasters submit a short description to the directory for their websites, and these manually edited descriptions will form the search base.
ž  They are good when you are interested in a general topic of search.
ž  Not an efficient way to find information when a specific search topic is in mind.
ž  Examples include:
¡  Yahoo directory 
ž  (Yahoo also provides crawler-based results)
¡  Open Directory
¡  LookSmart
ž  Meta-search engines transmit user-supplied keywords simultaneously to several individual search engines to actually carry out the search. 
ž  Search results returned from all the search engines can be integrated, duplicates can be eliminated and additional features such as clustering by subjects within the search results can be implemented by meta-search engines.
ž  Meta-search engines are good for saving time by searching only in one place and sparing the need to use and learn several separate search engines.