Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 1-C++

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

Chapter 1

Introduction to Programming
Introduction
What is a computer?
A Computer is an electronic device that accepts data,
performs computations, and makes logical decisions
according to instructions that have been given to it;
then produces meaningful information in a form that is
useful to the user.

Inputs Process Outputs


What is a program?
Computer programs, software programs, or just
programs are the instructions that tells the computer
what to do.

Computer programming (programming or coding)


is the process of writing, testing,
debugging/troubleshooting, and maintaining the source
code of computer programs
Intro
A computer program consists of two elements:
Code – action
Data – characteristics
Computer programs (also know as source code) is often
written by professionals known as Computer
Programmers. Source code is written in one of
programming languages.
Programming language
A programming language is an artificial language that
can be used to control the behavior of a machine,
particularly a computer.
Programming languages, like natural language, are defined
by syntactic and semantic rules which describe their
structure and meaning respectively.
The syntax of a language describes the possible
combinations of symbols that form a syntactically correct
program.
The meaning given to a combination of symbols is handled
by semantics.
Programming language
A main purpose of programming languages is to provide
instructions to a computer.
Available programming languages come in a variety of forms
and types.
Programming languages can be divided in to two major
categories:
Low-level languages
High-level languages
Low-level Languages
Machine language
It is the lowest level programming language in which all
instructions and data are written in a binary machine code, i.e. 0s and
1s.
Computers only understand one language and that is binary
language or the language of 1s and 0s.
For example:
00101010 000000000001 000000000010
10011001 000000000010 000000000011
In the initial years of computer programming, all the instructions
were given in binary form. Although the computer easily understood
these programs, it proved too difficult for a normal human being to
remember all the instructions in the form of 0s and 1s.
Therefore, computers remained mystery to a common person until
other languages such as assembly language was developed, which
were easier to learn and understand.
Low-level language
Assembly language
 correspondences symbolic instructions and executable machine
codes and was created to use letters (called mnemonics) to each
machine language instructions to make it easier to remember or write.
For example:
ADD A, B – adds two numbers in memory location A and B
However, no matter how close assembly language is to machine
code, computers still cannot understand it.
The assembly language must be translated to machine code by a
separate program called assembler.
The machine instruction created by the assembler from the original
program (source code) is called object code. Thus assembly
languages are unique to a specific computer (machine).
High-level Languages
Use naturally understandable languages
IDE’s are used for rapid development like C++, .Net
environment
they permitted a programmer to ignore many low-level details
of the computer's hardware.
closer the syntax, rules, and mnemonics to "natural
language“.
High-level languages are more English-like and, therefore,
make it easier for programmers to "think" in the programming
language.
High-level language
High-level languages also require translation to machine
language before execution.
This translation is accomplished by either a compiler or an
interpreter.
Compilers translate the entire source code program before
execution.
Interpreters translate source code programs one line at a time.
Interpreters are more interactive than compilers.
E.g, FORTRAN (FORmula TRANslator), BASIC (Bingers
All Purpose Symbolic Instruction Code), PASCAL, C, C++,
Java.
Programming paradigms
Procedural Programming Languages
Object-Oriented Programming Languages
Procedural Programming Languages
 is a list of instructions telling a computer, step-by-step, what to do,
usually having a linear order of execution from the first statement
to the second and so forth.
 Procedural programming specifies a list of operations that the
program must complete to reach the desired state.
 Each program has a starting state, a list of operations to complete,
and an ending point. This approach is also known as imperative
programming.
 Procedures, also known as functions, subroutines, or methods, are
small sections of code that perform a particular function.
 A procedure is effectively a list of computations to be carried out.
 By splitting the programmatic tasks into small pieces, procedural
programming allows a section of code to be re-used in the program
without making multiple copies.
 E.g. FORTRAN and BASIC.
