Unit 18: Procedural Programming

Unit 18: Procedural Programming

Introduction to Program Logic Formulation
With Basic C

Chapter 1
Data and Information
Data are raw facts and figures which are meaningless.

Information are useful processed data.

Procedure vs. Computer Program
Procedure is a logical sequence of actions, either consciously or subconsciously, to carry out a certain task or process.

Elements of Procedure

Action – the sequence of steps to be taken
Order – specifies the logical arrangement of the actions
Data – the elements operated upon by the action
Language – the medium of communication
Computer and Program
Computer is an electronic device designed to manipulate data.

Program is a set of instructions to carry out a particular task.

Computer program is a set of instructions which directs computer to perform/carry out a particular task. Elements of Computer Program
Operation – define the series of actions to be followed by the computer Order - specifies the logical arrangement of the operations Data - the elements operated upon by the operations
Language – the medium by which the operations are communicated Program Development Process
Problem Analysis
Program Design
Program Coding
Program Debugging and Testing
Program Implementation and Maintenance
Problem Analysis
It is the step wherein a good definition of the problem are considered and the program specification are developed.

Program specification is a document being prepared as a part of system design which contain the following: Program Background
I/O Specifications
Program Methodology

Program Design
It is the stage where logical sequence/ arrangement of operations for the solution have to be planned, consistent with the program specifications.

Algorithm is a written series of logical instructions which accomplish solution to a problem. Algorithms are developed by using any of the program design tools such as structure charts, flowcharts, decision table, pseudo codes, etc. Program Design Tools

Commonly used program design tools:

Structure/hierarchy chart - depicts the overall organization of a program, it shows how segments/modules are defined and how they relate to one another

Program Design Tools
Flowchart – it shows graphical presentation of an algorithm - also known by other names such as block diagram, logic diagram, run diagram, flow diagram and system chart

Program Design Tools
Decision table – is a table showing the actions to be taken for different combinations of condition (conditions and actions)

Pseudo codes – it describes the logical flow of the solution to a problem through the use of English–like statements (structured English) Program Coding
Coding – is the process of writing the actual program steps in the proper format for a particular programming language

Factors that may affect the programming language used are:
Suitability
Integration
Standards
Portability
Development speed
Program Testing and Debugging
Testing – refers to the process of verifying the accuracy or workability of the program

Debugging – refer to the process of locating/identifying and correcting the errors in the program

Two main types of errors:
Syntax error – occurs when the programmer has not followed the rules of the programming language Logic error – occurs when running a program produces incorrect results

Testing and Debugging
Source code – a computer program before it is compiled

Object code – the machine language version of the computer program generated after the program source code is compiled

Compiler – is a system special software which translates a programming language into machine language and assembles the program to check errors upon correct usage of programming language

Program Documentation
It entails the preparation of a document describing how the program is constructed, together with all the significant resources surrounding its creation

This phase is performed at every stage of the program development such that an output of each development stage is documented

Program Implementation and Maintenance
It is the final stage of program development life cycle which involves running the system and updating the software as needed

Program continually be modified or revised to cope with changes and persistently be useful. Evolution of Programming Languages
“Programming languages were developed to facilitate communication between the user and the computer.”

Programming Language Era
Machine Language
Symbolic Language
High – level Language
Fourth Generation Language

Machine Language
It is the only language that computer can understand which consists of a string of ones and zeros (1s and 0s)

It is machine-dependent

Computer ready to execute it
Symbolic Language
Assembly language/Language processor – it enabled programmer to write instructions that used symbols, words or abbreviations which stand for certain operations

Assembler – it translate the assembly language into machine language

It is machine-dependent

High – level Language
Compiler/interpreter – it is a special software which act as language translator for high-level language

It is machine independent

The use of high-level language means lesser time in writing program

High – level Programming Languages
Some of the high-level programming languages:

Fortran (Formula Translation)
Started in 1954 by IBM Laboratory
It is a scientific language used in research applications or analysis of problems in science, engineering and sometimes in business Lisp (List Processing)
Developed in 1958
Designed for the manipulation of symbols and patterns
One of the most popular languages for Artificial Intelligence

