0% found this document useful (0 votes)
4 views

Chapter One Class2015

This document provides an introduction to programming, explaining the basics of computers, programming languages, and the importance of programming skills. It covers different programming paradigms, problem-solving techniques, and the software development life cycle, along with algorithm design and flowcharting. Additionally, it discusses the roles of compilers and interpreters in program execution, specifically in the context of C++ programming.

Uploaded by

ephremmulu486
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter One Class2015

This document provides an introduction to programming, explaining the basics of computers, programming languages, and the importance of programming skills. It covers different programming paradigms, problem-solving techniques, and the software development life cycle, along with algorithm design and flowcharting. Additionally, it discusses the roles of compilers and interpreters in program execution, specifically in the context of C++ programming.

Uploaded by

ephremmulu486
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CHAPTER ONE

INTRODUCTION TO PROGRAMMING

General Introduction to Computer and Programming


Computer is a machine that needs instruction to perform a variety of tasks. A computer
programs, software programs, or just programs or source code are a set of instructions that
tells the computer what to do. Computer requires programs to perform some task, and a
computer programs does nothing unless its instructions are executed by a CPU. Computer
programming or programming or coding) is the process of writing, testing,
debugging/troubleshooting, and maintaining the source code of computer programs.
Programming is a skill that can be acquired by a computer professional that gives him/her
the knowledge of making the computer perform the required operation or task.
Why do we need to Learn Computer Programming?
Computer programming is critical if one wants to know how to make the computer
perform a task. Most users of a computer only use the available applications on the
computer. Computer programs is often written by professionals’ person known as
computer Programmers (simply programmers).
What skills do we need to be a programmer?
Programming Language Skill: knowing one or more programming language to talk to
the computer and instruct the machine to perform a task. Since, source code is written in
one of programming languages.
Problem Solving Skill: skills on how to solve real world problem and represent the
solution in understandable format.
Algorithm Development Skill: skill of coming up with sequence of simple and human
understandable set of instructions showing the step of solving the problem. Those set of
steps should not be dependent on any programming language or machine.
What is programming language?
A programming language is an artificial language that can be used to control the behavior
of a machine, particularly a computer. Computer programing language, language which
enable to write computer programs (C, C#, C++, java, VB…). It’s like natural language (such

1
as English), that are defined by Syntax 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. 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 and
high-level languages.
Low-Level Languages
Computers only understand one language and that is binary language or the language in
the form of string of 0s and 1s. Binary language is also known as machine language. Machine
language referred to as first generation language (one of low-level languages). Computer
programming, all the instructions were given in binary form which computer easily
understood.
Assembly language: These languages use symbols or mnemonic codes to describe
machine instructions, which is easier to learn and understand. Assembly language referred
to as second generation language. ADD A, B – adds two numbers in memory location A and
B. 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
(Assembly language source code) is called object code.
High-level languages
Although programming in assembly language is not as difficult and error prone as stringing
together ones and zeros, but lack of portability between different computers, this led to the
development of high-level languages which permitted a programmer to ignore many low-
level details of the computer's hardware. High-level languages that look like natural
language which more English-like. 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 written in high level
languages before execution. Interpreters translate source code programs one line at a time,
before going onto the next instruction. FORTRAN, BASIC, PASCAL, C, C++, Java are some
examples of high-level languages.

2
Major Programming Paradigms
Most fundamental ways programming languages are characterized (categorized) is by
programming paradigm. A programming paradigm provides the programmer's view of
code execution. Three basic programing paradigms are:
 Procedural Programming
 Structured Programming
 Object-Oriented Programming

Procedural Programming Languages


 Procedural programming specifies a list of operations that the program must
complete to reach the desired state.
 Its programming paradigm based upon the concept of procedure call.
 Procedures, also known as functions, subroutines, or methods, are small sections
of code that perform a particular function. In procedural programming, all of the
code resides in a single large block.
 Two of the most popular procedural programming languages are FORTRAN and
