0% found this document useful (0 votes)
18 views56 pages

Chapter One - Introduction - All Parts

C++ or computer Programming chapter 1

Uploaded by

neguyared837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views56 pages

Chapter One - Introduction - All Parts

C++ or computer Programming chapter 1

Uploaded by

neguyared837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

{

Start course;

Computer Programming (CoSc1012);


{

Start chapter;
{
Chapter One→ Introduction;

June 7, 2024 By Kibru G. 1


Introduction to Programming
▪ Computer ?
▪ 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.
▪ Computer programs ?
▪ Sets of instructions that control a computer’s processing of data
▪ the instructions that tells the computer what to do
▪ Computer programming ?
▪ is the process of writing, testing, debugging / troubleshooting, and
maintaining the source code of computer programs.
June 7, 2024 By Kibru G. 2
Introduction to Programming

▪ Computer programs (also know as source code) is often


written by professionals known as Computer Programmers

▪ A computer program usually consists of two elements:


▪ Data – characteristics-what is being manipulated.
▪ Code – actions on the data- the operations the program does.

June 7, 2024 By Kibru G. 3


Introduction to Programming

▪Computer programs are Sequences of instructions


expressed in specific programming language:
▪ as a result, programs cannot be ambiguous
▪ all instructions together are called source code
▪ Executed by computer by carrying out or following
individual instructions
June 7, 2024 By Kibru G. 4
Introduction to Programming
▪ Source code is written in a programming languages.
▪ What is Programming Language?
▪ Programming Language – is an artificial language with a set of
rules, symbols, and special words used to construct a computer
program. It is used for controlling the behavior of a computer.
▪ Is defined by:
▪ Syntactic - describes the possible combinations of symbols that
form a syntactically correct program
▪ Semantic - The meaning given to a combination of symbols
▪ computers do exactly what they are told to do

June 7, 2024 By Kibru G. 5


Types and usage of computer languages
▪ There are many languages for writing programs. But they can be categories broadly into
three categories.
1. Machine language(binary language)
▪ The most elementary and first type of computer language invented, was machine
language.
▪ Machine language is a collection of binary digits or bits that the computer reads and
interprets.
▪ Machine language was machine dependent means different computers understand
different sequences.
▪ It is computer’s native language, sequence of zeroes and ones (binary)
▪ Machine languages are the only languages understood by computers.
▪ Might be hard for humans to understand: 01010001

June 7, 2024 By Kibru G. 6


Cont.
2. Assembly language

▪ A program written in assembly language consists of a series of instructions mnemonics that


correspond to the executable instructions.

▪ Assembly language instructions and their translation into zeros and ones differ from machine
to machine.

▪ machine language programming was simply too slow tedious for most programmers.

▪ still hard for humans to understand on complex program: example ADD X Y Z

▪ Any Assembly language program must be translated into machine language (by a program
called an assembler) before the computer can understand and execute the program.

June 7, 2024 By Kibru G. 7


Cont.
3. High-level languages
▪ They are designed to be easy for human beings to write programs in and to be easy for human
beings to read.
▪ High level languages are the computer language in which it is much easier to write a program than
the low level language.
▪ A program written in high level language is just like giving instruction to person in daily life
▪ closer to natural language such as English easier to read and understand:
▪ Low-level languages are closer to the language used by a computer, while high-level languages are
close to human languages.
▪ FORTRAN, Pascal, BASIC, C, C++, Java, etc.

June 7, 2024 By Kibru G. 8


Interpreting vs Compiling program
▪ Any high-level language program must be translated into machine language
before the computer can understand and follow the program.
▪ Each type of computer only “understands” its own machine language (zeroes
and ones)
▪ Compiler: is to transform a program written in a high level programming
language from source code into object code.
▪ Interpreter: is a program that executes instructions written in a high-level
language. They do the same thing but interpreter works each statement at a
time.
▪ https://www.geeksforgeeks.org/difference-between-compiler-and-interpreter

June 7, 2024 By Kibru G. 9


What is Compiler?
▪ A compiler is a computer program that transforms code written in a high-level
programming language into the machine code.
▪ It is a program which translates the human-readable code to a language a
computer processor understands (binary 1 and 0 bits).
▪ The computer processes the machine code to perform the corresponding tasks.
▪ A compiler should comply with the syntax rule of that programming language
in which it is written.
▪ However, the compiler is only a program and can not fix errors found in that
program.
▪ So, if you make a mistake, you need to make changes in the syntax of your
program. Otherwise, it won’t compile.

June 7, 2024 By Kibru G. 10


Compiler compiling phases
▪ It is used by the compiler to achieve compile-time efficiency.
▪ It is used by various phases of the compiler as follows:-
▪Lexical Analysis: Creates new table entries in the table, for example like entries about tokens.
▪Syntax Analysis: Adds information regarding attribute type, scope, dimension, line of
reference, use, etc. in the table.
▪Semantic Analysis: Uses available information in the table to check for semantics i.e. to verify
that expressions and assignments are semantically correct(type checking) and update it
accordingly.
▪Intermediate Code generation: Refers symbol table for knowing how much and what type of
run-time is allocated and table helps in adding temporary variable information.
▪Code Optimization: Uses information present in the symbol table for machine-dependent
optimization.
▪Target Code generation: Generates code by using address information of identifier present
in the table.

June 7, 2024 By Kibru G. 11


How compiler works?

June 7, 2024 By Kibru G. 12


Basic Compiling and linking process

June 7, 2024 By Kibru G. 13


What is Interpreter?
▪ An interpreter is a computer program, which converts each high-level program
statement into the machine code.

▪ This includes source code, pre-compiled code, and scripts.

▪ Both compiler and interpreters do the same job which is converting higher level
programming language to machine code.

▪ However, a compiler will convert the code into machine code (create an exe) before
program run.

▪ Interpreters convert code into machine code when the program is run.

June 7, 2024 By Kibru G. 14


Cont.

June 7, 2024 By Kibru G. 15


Cont.
▪ Compiler transforms code written in a high-level programming language into
the machine code, at once, before program runs,
▪ whereas an Interpreter converts each high-level program statement, one by one,
into the machine code, during program run.
▪ Which one is faster, compiler or interpreter? Reading assignment
▪ Compiler displays all errors after compilation, on the other hand, the Interpreter
displays errors of each line one by one.
▪ Compiler is based on translation linking-loading model, whereas Interpreter is
based on Interpretation Method.
▪ Compiler takes an entire program whereas the Interpreter takes a single line of
code

June 7, 2024 By Kibru G. 16


Problem Solving Techniques

June 7, 2024 By Kibru G. 17


Programming paradigm
▪ A computer program usually consists of two elements:
▪ Data – characteristics
▪ Code – action
▪ Some program development approaches conceptually organize the program around the code:
▪ Around what is happening
▪ known as Process oriented approach/ method, which characterizes the program as serious
linear steps.
▪ Code acting on data
▪ Other programming languages conceptually organize the program around data:
▪ Who is being affected
▪ designed to manage increasing complexity.
▪ Ask not what your code does to your data structure, but what your data structure can do for
you

June 7, 2024 By Kibru G. 18


Programming paradigm
▪ It is a fundamental style of computer programming, a way of building the structure
and elements of computer programs.

▪ Capabilities and styles of various programming languages are defined by their


supported programming paradigms;

▪ Some programming languages are designed to follow only one paradigm, while others
support multiple paradigms

▪ Programming paradigms that are often distinguished include declarative, Procedural


and object-oriented programming

June 7, 2024 By Kibru G. 19


Procedural (imperative) programming
▪ Based upon the concept of procedure call and top down approach. A procedure call is
used to invoke the procedure

▪ Procedures (routines, subroutines, methods, functions) simply contain a series of


computational steps to be carried out to solve a problem.

▪ Any given procedure might be called at any point during a program's execution,
including by other procedures or itself.

▪ Procedural programming is a list or set of instructions telling a computer what


to do step by step and how to perform from the first code to the second code.
June 7, 2024 By Kibru G. 20
Cont.
We have a single program, which is divided into small pieces called procedures

Procedural programming languages include C++, FORTRAN, Pascal, and BASIC.

June 7, 2024 By Kibru G. 21


Declarative (Functional) Programming
▪ Is when you write your code such a way that is describes what you want to do not how
you want to do it. It needs to perform logic.

▪ Program describes logic rather than control flow

▪ Program describes “what” rather than “how”.

▪ Aims for correspondence with mathematical logic

▪ Automatic reasoning by applying inference rules

▪ SQL , Markup languages, Lisp, Prolog.

▪ Functional programming is becoming popular paradigm now days.

June 7, 2024 By Kibru G. 22


Object Oriented Programming
▪ Is a method of implementation in which programs are organized as cooperative
collections of objects.
▪ Data and operations are grouped together
▪ Each object is capable of receiving messages, processing data, and sending
messages to other objects
▪ Modeling of the domain as objects so that the implementation naturally reflects
the problem at hand.
▪ Examples: C++,Java…C#..

June 7, 2024 By Kibru G. 23


Cont.

June 7, 2024 By Kibru G. 24


Problem Solving Techniques
▪ A computer is not intelligent. It cannot analyze a problem and come up with a
solution.
▪ Instead, a human (the programmer) must analyze the problem, develop
instructions for solving the problem, and then communicate it to the computer.
▪ Once we have written the solution for the computer, it can repeat the solution very
quickly and consistently.
▪ The computer frees people from repetitive and boring tasks.
▪ To write instructions for a computer to follow, we must go through a two-phase
process: problem solving and implementation.

June 7, 2024 By Kibru G. 25


Problem Solving Techniques

June 7, 2024 By Kibru G. 26


Problem Solving Techniques
1. Problem-Solving Phase:
▪ Analysis and specification: Understand (define) the problem and what the solution must do.
▪ General solution (algorithm): Develop instructions for solving the problem, using a
combination of a human language, such as English, and mathematical notation.
▪ Verify: Follow the instructions exactly to see if the solution really does solve the problem.
2. Implementation Phase:
▪ Concrete solution (program): Translate the algorithm into a programming language.
▪ Test: Have the computer follow the instructions, then manually check the results. If you find
errors, analyze the program and the algorithm to determine the source of the errors, and then
make corrections.
3. Once a program has been written, it enters a third phase: maintenance.

June 7, 2024 By Kibru G. 27


Problem Solving Techniques
▪ In a computer program you tell a computer, step by step, exactly what you want
it to do. A computer program is written in order to solve a problem.
▪ A solution to a problem actually consists of two things:
▪ A way to organize the data
▪ Sequence of steps to solve the problem
▪ The way data are organized in a computer’s memory is said to be Data
Structure
▪ The sequence of computational steps to solve a problem is said to be an
algorithm
June 7, 2024 By Kibru G. 28
Algorithm
▪ The programmer begins the programming process by analyzing the problem and developing a
general solution called an algorithm. These steps are the heart of the programming process

▪ 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.

▪ Simply, algorithm is a sequence of instructions

▪ A procedure or formula for solving a problems in finite amount of time.

▪ Often used for calculation, data processing and programming.

▪ Algorithms can be expressed in any language or programming language independent. An


algorithm is a verbal or written description of the actions that must be taken to solve a problem.
June 7, 2024 By Kibru G. 29
Algorithm
▪ It’s tempting to try to go directly from the problem definition to coding the program. However,
this kind of shortcut actually takes more time and effort. Developing an algorithm before you
start coding helps you manage the problem, keep your thoughts straight, and avoid mistakes. If
you don’t think out your algorithm first, you’ll spend a lot of extra time debugging and revising
your program.

June 7, 2024 By Kibru G. 30


Algorithm
▪ We use algorithms every day. Recipes, instructions, and directions are all examples of algorithms that are
not programs. When you start your car, you follow a step-by-step procedure. The algorithm might look
something like this:
1. Depress the brake pedal.
2. Make sure the transmission is in Park.
3. Press the start button.
4. If the engine doesn’t start, wait five seconds and repeat Steps 1 through 3, but not more than five
times.
5. If the car doesn’t start, call the repair shop.
▪ Without the phrase “but not more than five times” in Step 4, you could be trying to start the car forever.
Why? Because if something is wrong with the car, repeating Steps 1 through 3 will not start it. This never-
ending situation is called an infinite loop. If we leave the phrase “but not more than five times” out of Step
4, we do not have an algorithm.
▪ An algorithm must terminate in a finite amount of time for all possible conditions.

June 7, 2024 By Kibru G. 31


Properties of Algorithms
▪ Finiteness: Algorithm must complete after a finite number of steps.

▪ Definiteness: Each step must be clearly defined, having one and only one
interpretation. At each point in computation, one should be able to tell exactly what
happens next.

▪ Sequence: Each step must have a unique defined preceding and succeeding step. The
first step (start step) and last step (halt step) must be clearly noted.

▪ Feasibility: It must be possible to perform each instruction.

▪ Correctness: It must compute correct answer for all possible legal inputs.
June 7, 2024 By Kibru G. 32
Cont.
▪ Completeness: It must solve the problem completely.

▪ Effectiveness: It must be possible to perform each step exactly and in a finite


amount of time.

▪ Efficiency: It must solve with the least amount of computational resources such
as time and space.

▪ Generality: Algorithm should be valid on all possible inputs.

▪ Input/Output: There must be a specified number of input values, and one or


more result values.
June 7, 2024 By Kibru G. 33
Cont.
▪ Algorithms can be expressed in many different notations, including natural
languages, pseudo code, flowcharts and/or programming languages.

▪ Pseudo code is an informal high-level description of the operating principle


of a computer program or other algorithms. It is not an executable program.

▪ A flowchart is a type of diagram that represents an algorithm, workflow or


process, showing the steps as boxes of various kinds, and their order by
connecting them with arrows.

June 7, 2024 By Kibru G. 34


Cont.

▪ The algorithm and flowchart include following three types of control


structures.

▪ or programs could be written in terms of 3 structures:


▪ Sequence – which instruction should be done next?
▪ Selection – select between options
▪ Repetition – repeat an action while a given condition stays true

June 7, 2024 By Kibru G. 35


Flowchart
▪ Flow chart is diagrammatic representation of an algorithm.

▪ A flow chart is a type of diagram that represents a workflow on a process

▪ Step by step approach to solve a task by using some standard symbols

▪ The flowchart is a diagram which visually presents the flow of data through processing
system.

▪ This means by seeing a flow chart one can know the operations performed and
the sequence of these operations in a system.
▪ Flow chart is a blueprint of a design you have made for solving a problem.

June 7, 2024 By Kibru G. 36


Flowchart Symbols

June 7, 2024 By Kibru G. 37


General Rules of flowcharts
1. All boxes of the flowchart are connected with Arrows. (Not lines)
2. Flowchart symbols have an entry point on the top of the symbol with no other
entry points.
3. The exit point for all flowchart symbols is on the bottom: The Decision symbol has
two exit points.
4. Generally a flowchart will flow from top to bottom.
5. Connectors are used to connect breaks in the flowchart.
Examples are:
▪ From one page to another page.
▪ From the bottom of the page to the top of the same page.

June 7, 2024 By Kibru G. 38


Flowchart: Decision and Iteration Structure

June 7, 2024 By Kibru G. 39


Working on algorithms and flowcharts
Write an algorithm that finds( calculate) the sum of two numbers and
display the result.
Algorithm
Step 1: Start
Step 2: Read A, and B
Step 3: C=A+B
Step 4: Write C
Step 5: Stop

June 7, 2024 By Kibru G. 40


Working on algorithms and flowcharts
Write an algorithm and flowchart to find area of the circle.
Algorithm
Step1: Begin
Step2: Read r
Step3:Area=PI*r*r
Step4: Write Area
Step5:End

June 7, 2024 By Kibru G. 41


Working on algorithms and flowcharts
Write an algorithm to determine a student’ s Algorithm description
final grade and indicate whether it is passing or
Step1. Start
failing. The final grade is calculated as the
average of four marks. If average is greater than Step2. Read four marks (mark1, mark2, mark3, mark4) from the
50, the student passes else fails. user.
Algorithm Step3. Calculate the sum of four marks and store the result in
Step1: Start variable called “Sum”
Step2: Input M1,M2,M3,M4 Step4. Calculate the average of the four marks by dividing by the
Step3: Sum=M1+M2+M3+M4
“Sum” by 4. Store the result in a variable called “Grade".
Step4: Grade= Sum/4 Step5. If the “Grade” is greater than or equal to a passing
threshold (e.g., 50), go to step 6. Otherwise, go to step 7.
Step5: If (Grade<50) Then
Step6: Print “Fail” Step6. Display "Pass" along with the student's final grade
(average).
else
Step7: Print “pass” Step7. Display "Fail" along with the student's final grade (average).
Step8: END Step8.Stop.
June 7, 2024 By Kibru G. 42
Working on algorithms and flowcharts
Draw a flowchart and determine the algorithm to find the largest of
Three Numbers.

June 7, 2024 By Kibru G. 43


Working on algorithms and flowcharts
Determine the algorithm and flow chart that sums N numbers.
Algorithm
Step1: initialize sum=0 , counter=0
Step2:Read N
Step3. Read num
Step4:sum=sum+num; counter=counter+1
Step5: is (counter<N)
if yes
goto step3
else
print sum
Step6: End

June 7, 2024 By Kibru G. 44


Working on algorithms and flowcharts
▪ Multiple Selection (switch)
▪ If the light is ... Light True
Stop
Red?
red -> stop
False
green -> go True
Light
yellow -> slow down Green? Go

False True
Light Slow
Yellow? Down
False

45
Pseudocode
▪ Is an artificial and informal language that helps programmers develop
algorithms.

▪ A shorthand notation for programming which uses a combination of


informal programming structures and verbal descriptions of code.

▪ In general, pseudocode is used to outline a program before translating it


into proper syntax.

▪ Pseudocode is very similar to everyday English.

June 7, 2024 By Kibru G. 46


Pseudocode
▪ Pseudocode (which means fake code, because its not really programming code) specifies the steps
required to accomplish the task.

▪ Pseudocode is a type of structured English that is used to specify an algorithm.

Advantages of Pseudocode

▪ Reduced complexity.

▪ Increased flexibility.

▪ Ease of understanding.

▪ The difference between algorithm and pseudocode is that an algorithm is a step-by-step procedure
developed to solve a problem, while a pseudocode is a technique of developing an algorithm.
June 7, 2024 By Kibru G. 47
How to Write Pseudocode Statements?
There are six basic computer operations:

1. A computer can receive information: When a computer is required to receive information or input from a particular
source, whether it be a terminal, a disk or any other device, the verbs Read, Input and Get are used in Pseudocode. Read
is usually used when the algorithm is to receive input from a record on a file, while Get, Input are used when the
algorithm is used to receive input from the keyboard. For example, typical Pseudocode instructions to receive
information are:
Read student name
Get system date
Read number-1, number-2
Get tax-code
Input marks
Each example uses a single verb, Read or Get followed by one or more nouns to indicate what
data is to be obtained. At no stage is necessary to specify the source of the data, as this
information is not required until run time.

June 7, 2024 By Kibru G. 48


How to Write Pseudocode Statements?
There are six basic computer operations:
2. A computer can put out information: When a computer is required to supply
information or output to a device, the verbs Print, Write, Put, Output or Display are used
in Pseudocode. Print is usually used when the output is to be sent to the printer, while
write is used when the output is to be written to a file. If the output is written to the
screen, the words put, output or display are used in Pseudocode. Typical Pseudocode
examples are -
Print 'Program completed'
Write customer record to master file
Put out name, address and postcode
Output total-tax
Display 'End of data'
In each example, the data to be output is described concisely using mostly lower-case letters.

June 7, 2024 By Kibru G. 49


How to Write Pseudocode Statements?
There are six basic computer operations:
3. A computer can perform arithmetic: Most programs require the computer to perform some sort of
mathematical calculation or formula and for these a programmer may use either actual mathematical
symbols or the words for those symbols. With high-level programming languages, the following
symbols can be written in Pseudocode :
+ Add
- Subtract
* Multiply
/ Divide
() Parentheses
The verbs Compute and Calculate are also available. Some Pseudocode examples to perform a
calculation are:
Divide total – marks by student-count
Sales – tax = cost – price * 0.10
Compute C = (F - 32) * 5/9

June 7, 2024 By Kibru G. 50


How to Write Pseudocode Statements?
There are six basic computer operations:
4. A computer can assign a value to a variable or memory location: There are three
cases where you may write Pseudocode to assign a value to a variable or memory
location:
1. To give data an initial value in Pseudocode, the verbs Initialize or Set are used.
2. To assign a value as a result of some processing, the symbol ‘=’ is written
3. To keep a piece of information for later use, the verbs Save or Store is used
Some typical Pseudocode examples are:
Initialize total accumulators to zero
Set student - count to 0
Total - price = cost - price + soles - tax
Store customer - num in lost - customer - num

June 7, 2024 By Kibru G. 51


How to Write Pseudocode Statements?
There are six basic computer operations:
5. A computer can compare two variables and select one of two alternative actions: An
important computer operation available to programmer is the ability to compare two variables and
then, as a result of the comparison, select one of two alternative actions. To represent this
operation in Pseudocode, special keywords are used: IF, THEN and ELSE. The comparison of data
is established in the If clause, and the choice of alternatives is determined by the Then or Else
options. Only one of these alternatives will be performed. A typical Pseudocode example to
illustrate this operation is:
IF student is part-time THEN
add 1 to part-time count
ELSE
add 1 to full-time count
ENDIF
June 7, 2024 By Kibru G. 52
How to Write Pseudocode Statements?
There are six basic computer operations:

6. A computer can repeat a group of actions: When there is a sequence of processing tips, which
need to be repeated, two special keywords, DO WHILE and END DO are used in Pseudocode. The
condition for the repetition of a group of actions is established in the Do While clause, and the
actions to be repeated are listed beneath it. For example:
DO WHILE student-total < 50

Read Student record

Print student name, address to report

Add 1 to student-total

END DO

June 7, 2024 By Kibru G. 53


Example
Find the biggest of three (3) Numbers:
BEGIN
NUMBER num1,num2,num3
INPUT num1
INPUT num2
INPUT num3
IF num1>num2 AND num1>num3 THEN
OUTPUT num1+ "is higher"
ELSE IF num2 > num1 And num2 > num3 THEN
OUTPUT num2 + "is higher"
ELSE
OUTPUT num3+ "is higher“
ENDIF
END

June 7, 2024 By Kibru G. 54


Examples
Write pseudocode that print “computer Science Pseudocode
5 times:
Algorithm Begin
1. Start Set counter=1
2. Declare and initialize counter variable, Set
counter to 1 While counter ≤ 5 D o
3. Print “computer Science Print “Computer science"
4. Increment the counter by one Counter=counter+1
counter=counter+1
5. Check if counter is less than five, if counter less
End While
than and equal to 5 goto step 3
6. Stop

June 7, 2024 By Kibru G. 55


End chapter 1;
}

June 7, 2024 By Kibru G. 56

You might also like