High – level Programming Languages
Cobol (Common Business Oriented Language)
Developed in 1959 by USA Army
Designed for administrative purposes and among its significant features are its file processing, editing, and I/O capabilities PL/1 (Programming Language 1)
Developed in the middle of 1960s by IBM
Characterized as general-purpose and procedural applicable to business as well as scientific applications Basic (Beginners All – purpose Symbolic Instruction Code)
Developed in 1965 by John Chimney and Thomas Kurt at Dartmouth College, New Hampshire, USA Described as conversational, procedure-oriented and general-purpose programming language

High – level Programming Languages
Prolog (Programming in Logic)
Developed in 1972
A logic-language based on logical rewrite systems that can support artificial intelligence and expert system Pascal
Named after Blaise Pascal, a brilliant 17th century mathematician Designed by Niklaus Wirth at the Institute fur Informatik in Zurich (1971) Intended as an instructional language
RPG (Report Program Generator)
Designed for programming business-oriented reports
High – level Programming Languages
ADA
Named after Lady Ada Byron
Conceptualized in 1974 by the US Department of Defense for military applications C (Successor to B)
Designed by Dennis Ritchie during 1969 to 1971
One of the most powerful languages at all times which is very well structured, powerful, portable, and flexible Smalltalk
Developed at Xerox Palo Alto Research Center in 1972
An extreme object oriented language
High – level Programming Languages
C++(C + 1 in the C language)
Developed in 1985 at AT&T Bell Laboratory by Bjarne Stroustrup One of the most popular object-oriented languages
Java
Developed in 1994 by James Gosling at Sun Microsystems
Combination of C++ and Smalltalk, developed to be as portable as possible

Fourth Generation Language
It aims to make programming easier even to non-technical people

Three categories of Fourth Generation Language:
Database and Query Languages
Natural Languages
Intelligent Languages

PLF Basic Concepts
Chapter 2
Constant
Literal values that does not change during program execution It can be numeric or string type

Numeric Constant- may be an integer(whole number) or real number(with decimal place). It may be preceded by a positive or negative sign. Ex. 101, -3, 0.6, 35.00, 0, 25

B. String Constant- are characters enclosed in double quotation marks(“ “) symbol.

- the numbers 0 to 9, letters a to z or A to Z, all special characters like space, %, $, - etc. , may be included.

Ex. “AB”, “Madonna”, “.”, “24”, “ 87 Sta. Veronica”. Character Set
It is the basic element of a programming language composed of characters or symbols and combinations

