Sie sind auf Seite 1von 8

Summer 2009/10

ITEC 113
LECTURE NOTES 1 :
Introduction to Computer Programming Concepts.
What is a Computer?
A computer is accepting, storing and processing data in order to produce the
required information (output).

Computers do what we tell them to do they dont do anything by themselves.

A computer is an electronic device that works under the control of stored


programs. In our life, the role of computers are very important. You are
using them almost everywhere (in mobile phones, washing-machines, TV
sets, cars ).

Why we are using Computers?

Computers produce fast, accurate and reliable results.

While computers do the boring, repetitive, ordinary jobs, human beings can
spend their whole efforts and time to the more interesting and creative
works.

The use of computers in business and manufacturing decreases the cost of


produced goods and services.

It is more difficult and needs more time to find or grow up a skillful labor in
industry, while buying an additional computer and installing the required
software on is more easy and cheaper.

Data, Information and Knowledge

Data is the raw fact, gathered from the environment which does not have
much meaning.

Information is the end product of the processing of data, which has more
meaning, and is used in decision making.

Knowledge is the proved and generalized form of information, that is used in


strategic planning.

Data

Information

Knowledge

What is a Computer Program?

A computer program is a set of instruction written in a computer language


in order to be executed to perform a specific task.
There are tens of programming languages, used nowadays.
Computer Programs are also named as SOFTWARE.

Who is a Programmer?

A programmer is a person who writes the required computer programs.


Programmers translate the expected tasks given in human understandable
form into the machine understandable form by using compilers and
interpreters.

Types of Errors
Syntax Errors: Violation of syntactic rules in a Programming Language generates
syntax errors.
Effect? Compiler helps user to identify the Syntax error and correct it
Semantic Errors: Doing logical mistakes causes semantic errors .
Effect? The Compiler can not notice these errors, but on execution, they cause
unexpected wrong results. These errors can only be corrected by the careful
programmer
Run-time Errors: Occur on program execution. Mostly caused by invalid data entry
or tries to use not existing resources. ( E.g. Attempting to divide a number by 0 )
Effect? It occurs on run time and may crash the program execution.
Well Designed Programs.
Well designed programs must be:
Correct and accurate
Easy to understand
Easy to maintain and update
Efficient
Reliable
Flexible

3
The Way of

Problem Solving Using Programs :


Analyze the problem.
Write the algorithm (flowchart or pseudocode )
Check the algorithm.
Code the algorithm into a program
Check the program
Run the program ( test and modify if necessary).

What is Algorithm?

An algorithm is the method of solution of a given problem. This method of


solution should be designed in a programming manner.

Before starting to write the code of a computer program, a programmer has


to decide and design steps that is required to solve the problem; this is called
designing the algorithm of the proposed program.

While programmers design their software, they use algorithm tools to make
their works more clear, easy to understand, and easy to remember. Because,
these algorithms will later be used by themselves and may be by other
programmers .

Problem : Write an algorithm for a program which will sum up and display the
following:
Sum = 2 + 5 + 8 + 11 + 14
Method of Solution Using Stones: ( Algorithm )
1. Make sure that the bigcup is empty
2. Note number 2 on symbol n on paper
3. Read the number in symbol n and then count so many stones and put them in
smallcup
4. Pour the stones from the smallcup in the bigcup.
5. Increase the symbol n by 3
6. If the number in symbol n is less than or equal to 14 goto line 3
7. Count and display the total number of stones in the bigcup.
More refined Method of solution of the same problem using GOTO instructions: (
Algorithm )
1.
2.
3.
4.
5.

set bigcup to 0
set n to 2
add n to bigcup
Increase n by 3
If n is less than or equal to 14 GOTO line 3

4
6. display bigcup
Steps 3, 4, 6 repeating ( Looping statement )
Step 5 selection operation ( Selection statement )
Trace(Test) above algorithm and show that it is producing correct result
Method of Solution ( Algorithm )
1.
2.
3.
4.
5.
6.