Object-Oriented Programming Languages
Object-oriented programming is one the newest and most
powerful paradigms.
Object-oriented programming is a programming paradigms
based on the concept of objects, which are data structures
that contains data in the form of fields, known as attributes
and code in the form of procedures known as method.
This pairing of a piece of data with the operations that can be
performed on it is known as an object.
A program thus becomes a collection of cooperating objects,
rather than a list of instructions.
Objects can store state information and interact with other
objects, but generally each object has a distinct, limited role.
Problem Solving Techniques
Computer solves varieties of problems that can be
expressed in a finite number of steps leading to a precisely
defined goal by writing different programs. A program is
not needed only to solve a problem but also it should be
reliable, (maintainable) portable and efficient.
In computer programming two facts are given more weight:
The first part focuses on defining the problem and logical
procedures to follow in solving it.
The second introduces the means by which programmers
communicate those procedures to the computer system so that
it can be executed.
Cont’d
There are system analysis and design tools, particularly
flowchart and structure chart, that can be used to define the
problem in terms of the steps to its solution.
The programmer uses programming language to
communicate the logic of the solution to the computer.
Before a program is written, the programmer must clearly
understand what data are to be used, the desired result, and
the procedure to be used to produce the result.
The procedure, or solution, selected is referred to as an
algorithm.
An algorithm is defined as a step-by-step sequence of
instructions that must terminate and describe how the data is
to be processed to produce the desired outputs.
Algorithms
Simply, algorithm is a sequence of instructions.
Algorithms are a fundamental part of computing.
An algorithm must satisfy the following requirements:
Unambiguousness: i.e. it must not be ambiguous. Every step in an
algorithm must be clear as to what it is supposed to do and how many
times it is expected to be executed.
Generality: i.e. it should have to be general, not to be specific.
Correctness: it must be correct and must solve the problem for which it
is designed.
Finiteness: it must execute its steps and terminate in finite time.
 Algorithms are usually represented in pseudo code, Structured chart or by a
flow chart.
Pseudocodes
 is English like language for representing the solution to a problem.
 Pseudo code is independent of any programming language. Pseudo
code (or a flow chart) is the first step in the process of planning the
solution to a problem (also called developing an algorithm).
 Pseudo code is a compact and informal high-level description of a
computer algorithm that uses the structural conventions of
programming languages, but typically omits details such as
subroutines, variables declarations and system-specific syntax.
 The purpose of using pseudocode is that it may be easier for
humans to read than conventional programming languages
 No standard for pseudocode syntax exists, as a program in
pseudocode is not an executable program.
Pseudocode
 As the name suggests, pseudocode generally does not actually
obey the synatx rules of any particular language; there is no
systematic standard form, although any particular writer will
generally borrow the appearance of a particular language.
 The programming process is a complicated one. You must first
understand the program specifications, of course, Then you need
to organize your thoughts and create the program.
 This is a difficult task when the program is not trivial (i.e. easy).
 You must break the main tasks that must be accomplished into
smaller ones in order to be able to eventually write fully developed
code.
 Writing pseudocode will save you time later during the
construction & testing phase of a program's development.
Example:
Original Program Specification:   
Write a program that obtains two integer numbers from the user.
It will print out the sum of those numbers.
Pseudo code:
Prompt the user to enter the first integer
Prompt the user to enter a second integer
Compute the sum of the two user inputs
Display an output prompt that explains the answer as the sum
Display the result

• If student's grade is greater than or equal to 60


Print "passed"
else
Print "failed"
Flowcharts
A flowchart is a graphical way of representing the solution
to a problem
The advantage of flowchart is it doesn’t depend on any
particular programming language, so that it can used, to
translate an algorithm to more than one programming
language. 
Flowchart uses different symbols (geometrical shapes) to
represent different processes.
Example 1:
Draw flow chart of an algorithm to add two numbers and
display their result.
Algorithm description
Read the rules of the two numbers (A and B)
Add A and B
Assign the sum of A and B to C
Display the result ( c)
Example 2
Write an pseudo code and draw a flow chart to check a
number is negative or not.
 Algorithm description.
1. Start
2. Read a number x
3. If x is less than zero then
Write “Negative”
Else
Write “Not Negative”
4. stop
Example 3
 Write the algorithm description (Pseudo code) and Draw flow
chart of an algorithm to find the following sum.
Sum = 1+2+3+…. + 50
 Algorithm description (Pseudo code)

1. Start
2. Initialize Sum to 0 and Counter to 1
2.1 If the Counter is less than or equal to 50  
• Add Counter to Sum
• Increase Counter by 1
• Repeat step 2.1
2.2 Else
• Exit Loop
3. Write Sum
4. Stop
Exercise:
1. Write an algorithm description and draw a flow chart to
check a number is even or odd.

You might also like