Character set:
Low value- LOW VALUE
Special characters- .:;=\< (+ !%#*&^$@)”’/
Alphabetic- A-Z, a-z
Numeric- 0-9
High value- HIGH VALUE

Data Types
In programming, each item of data is classified according to a specific type and there are rigid rules governing each type

There are four types of data types:
Integer
float
Boolean
Character
string

Integer vs. Real
Integer
Is a whole number
It can combined with a unary plus (+) to indicate positive or unary minus (-) or negative value, respectively

float
Used to represent floating point values
It can combined with a unary plus (+) to indicate positive or unary minus (-) or negative value, respectively

Boolean, Character and String
Boolean
It is a data type which bears a value of either true or false (on/off) Used in Pascal language

Character*
Composed of a single byte of at most one element

String*
Composed of a cluster of characters

* They are enclosed in a pair of single (‘ ’) or double quote(” ”)

Examples
Variables


A value that change from time to time
Portion of the computer memory used to store a numeric or string constant. Can be viewed as a container with a name (label)

Three things to remember about variables:
Variables have names.
Variables have contents. Those contents are called values.
Contents of variables are not permanent. These may change.
Rules in the Formation of Variable Names
Rules:
The first character should be alphabetic.
It may consist of alphabetic characters, numeric characters and underscore (_). It may consist of any number of characters.
There should be no embedded space.
It must be unique and not reserved words of a programming language

Ex. X, ctr, No_of _unit, SUM102, Num_point , Total1

A variable may be one of the following types:
Numeric variable- variables that can contain or hold numeric constant only.

String variable- variables that can contain or hold string constant only.

Arrays- a group of variables in orderly series using a single variable name. Word Types
Reserved words and Programmer-supplied Words are the two word types

Reserved words – words with predefined meaning in a programming language Example: IfUntilCaseWhileElseDo
Programmer-supplied words – user-defined words which function as variable names, filenames, record name, program header, procedure or function names Example: student, first_no, sum, z, x, pos
Operators
Are group into three categories:
Arithmetic
Conditional/relational
Compound/logical
Arithmetic Operators
Conditional/relational Operators
Compound/logical Operators
Expression
It is a group of elements consisting of operands and operators

Evaluating expression should conform with the Precedence Rule (Priority Hierarchy of Operators)

General Format for Expressions:
 Operand Operator Operand…… Expressions always yields a value of a certain, depending on the type of expressions used.

The following are the different types of expressions:

a. Arithmetic Expressions
b. String Expressions
c. Boolean Expressions
Arithmetic expression

Format: Operand Operator Operand……..
Ex.
20 + 8

Mixed Arithmetic Expression
Arithmetic expressions can be mixed to contain several operands and operators.

Ex. 53+12/4*2
(77-50) % 2
15.50*6-100/20
10-7+200/(5+3)*(16%5)

Hierarchy or priority of operators
1. Expressions in parenthesis Highest priority, to be evaluated first.

2. * / % Second highest, to be evaluated next.

3. + -Lowest priority, last to be evaluated.

4. Left to right rule same priority

Left to right rule- states that when the operation involves operators with equal priority, the left most operation is to be evaluated. Constructing Arithmetic Expressions

Example:

2x + xy[(2*x)+(x*y)]/[(3*x)/(x+y)]
3x/(x+y)

Algebraic expression

String expression
It is an expression consists of string values (constant or variable) separated by an operator, which yields a string value + is the only operator for string expression, the effect is called concatenation

Concatenation means appending the value of the second operand to the end of the first operand. It is applicable on string expressions

Example:
“Big” + “5” = “Big5”
Boolean expression
It is an expression of either a relational or logical which yields a value of either true or false

Relational expression – consists of numeric or string values separated by a relational operator Example:
13 3False
False AND False

Hierarchy Rule
notIst (highest)

and2nd

Or3rd (least)

Ex. 400=50-24 and 40025

Result = true

Algorithms and Flowcharts
Chapter 3
Algorithm
A typical programming task can be divided into two phases:
Problem solving phase
Implementation phase

Algorithm is a written series of logical instructions which accomplish solution to a problem.

Pseudo code is an artificial and informal language that helps to develop algorithms which is similar to everyday English Flowchart
Flowchart shows graphical representation of a sequence of steps involved in a program (algorithm)

The two major types of flowchart:
System flowchart – depicts the entire data flow from one program to another throughout a system Program flowchart – depicts the series of operations that the computer follows to generate the desired information

Flowchart Symbols
Basic Control Structure
Used to organize the flow of control in an algorithm
Also called single entry/single exit structure, there are three basic structure:

Sequence – the process is execute from one to another in a straight forward manner Basic Control Structure
Selection – a choice is provided between two alternatives

The condition to be tested
Process/statement to be performed if the condition is satisfied Process/statement to be performed if the condition is satisfied

Basic Control Structure
Iteration/Repetitive or loop control – provides a means of repeating part of an instruction without rewriting that part of an instruction

Body of the loop
Loop-exit condition

Example No. 1
Pseudo code:
Print “Enter your name”
Input a name
Print “Hello” and the name inputted

Algorithm:
Print “Enter your name”
Input Name
Print “Hello”, Name

Example No. 1
Example No. 2
Pseudo code:
Print “The program will add 2 numbers:”
Print “Input the first number:”
Input the first number
Print “Input the second number:”
Input the second number
Calculate the sum of the two numbers
Print “The sum is”, and the sum of the two numbers

Example No. 2
Algorithm:
Print “The program will add 2 numbers:”
Print “Input the first number:”
Input fst
Print “Input the second number:”
Input sec
Sum = fst+sec
Print “The sum is”, Sum
Example No. 2

Example No.3
Create a flowchart that computes the product and the quotient of two numbers. Create a flowchart that convert Inches to centimeter. Wherein,
1 inch = 2.54cm.

Introduction to Turbo C
Chapter 4
History of C
The milestones in C's development as a language:
In 1970, a new language “B” as second attempt developed by Ken Thompson In1971, a totally new language “C”, successor to “B” Dr. Dennis Ritchie of AT&T Bell Laboratories developed C programming language It is a general programming language, and it started to run under Unix environment. By 1973, UNIX OS almost totally written in “C”

About C
Characteristics of C:
Small size
Extensive use of function calls
Loose typing
Structured language
Low level (BitWise) programming readily available
Pointer implementation - extensive use of pointers for memory, array, structures and functions. C has now become a widely used professional language because of the following reasons: It has high-level constructs

It can handle low-level activities
It produces efficient programs
It can be compiled on a variety of computers
About C
Compiler
A C program is made by running a compiler which takes the typed source program and converts it into an object file that the computer can execute. A two-phase compiler works in the following way:
Phase 1 scans a source program, perhaps generating an intermediate code which helps to simplify the grammar of the language for subsequent processing. It then converts the intermediate code into a file of object code (though this is usually not executable yet). Phase 2 is a Linker. This program appends standard library code to the object file so that the code is complete and can "stand alone". About C

C is case sensitive (lower and upper case letters treated differently) Declaration
It is a list of the names and types of all variables which are going to be used in a program and provide information about where they are going to be used. It provides the compiler with a definitive list of the variables, enabling it to cross check for errors; It informs the compiler how much space must be reserved for each variable when the program is run. Reserved Words

C programs are constructed from a set of reserved words which provide control and from libraries which perform special functions. Libraries provide frequently used functionality and, in practice, at least one library must be included in every program: the so-called C library, of standard functions


C Program Structure
A C program basically has the following form:
comments
preprocessor commands
functions
A function has the form:
type function_name (parameters)
{
Local variables 
C Statements 
}
declarations
variables
statements

We must have a main() function
C Structure:

#include

main()
{

getch();
}
Example 1:
Example

#include
main()
{
char name[20]; Declaration
printf(“Enter a name”);
scanf(“%s”, &name);Statements
printf(“Hello! %s”, name);
getch();
}
C Data Types
There are five elementary data types in C:
Type Bandwidth(in bits)Range
character (char)80 to 255
integer (int)16-32768 to 32767
float323.4E-38 to 3.4E+38
double641.7E-308 to 1.7E+308
void0valueless

Three uses of void:
A. To declare explicitly a function as returning no value
B. To declare explicitly a function as having no parameters
C. To create generic pointers

Format Specifiers in C
Commonly used C format specifiers (for I/O statements):
%c – used for single character
%s – used for strings of characters
%i or %d – used for integer
%f – used for float

Constant and Variable
Constant – a value that cannot be changed during program execution Variable – a value that can change during program execution Two kinds of variable:
Local variable – variable which is declared inside a function, it can only be referenced by statements that are inside the block in which the variable is declared Global variable - is known throughout the entire program

Operators
Operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. There are four classes of operators in C:
Arithmetic
Logical
Relational
Bitwise

Arithmetic Operators
Logical Operators
Relational Operators
Evaluation of Expression
Order of Precedence

Highest()
!, unary +,-
*, /, %
binary +,-
, =
==, !=
&&
Lowest||

At the same level of operators: left to right rule
Sample Program
Construct a program that will display the word “Hello”.
ProgramFlowchart

#include
main()
{
printf(“Hello”);
getch();
}

Sample Program
Create a program that will get the sum of two numbers.
Program

#include
main()
{
int fst, sec, Sum;

printf(“The program willadd 2 numbers:”)
printf(“Input the first number:\n”);
scanf(“%d”, &fst);
printf(“Input the secondnumber:\n”);
scanf(“%d”,&sec);
Sum=fst+sec;
printf(“The sum is %d”, Sum);
getch();
}


Comments

  1. Sometimes it's mandatory to submit assignment on programming. So student need programming assignment help from professionals. In this case they help them to prepare programming assignment for them. Some take payment and some do it for their self improvement.

    ReplyDelete

Post a Comment