set bigcup to 0
set n to 2
add n to bigcup
Increase n by 3
If n is less than or equal to 14 GOTO line 3
display bigcup

Program Code
#include <stdio.h>
main()
{
int bigcup =0,n=2;
start:
bigcup = bigcup + n;
n = n + 3;
if ( n <= 14 ) goto start;
printf("\nSum = %d",
bigcup);
}

Method of solution using WHILE . ENDWHILE structure: ( Algorithm )


set bigcup to 0
set n to 2
WHILE n is less than or equal to 14
add n to bigcup
Increase n by 3
ENDWHILE
display bigcup

Method of Solution ( Algorithm )

Program Code
#include <stdio.h>
set bigcup to 0
main()
set n to 2
{
WHILE n is less than or equal to 14 int bigcup =0,n=2;
add n to bigcup
while ( n <= 14 )
Increase n by 3
{
ENDWHILE
bigcup = bigcup + n;
display bigcup
n = n + 3;
}
printf("\nSum = %d", bigcup);
}

5
Method of solution using FOR . ENDFOR structure: ( Algorithm )
set bigcup to 0
FOR n going from 2 to 14 with steps of 3
add n to bigcup
ENDFOR
display bigcup

Method of Solution ( Algorithm )


set bigcup to 0
FOR n going from 2 to 14 with steps
of 3
add n to bigcup
ENDFOR
display bigcup

Program Code
#include <stdio.h>
main()
{
int bigcup =0;
for ( int n=2 ; n<=14 ; n+=3 )
{
bigcup = bigcup + n;
}
printf("\nSum = %d",sum);
}

Problem : Write the algorithm for a program which will prompt the user to input 10
integer numbers. The algorithm and the program would then calculate and display
the average of these numbers.
Set counter to 0
Set sum to 0
WHILE counter less than or equal to 10
Input n ( Enter a value to n )
Add n to sum
Increase counter by 1
ENDWHILE
Calculate sum/n and set it to average
Display average
Problem : Write an algorithm for a program which will display the following:

Set n to 5
WHILE n is less than or equal to 25
Display n and n*n
`Increase n by 4
ENDWHILE

FOR n going from 5 to 25 with steps of 4


Display n and n*n
ENDFOR

Tools of Algorithm
There are so many Algorithm tools available today, but the most famous ones
are Flowcharts and Pseudo-Codes.
In this part of the course, we will try to learn and use both Flowchart and
Pseudo-code techniques while we are designing algorithms (finding solutions)
for the given problems.
A) Flow Charts
Flowcharts are graphical tools, containing a set of shapes, each expressing a
different action in a sequence of program execution.
While there are so many shapes for specific purposes, to avoid complexity, in
this course, only a limited subset of these shapes will be shown and going to
be used in applications.

Every flowchart has to start with a TERMINAL shape containing the


caption START and has to end with another TERMINAL shape containing
the caption of END.
INPUT / OUTPUT shape is used to indicate the time and place of reading
some values and/or giving some output to the user.
PROCESS symbol is used to represent assignments and manipulations of
data such as arithmetic operations or movement of data from one variable to
the other.
DECISION symbol represents the comparison of two values. Alternating
course of actions will be followed depending on the result of checked criteria.
CONNECTOR symbol is used to represent the exit to or entry from another
part of the program. It is used to show the connections of two pages, when
your design occupies more then one page.
FLOWLINE symbol is used to show the direction of the program flow
between other symbols.

Symbols of Flow Charts

Structured English (Pseudocode)


Pseudo-code or Structured English is recognized as an alternative method to
flowcharts for planning structured programs.
There are no general accepted standards for pseudo-codes. We will work
with a form that has minimum number of rules and is essentially languageindependent.
Since pseudo-code instructions are written in English, they can be easily
understood and reviewed by users.
The only syntax rules to be concerned with involve the LOOP and
SELECTION structures. They has to be used as CAPITALISED words.