BASIC.
 Possible benefits: the ability to re-use the same code (function or procedure) at
different places, an easier way to keep track of program flow than a collection of
“GO TO” or “JUMP” statements.
Structured Programming Languages
 Process of writing a program in small, independent parts. This makes it easier to
control a program's development and to design and test its individual component
parts. Uses local variables to each subroutine.
 Structured programs are built up from units called modules, which normally
correspond to single procedures or functions and that are easily understood.
 Can be seen as a subset or sub discipline of procedural programming. It is most
famous for removing or reducing reliance on the GO TO statement.
 It is often associated with a "top-down" approach to design. Example: C, Ada,
PASCAL

3
Object-Oriented Programming
 Object-oriented programming is one of the newest and most powerful paradigms.
 The idea behind OOP is that, a computer program is a collection of cooperating
objects, rather than a list of instructions.
 Object-oriented programming is claimed to give more flexibility, easing changes to
programs.
 The OOP approach is often simpler to develop and maintain. Example: Java

Problem Solving Techniques


Step 1: Identify the Problem.
Step 2: Generate potential solutions.
Step 3: Choose one solution.
Step 4: Implement the solution you've chosen.
Step 5: Evaluate results.
Problem Solving Process and Software Engineering.
Software Engineering.
Software engineering is the profession that creates and maintains software applications
by applying technologies and practices from computer science, project management,
engineering, application domain and other fields. The method used in solving problems
in computer science and/or information systems is called the software development life
cycle. The software development life cycle has the following components.
 Preliminary investigation  Analysis  Design Implementation Testing
and Maintenance
Problem Solving
Problem solving is the process of transforming the description of a problem into the
solution by using our knowledge of the problem domain and by relying on our
ability to select and use appropriate problem-solving strategies, techniques, and tools.
A problem is an undesirable situation that prevents the organization from fully achieving
its purpose, goals and objectives. Or problem can also be defined as the gap between the
existing and the desired situation where problem solving will try to fill this gap.

4
There are two Approaches of Problem Solving:
Top down design: is a systematic approach based on the concept that the structure
of the problem should determine the structure of the solution and what should be
done in lower level. This approach will try to split (disintegrate) a larger problem into
more smaller and manageable problems to narrow the problem domain.
Bottom up design: is the reverse process where the lowest level component are built
first and the system builds up from the bottom until the whole process is finally
completed.
Basic Program Development tips
The program we design in any programming language need to be:
Reliable: the program should always do what it is expected to do and handle all types
of exception.
Maintainable: the program should be in a way that it could be modified and
upgraded when the need arises.
Portable: It needs to be possible to adapt the software written for one type of
computer to another with minimum modification.
Efficient: the program should be designed to make optimal use of time, space and
other resources of the computer.
Algorithm Designing and Modeling the Logic using Flow chart
A digital computer is a useful tool for solving a great variety of problems. Writing a
logical step-by-step method to solve the problem is called the algorithm. A solution to a
problem is called an algorithm, it describes the sequence of steps to be performed for the
problem to be solved. Generally, an algorithm is a finite set of well-defined instructions
for accomplishing some task which, given an initial state, will terminate in a
corresponding recognizable end-state. The algorithm should be: Precise and
unambiguous, Simple, Correct and Efficient.

5
Modeling a Programs Logic using Flow Chart
Algorithm could be designed using many techniques and tools. One tool of designing
algorithm is by using flowcharts. Flowchart is a graphical way of expressing the steps
needed to solve a problem. A flow chart is a schematic (diagrammatic description)
representation of a process.
Basic flowcharting Symbols are:
A flowchart is the graphical or pictorial representation of an algorithm with the help of
different symbols, shapes, and arrows to demonstrate a process or a program. With
algorithms, we can easily understand a program. The main purpose of using a flowchart is
to analyze different methods. Several standard symbols are applied in a flowchart:

