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

Chapter 1 - Introduction

The document provides an introduction to computer programming, defining key concepts such as computers, computer programs, and the programming process. It explains the components of a computer, programming languages, and various programming paradigms, including procedural and object-oriented programming. Additionally, it discusses problem-solving techniques, algorithms, flowcharts, and the system development life cycle (SDLC) in programming.
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)
12 views

Chapter 1 - Introduction

The document provides an introduction to computer programming, defining key concepts such as computers, computer programs, and the programming process. It explains the components of a computer, programming languages, and various programming paradigms, including procedural and object-oriented programming. Additionally, it discusses problem-solving techniques, algorithms, flowcharts, and the system development life cycle (SDLC) in programming.
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/ 53

Computer Programming

Chapter 1 Introduction
Compiled by: Kidane W.

1
Introduction
• 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,, debugging / troubleshooting, testing and maintaining the source code of computer
programs.

2
3
Introduction
• Central Processing Unit (CPU) - The part of the computer that executes the instructions
(program) stored in memory. The CPU is made up of the Control Unit and the
Arithmetic/Logical Unit.

• Control Unit - The component of the CPU that controls the actions of the other components
so that instructions are executed in the correct sequence.

• Arithmetic/Logical Unit (ALU) - the component of the CPU that performs arithmetic and
logical operations.

• Memory Unit - Internal data storage in a computer. The memory is comprised of a large
number of individual cells, each with a unique number, known as its address, associated
with it.
4
Introduction
• 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.

5
What is a program ?
 A program is a sequence of instructions that specifies how to
perform a computation and written in a language the computer
understands.
 The computation might be something mathematical, such as
solving a system of equations or finding the roots of a polynomial.
 But it can also be a symbolic computation, such as searching and
replacing text in a document or (strangely enough) compiling a
program.
What’s a Program?

• 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

7
Program Components
• A few basic instructions appear in every language:
1. Input - Get data from the keyboard, a file, or some other device.
2. Output - Display data on the screen or send data to a file or other
device.
3. Math Perform basic mathematical operations like addition and
multiplication.
4. Conditional execution - Check for certain conditions and execute the
appropriate sequence of statements.
5. Repetition/Looping - Perform some action repeatedly, usually with
some variation.
Programming: a way of thinking
 Combines features from mathematics, engineering, and
natural science.
 Like mathematicians, computer scientists use formal languages to
denote ideas (specifically computations).
 Like engineers, they design things, assembling components into
systems and evaluating tradeoffs among alternatives.
 Like scientists, they observe the behavior of complex systems, form
hypotheses, and test predictions.
Programming: a way of thinking

• The single most important skill for a computer scientist is


problem solving.

• Problem solving means the ability to formulate problems,


think creatively about solutions, and express a solution
clearly and accurately.
A Programming Language
• A programming language is a set of symbols, grammars and rules
with the help of which one is able to translate algorithms to
programs that will be executed by the computer.

• At the beginning there was only machine language: a sequence of


bits (binary with: electric switches on and off, or punched cards)
that directly controls a processor, causing it to add, compare, move
data from one place to another, etc.

11
A Programming Language
• an artificial language that can be used to control the behavior of a computer
which 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

• Available programming languages come in a variety of forms and types.


Programming languages can be divided in to two major categories:
• Low-level – machine language

• High-level languages- C++ , Java , etc

12
A programming Language
• Machine language
• the binary representation of the instructions that a computer's
hardware can perform
• different computers understand different sequences
• hard for humans to understand: 01010001…

• Assembly language
• a low-level programming language in which a mnemonic is
used to represent each of the machine language instructions
for a specific computer.
• Still hard for humans to understand:

13
Source Program - A program written in a human readable version.
Object Program - The machine language version of a source
program.

14
Programming Language
• High-Level Language - A computer language that is more understandable and
closer to standard notations than assembler language.
• For example , C++ is a high-level language based on algebraic notation.
Interpreting vs. Compiling Programs

• Each type of computer only “understands” its own machine language (zeroes
and ones)

• Thus we must translate from High-level language to machine language


• a team of experts program a translator, called a “compiler” which translates entirety of a
High-level language program to an executable file in computer’s native machine
language.

• At the time of Running :


• compilation: program  executable
• execution: run executable
• The machine executes your program by “running” each machine language instruction in
the executable file 16
Interpreting vs. Compiling Programs
• An alternative to compiling program is to interpret program

• each line of your program is translated into machine language and


immediately executed

• Like translating between natural languages

• Compiler: human translator translates book in its entirety and then translated
book is printed and read

• Interpreter: human interpreter translates each spoken statement in sequence as


speaker is speaking

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 19
Programming paradigm…
• A programming paradigm is a fundamental style of programming
• Unstructured Programming

• Procedural programming

• Object Oriented Programming

20
Unstructured Programming
• Consisting only of one large (usually main) program