Difference between Algorithm and Flowchart


Algorithm Flowchart
 It is a procedure for solving problems.  It is a graphic representation of a process.
 The process is shown in step-by-step instruction.  The process is shown in block-by-block
 It is complex and difficult to understand. information diagram.
 It is convenient to debug errors.  It is intuitive and easy to understand.
 The solution is showcased in natural language.  It is hard to debug errors.
 It is somewhat easier to solve complex problem.  The solution is showcased in pictorial
 It costs more time to create an algorithm. format.
 It is hard to solve complex problem.
 It costs less time to create a flowchart.
6
Example: Algorithm Flowchart

Step 1: Initialize X as 0
Step 2: Increment X by 1
Step 3: Print X
Step 4: If X is less than 20
then go back to step 2

Example 2: Draw a flow chart to check if a


given number is positive or negative.

Flowchart

Algorithm Example 3: - Draw flow


chart of an algorithm to add two
numbers and display their result/Sum
 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)

7
Compiler and Interpreter
Any program written in a language other than machine language needs to be
translated to machine language. The set of instructions that do this task are known as
translators. There are different kinds of translator software among which compiler
and interpreters are of interest for most programmers’.
Compilers: a compiler is a computer program that translates a serious of statements
written in source code (a collection of statements in a specific programming language)
into a resulting object code (translated instructions of the statements in a programming
language). A compiler changes or translates the whole source code into executable machine
code (also called object code) which is output to a file for latter execution. E.g. C++, Pascal,
FORTRAN, etc.
Interpreters: is a computer program that translates a single high level statement
and executes it and then goes to the next high level language line etc. E.g. QBASIC, Lisp
etc.
C++ Program Execution Phases
C++ programs (source code) typically go through five phases to be executed these are edit,
preprocess, compile, link, and load:
Edit: this is accomplished with an editor program the programmer types C++
statements with the editor and makes corrections if necessary. The programs source
file is then stored on secondary storage device such as a disk with a “.cpp” file name. After
the program is edited, C++ is principally compiled in three phases. Preprocessing,
translation to object code, and linking (the last two phases are what is generally thought of
as the "compilation" process).
Preprocess: In a C++ system, a preprocessor program executes automatically before
the compiler’s translation phase begins. The C++ preprocessor obeys command called
preprocessor directives, which indicate that certain manipulations are to be performed
on the program before compilation. The preprocessor is invoked by the compiler before
the program is converted to machine language. The C++ preprocessor goes over the
program text and carries out the instructions specified by the preprocessor directives

8
(e.g., #include). The result is a modified program text which no longer contains any
directives.
Compile: Then, the C++ compiler translates the program code. The compiler may be a
true C++ compiler which generates native (assembly or machine) code. The outcome may
be incomplete due to the program referring to library routines which are not defined as
a part of the program. For example, the << operator which is actually defined in a
separate IO library.
Linking: C++ programs typically contain reference to function and data defined elsewhere,
such as in the standard libraries. The object code produced by the C++ compiler typically
contains “holes” due to these missing parts. A linker links the object code with the code
for the missing function to produce an executable image (with no missing pieces).
Generally, the linker completes the object code by linking it with the object code of any
library modules that the program may have referred to. The final result is an executable file
Loading: the loader takes the executable file from disk and transfers it into memory.
Additional components from shared libraries that support the program are also loaded.
Finally, the computer, under the control of its CPU, executes the program.

Example: Write the algorithmic description and draw a flow chart to find the following sum.

Algorithmic Solution
#Sum = 1+2+3+…. + 50
Algorithmic description
1. Initialize sum to 0 and counter to 1 Sum = 0
1.1 If the counter is less than or equal to 50 Counter =1
Add counter to sum
Increase counter by 1
Repeat step 1 .1
1.2 else
Exit
2. Write sum 9

You might also like