• “main program”' stands for a sequence of commands or statements


• data is global throughout the whole program

• disadvantages
• Complex
• if the same statement sequence is needed at different locations within the
program, the sequence must be copied

21
Procedural programming

• Based upon the concept of procedure call

• 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

22
Procedural programming
• We have a single program, which is divided into small pieces called procedures

• Advantage
• to re-use the same code at different places in the program without copying it
• easier way to keep track of program flow
• Example
• FORTRAN, ADA

23
Object Oriented Programming - OOP
• 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.

24
Problem Solving
• The ability to solve problems is a basic life skill and is essential to our day-to-day
lives, at home, at school, and at work. We solve problems every day without really
thinking about how we solve them.

• For example: it’s raining and you need to go to Bate. What do you do? There are
lots of possible solutions.
1. Take your umbrella and walk.

2. If you don't want to get wet, you can drive, or take the bus.

3. You might decide to call a friend for a ride, or you might decide to go to Bate another day.

There is no right way to solve this problem and different people will solve it differently.

25
Problem solving Techniques
• Computer solves varieties of problems that can be expressed in a finite number of
steps

• A program
• solve a problem : should be effective

• should be reliable, (maintainable) portable and efficient

• Computer programming two facts :


• Defining the problem and logical procedures to follow in solving it.

• Introducing the means by which programmers communicate those procedures to the


computer system so that it can be executed.
26
Control Structures
• programs could be written in terms of three ways:
• Sequence – which instruction should be done next?
• Selection – select between options
if (condition)
Statement
else
Statement
• Repetition – repeat an action while a given condition stays true
While (condition)
statement

27
Algorithm
• Writing a logical step-by-step method to solve the problem is called
the algorithm. In other words, an algorithm is a procedure for solving
problems. In order to solve a mathematical or computer problem,
this is the first step in the process.

• An algorithm includes calculations, reasoning, and data processing.


Algorithms can be presented by natural languages, pseudocode,
and flowcharts, etc.

28
Programs vs. Algorithms
• To make a computer do anything, you have to write a program

• To write a computer program, you have to tell the computer, step by step, exactly what
you want it to do.

• The computer then "executes" the program, following each step mechanically, to
accomplish the end goal.

• An algorithm is logical procedures and a finite, step-by-step sequence of instructions that


describe how the data is to be processed to produce the desired outputs

• The algorithm is implemented by a program


• An algorithm can be represented as
• Flowchart
• Pseudocode

29
Pseudocode
• is an artificial and informal language that helps programmers
develop algorithms
• allows the designer to focus on the logic of the algorithm without
being distracted by details of programming language syntax
• “text-based" detail (algorithmic) design tool
• Pseudocode is not a rigorous notation, since it is read by other
people, not by the computer.

30
Pseudocode
• Computation/Assignment • Conditional
if "condition"
"variable" equals "expression" or (subordinate) statement 1
"variable" = "expression" else
(subordinate) statement 2

• Input/Output
• Iterative
get "variable", "variable", ... while "condition"
display "variable", "variable", ... (subordinate) statement 1

31
Pseudocode
• Write a program that prints “passed” when the student grade is greater than 60 and
“failed” otherwise.
If student's grade is greater than or equal to 60
Print "passed"
else
Print "failed“
• Pseudo-code for computing the final price of an item after the sales tax rate. Note the
three types of instructions: input (get), process/calculate (=) and output (display)
1. get price of item
2. get sales tax rate
3. sales tax = price of item times sales tax rate
4. final price = price of item plus sales tax
5. display final price
6. stop

• Variables: price of item, sales tax rate, sales tax, final price

32
Flowchart symbols
A graphic representation of an algorithm, often used in the design phase of
programming to work out the logical flow of a program.

• Action\operation Symbol
• Instructions changing a state
• Decision Symbol
• Instructions testing a state
• Flowline
• Instructions transferring to next step
• Start/End Symbol

• Input/output symbol
33
Decision Structure

• A new symbol, the diamond, indicates a yes/no question.


If the answer to the question is yes, the flow follows one
path. If the answer is no, the flow follows another path:

34
Repetition(Looping) Structure
• Notice the use of the diamond symbol. A loop tests a condition, and
if the condition exists, it performs an action. Then it tests the
condition again. If the condition still exists, the action is repeated.
This continues until the condition no longer exists.

35
GUIDELINES DRAW A FLOWCHARTS
1. The A flowchart should be clear, neat and easy to follow.
2. The usual direction of the flow of a procedure or system(arrow) is
from “Left to Right” or “ Top to Bottom”.
3. Only one flow line should come out from a process symbol.
4. Only one flow line should enter a decision symbol.
5. Ensure that the flowchart has a logical start and finish.

36
Flowchart Examples
• Single Selection (if)
• If you’ve won the lottery:
raise your hand True
Won Raise
lottery? Hand

False

• Double Selection (if-else)


• If you’re happy: False True
smile Frown Happy? Smile
else:
frown

37
Selection (continued)
• Multiple Selection (switch)
• If the light is ... Light True
Stop
Red?
• red -> stop
False
• green -> go True
• yellow -> slow down Light
Green? Go

False True
Light Slow
Yellow? Down
False

38
Teaching a baby to count from 1 to
10: Repetition
counter = 1
if counter <= 10: Counter = 1
increment counter
Add 1
print counter number to counter

Counter True
Print
≤ 10? counter

False

39
Exercises:
Draw a flow chart for the following series
• Sum=2+4+6+....+n
• Sum=1+3+5+7+...+n
• Sum=12 + 22+...+n2
• Sum=1-2+3-4+5-6+7....n

40
System Development Life Cycle
(SDLC)
1. Feasibility Study
2. Requirements analysis
3. Designing solution
4. Testing designed solution
5. Implementation
6. Unit testing
7. Integration and System testing
8. Maintenance

41
Development of Computer Solution
1. Identify or Define the problem
2. Analyze the problem in terms of inputs,
outputs, formulas, constants)
3. Design the Solution
4. Implement (program coding)
5. Evaluate
Analyze the problem
 We need to read it till we understand every detail
 We need to dissect the problem into its component parts (e.g.
problems and sub-problems)
 We need to remove any ambiguity, extra information
 We need to determine our known and our unknowns
 We need to be aware of any assumptions we are making.
Design the solution

 Developing the algorithm that solves the problem


 Identify alternative ways to solve the problem
 Select the best way to solve the problem from the list of
alternative solutions
 List instructions that enable you to solve the problem using
selected solution

 The algorithm is expressed a s flowchart or pseudo-code


Payroll example - Analysis
Payroll example - Design
Principles of Computer Programming
• Keep it simple: Write code that is straight forward and easy to understand.

• Define things once and only once and let computers repeat and execute tasks:
[Rule of 3]

• Only implement functionality that is required, rather than trying to anticipate future
needs.

• The single responsibility principle (SRP): Each module should have a single, well-
defined responsibility.

• Open-closed principle (OCP): Code should be open for extension but closed for
modification.
47
Principles of Computer Programming
• Write programs for people, not computers: Make your code easy to understand
for humans. If your code looks very complex or messy, you’re probably doing it
wrong.

• Optimize software only after it works correctly: Even experts find it hard to predict
performance bottlenecks.

• Language-specific style guides (C++, Python, Java)

• Document Your Code

48
End of Chapter One

49
Bloom’s Taxonomy (1st Year –2nd Year)

50
Unified domain taxonomy (3rd & 4th Year)
There have been a number of attempts to produce a taxonomy that covers the cognitive (C), affective (A) and psychomotor (P)
domains. The work of De Block.

Level C-A-P
Knowledge C: Repeat, define, show, name, etc.
A: Listen to opinion of others, accept notes, realize, etc.
P: Show, imitate, understand sound, smell, taste, etc.
Understanding C: Describe, characterize, say in own words, explain, compare, etc.
A: Accept opinions of others, answer questions, react to rules correctly, ask relevant
questions, participate, etc.
P: Demonstrate a principle, put together and disassemble something that is known, etc
Application C: Solve, calculate, number, translate, illustrate, analyze, make, etc.
A: React to rules automatically, accept norms and values, cooperate in a group, apply
norms and rules, etc.
P: Make, produce, try, repair, adapt, cook, cut, put together and disassemble
something that is new, etc.
Integration C: Design, create, summarize, judge, decide, plan, etc.
A: React to rules spontaneously, apply norms spontaneously and behave under rules,
initiate cooperation, find satisfaction in behavior and work under society’s rules, etc.
P: Perform an activity fluently, without hesitation, without mistakes, automatically; work
precisely, quickly, etc.
51
Two Dimensional Adaptation of Bloom’s
Taxonomy – The Matrix Taxonomy
• The dimensions of the matrix
represent the two separate
ranges of competencies:
1. the ability to understand and
interpret an existing product (i.e.
program code), and
2. the ability to design and build a
new product.
• The idea of a cognitive learning
taxonomy can also be used in an
iterative, spiral way.
• Create could be described as the
ability to combine one subject with
others in order to build new solutions.
52
Programming Activities
• Model: illustrate or create an abstraction
• Adapt: modify a solution for other domains/ranges
of a solution
• Analyze: probe the [time] complexity of a solution
• Present: explain a solution to others
• Apply: use a solution as a component in a larger
• Recognize: base knowledge, vocabulary
problem
of the domain
• Debug: both detect and correct flaws in a design
• Refactor: redesign a solution (as for
• Design: devise a solution structure optimization)
• Implement: put into lowest level, as in coding a • Relate: understand a solution in context
solution, given a completed design. of others

• Trace: desk-check a solution


53

You might also like