برمجه
برمجه
برمجه
pg. 1
2 CoE 132 Programming and Problem Solving
List of Contents
1. Introduction to Computer and Programming
1.1. Introduction
1.2. Hardware
1.2.1 The CPU
1.2.2 Main Memory
1.2.3 Secondary Storage Devices
1.2.4 Input Devises
1.2.5 Output Devises
1.3. Software
1.3.1. System Software
1.3.2. Application Software
1.4. Computer Languages
1.4.1 Machine Language
1.4.2 Assembly Language
1.4.3 High Level Language
1.5. How Computer Store Data
1.6. Storing Numbers
1.7. Checkpoints
2. The programing processes.
3. How a Program works
4. Computer Languages
5. Compilers and Interpreters
5.1 Compiler
5.2 Interpreter
6. The Programmer Skills
7. Generations of Programming Language
8. Choosing a Programming Language
9. Algorithms
9.1 Pseudocode
9.2 Flowchart
pg. 2
3 CoE 132 Programming and Problem Solving
pg. 3
4 CoE 132 Programming and Problem Solving
A program is a set of instructions that a computer follows to perform a task. For example, Figure 1-
1 shows screens from two commonly used programs, Microso Word and Adobe
Photoshop. Microsoft Word is a word processing program that allows you to create, edit,
and print documents with your computer. Adobe Photoshop is an image editing program
that allows you to work with graphic images, such as photos taken with your digital
camera.
1.2 Hardware
The term hardware refers to all of the physical devices, or components, that a computer is made of. A
computer is not one single device, but a system of devices that all work together. Like the
different instruments in a symphony orchestra, each device in a computer plays its own
part. If you have ever shopped for a computer, you’ve probably seen sales literature listing
pg. 4
5 CoE 132 Programming and Problem Solving
components such as microprocessors, memory, disk drives, video displays, graphics cards,
and so on. Unless you already know a lot about computers, or at least have a friend that
does, understanding what these different components do might be challenging. As shown
in Figure 1-2, a typical computer system consists of the following major components:
• The central processing unit (CPU)
• Main memory
• Secondary storage devices
• Input devices
• Output devices
pg. 5
6 CoE 132 Programming and Problem Solving
Figure 1-3 The ENIAC computer (courtesy of U.S. Army Historic Computer
Images)
pg. 6
7 CoE 132 Programming and Problem Solving
RAM is usually a volatile type of memory that is used only for temporary storage
while a program is running. When the computer is turned off, the contents of RAM
are erased. Inside your computer, RAM is stored in chips, similar to the ones
shown in Figure 1-5.
pg. 7
8 CoE 132 Programming and Problem Solving
popular for data storage. Data is not recorded magnetically on an optical disc, but
is encoded as a series of pits on the disc surface. CD and DVD drives use a laser to
detect the pits and thus read the encoded data. Optical discs hold large amounts
of data, and because recordable CD and DVD drives are now commonplace, they
are good mediums for creating backup copies of data.
1.2.4 Input Devises
Input is any data the computer collects from people and from other devices. The
component that collects the data and sends it to the computer is called an input
device. Common input devices are the keyboard, mouse, scanner, microphone,
and digital camera. Disk drives and optical drives can also be considered input
devices because programs and data are retrieved from them and loaded into the
computer’s memory.
1.2.5 Output Devices
Output is any data the computer produces for people or for other devices. It might
be a sales report, a list of names, or a graphic image. The data is sent to an output
device, which formats and presents it. Common output devices are video displays
and printers. Disk drives and CD recorders can also be considered output devices
because the system sends data to them in order to be saved.
1.3 Software
If a computer is to function, software is not optional. Everything that a computer does, from
the time you turn the power switch on until you shut the system down, is under the
control of software. There are two general categories of software: system software and
application software. Most computer programs clearly fit into one of these two
categories. Let’s take a closer look at each.
1.3.1 System Software
The programs that control and manage the basic operations of a computer are generally referred
to as system software. System software typically includes the following types of
programs
pg. 8
9 CoE 132 Programming and Problem Solving
Figure 1-6 Screens from the Windows Vista, Mac OS X, and Fedora Linux
operating systems
1.3.1.2 Utility Programs A utility program performs a specialized task that enhances the
computer’s operation or safeguards data. Examples of utility programs are virus scanners,
file compression programs, and data backup programs.
1.3.1.3 Software Development Tools Software development tools are the programs that
programmers use to create, modify, and test software. Assemblers, compilers, and
interpreters are examples of programs that fall into this category.
1.3.2 Application Software
Programs that make a computer useful for everyday tasks are known as
application software. These are the programs that people normally spend most of
their me running on their computers. Figure 1-1 shows screens from two
commonly used applications: Microsoft Word, a word processing program, and
Adobe Photoshop, an image editing program. Some other examples of application
software are spreadsheet programs, email programs, web browsers, and game
programs.
1.4 Checkpoint
1.4.1 What is the program?
1.4.2 What is hardware?
1.4.3 List the five major components of a computer system?
1.4.4 What part of the computer actually runs programs?
1.4.5 What part of the computer serves as a work area to store a program and its data
while the program is running?
pg. 9
10 CoE 132 Programming and Problem Solving
1.4.6 What part of the computer holds data for long periods of time, even when there
is no power to the computer?
1.4.7 What part of the computer collects data from people and from other devices?
1.4.8 What part of the computer formats and presents data for people or other devices?
1.4.9 What fundamental set of programs control the internal operations of the
computer’s hardware?
1.4.10 What do you call a program that performs a specialized task, such as a virus
scanner, a file compression program, or a data backup program?
1.4.11 Word processing programs, spreadsheet programs, email programs, web
browsers, and game programs belong to what category of software?
Concept: All data that is stored in a computer is converted to sequences of 0’s and 1’s.
A computer’s memory is divided into tiny storage locations known as bytes. One byte is
only enough memory to store a letter of the alphabet or a small number. In order to do
anything meaningful, a computer has to have lots of bytes. Most computers today have
millions, or even billions, of bytes of memory. Each byte is divided into eight smaller
storage locations known as bits. The term bit stands for binary digit. Computer scientists
usually think of bits as tiny switches that can be either on or off. Bits aren’t actual
“switches,” however, at least not in the conventional sense. In most computer systems,
bits are tiny electrical components that can hold either a positive or a negative charge.
Computer scientists think of a positive charge as a switch in the on position, and a
negative charge as a switch in the off position. Figure 1-7 shows the way that a computer
scientist might think of a byte of memory: as a collection of switches that are each flipped
to either the on or off position.
pg. 10
11 CoE 132 Programming and Problem Solving
pg. 11
12 CoE 132 Programming and Problem Solving
Eventually, you produce a written agreement that, among other things, specifies the kind of
input, processing, and output required.
Example:
pg. 12
13 CoE 132 Programming and Problem Solving
Pseudocode is an English-like nonstandard language that lets you state your solution with more
precision than you can in plain English but with less precision than is required when using a formal
programming language. Pseudocode permits you to focus on the program logic without having
to be concerned just yet about the precise syntax of a particular programming language.
Step 3: Code the program.
You will translate the logic from the flowchart or pseudocode-or some other tool-to a
programming language. Program Coding means expressing the algorithm developed for solving a
problem, in a programming language.
pg. 13
14 CoE 132 Programming and Problem Solving
Types of errors:
Syntax Errors: Violation of syntactic rules in a Programming Language generates syntax errors.
Effect? Interpreter or Compiler finds it in Syntax Check Phase.
Semantic Errors: Doing logical mistakes causes semantic errors in Source code. Effect? Interpreters
and Compilers cannot notice them, but on execution, they cause unexpected results.
Run-time Errors: Occur on program execution. Mostly caused by invalid data entry or tries to use
not existing resources. Effect? It occurs on run time and may crash the program execution.
pg. 14
15 CoE 132 Programming and Problem Solving
As you can see from this list, the CPU performs simple operations on pieces of data. The CPU does
nothing on its own, however. It has to be told what to do, and that’s the purpose of a
program. A program is nothing more than a list of instructions that cause the CPU to perform
operations. Each instruction in a program is a command that tells the CPU to perform a
specific operation. Here’s an example of an instruction that might appear in a program:
pg. 15
16 CoE 132 Programming and Problem Solving
10110000
To you and me, this is only a series of 0s and 1s. To a CPU, however, this is an instruc on to
perform an opera on.1 It is wri en in 0s and 1s because CPUs only understand instruc ons
that are written in machine language, and machine language instructions always have an
underlying binary structure. A machine language instruction exists for each operation that a
CPU is capable of performing. For example, there is an instruction for adding numbers, there
is an instruction for subtracting one number from another, and so forth. The entire set of
instructions that a CPU can execute is known as the CPU’s instruction set.
Note: There are several microprocessor companies today that manufacture CPUs. Some of
the more well-known microprocessor companies are Intel, AMD, and Motorola. If you
look carefully at your computer, you might find a tag showing a logo for its
microprocessor. Each brand of microprocessor has its own unique instruction set,
which is typically understood only by microprocessors of the same brand. For
example, Intel microprocessors understand the same instructions, but they do not
understand instructions for Motorola microprocessors.
The machine language instruction that was previously shown is an example of only one
instruction. It takes a lot more than one instruction, however, for the computer to do anything
meaningful. Because the operations that a CPU knows how to perform are so basic in nature, a
meaningful task can be accomplished only if the CPU performs many operations. For example, if
you want your computer to calculate the amount of interest that you will earn from your savings
account this year, the CPU will have to perform a large number of instructions, carried out in the
proper sequence. It is not unusual for a program to contain thousands or even millions of
machine language instructions. Programs are usually stored on a secondary storage device such
as a disk drive. When you install a program on your computer, the program is typically copied to
your computer’s disk drive from a CD-ROM, or perhaps downloaded from a website. Although a
program can be stored on a secondary storage device such as a disk drive, it has to be copied into
main memory, or RAM, each time the CPU executes it. For example, suppose you have a word
processing program on your computer’s disk. To execute the program, you use the mouse to
double-click the program’s icon. This causes the program to be copied from the disk into main
memory. Then, the computer’s CPU executes the copy of the program that is in main memory.
This process is illustrated in Figure 1-10.
pg. 16
17 CoE 132 Programming and Problem Solving
Figure 1-10 A program is copied into main memory and then executed
When a CPU executes the instructions in a program, it is engaged in a process that is known as
the fetch-decode-execute cycle. This cycle, which consists of three steps, is repeated for each
instruction in the program. The steps are:
1- Fetch A program is a long sequence of machine language instructions. The first step of the
cycle is to fetch, or read, the next instruction from memory into the CPU.
2- Decode A machine language instruction is a binary number that represents a command
that tells the CPU to perform an operation. In this step the CPU decodes the instruction
that was just fetched from memory, to determine which operation it should perform.
3- Execute The last step in the cycle is to execute, or perform, the operation. Figure 1-11
illustrates these steps.
4. Computer languages
A vocabulary and set of grammatical rules (syntax) for instructing a computer to perform
specific tasks. Programming languages can be used to create computer programs. The term
pg. 17
18 CoE 132 Programming and Problem Solving
programming language usually refers to high-level languages, such as BASIC, C, C++, COBOL,
FORTRAN, Ada, and Pascal.
pg. 18
19 CoE 132 Programming and Problem Solving
for instructions, assembly language uses short words that are known as mnemonics. For example,
in assembly language, the mnemonic add typically means to add numbers, mul typically means
to multiply numbers, and mov typically means to move a value to a location in memory. When
a programmer uses assembly language to write a program, he or she can write short mnemonics
instead of binary numbers.
Note: There are many different versions of assembly language. It was mentioned earlier that
each brand of CPU has its own machine language instruction set. Each brand of CPU typically
has its own assembly language as well.
Assembly language programs cannot be executed by the CPU, however. The CPU only
understands machine language, so a special program known as an Assembler is used to translate an
assembly language program to a machine language program. This process is shown in Figure 1-12. The
machine language program that is created by the assembler can then be executed by the CPU.
pg. 19
20 CoE 132 Programming and Problem Solving
large numbers of low-level instructions. In addition, most high-level languages use words that
are easy to understand. For example, if a programmer were using COBOL (which was one of the
early high-level languages created in the 1950s), he or she would write the following instruc on
to display the message Hello world on the computer screen:
DISPLAY "Hello world"
Since the 1950s, thousands of high-level languages have been created. Table 1-1 lists several of
the more well-known languages.
pg. 20
21 CoE 132 Programming and Problem Solving
5.1. Compiler is a program that translates a high-level language program into a separate machine
language program. The machine language program can then be executed any time it is
needed. A compiler reads the whole source code and translates it into a complete machine code program
to perform the required tasks which is output as a new file. This is shown in Figure 1-13. As shown in
the figure, compiling and executing are two different processes.
pg. 21
22 CoE 132 Programming and Problem Solving
The statements that a programmer writes in a high-level language are called source code, or
simply code. Typically, the programmer types a program’s code into a text editor and then
saves the code in a file on the computer’s disk. Next, the programmer uses a compiler to
translate the code into a machine language program, or an interpreter to translate and
execute the code. If the code contains a syntax error, however, it cannot be translated. A
syntax error is a mistake such as a misspelled key word, a missing punctuation character, or
the incorrect use of an operator. When this happens the compiler or interpreter displays an
error message indicating that the program contains a syntax error. The programmer corrects
the error and then attempts once again to translate the program.
NOTE: Human languages also have syntax rules. Do you remember when you took your first
English class, and you learned all those rules about commas, apostrophes,
capitalization, and so forth? You were learning the syntax of the English language.
Although people commonly violate the syntax rules of their native language when
speaking and writing, other people usually understand what they mean. Unfortunately,
compilers and interpreters do not have this ability. If even a single syntax error
appears in a program, the program cannot be compiled or executed. When an
interpreter encounters a syntax error, it stops executing the program.
pg. 22
23 CoE 132 Programming and Problem Solving
- Computer and electronics: Knowledge of electric circuit boards, processors, chips, and
computer hardware and software, including applications and programming.
- Mathematics: Knowledge of numbers, their operations, and interrelationships including
arithmetic, algebra, geometry, calculus, statistics, and their applications.
- Oral expression: The ability to communicate information and ideas in speaking so others
will understand.
- Deductive Reasoning: The ability to apply general rules to specific problems to come up
with logical answers. It involves deciding if an answer makes sense.
- Information Organization: Finding ways to structure or classify multiple pieces of
information.
pg. 23
24 CoE 132 Programming and Problem Solving
9. Introduction to Algorithms
The Algorithm is a sequence of instructions. A procedure or formula for solving a problem. It was created by
mathematician, Mohammed ibn-Musa al-Khwarizmi. Often used for calculation, data processing and programming.
Algorithms can be expressed in any language.
9.1 Pseudocode: (which means fake code, because it’s 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. Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules.
pg. 24
25 CoE 132 Programming and Problem Solving
Advantages of Pseudocode
- Reduced complexity.
- Increased flexibility.
- Ease of understanding.
Why is Pseudocode Necessary?
• Then you need to organize your thoughts and create the program.
• You must break the main tasks that must be accomplished into smaller ones in order to be able to
• Writing Pseudocode will save you time later during the construction & testing phase of a program's
Development.
1. A computer can receive informa on Read (information from a file) Get (information from the
keyboard)
2. A computer can put out informa on Write (information to a file) Display (information to the screen)
3. A computer can perform arithme c Use actual mathema cal symbols or the words for the symbols
Example:
+, -, *, /
pg. 25
26 CoE 132 Programming and Problem Solving
4. A computer can assign a value to a piece of data. There are three cases:
Initialize, Set
b- to assign a value as a result of some processing
x=5+y
5. A computer can compare two piece of information and select one of two alternative actions
IF condition THEN
some action
ELSE
alternative action
ENDIF
6. A computer can repeat a group of ac ons
some action
ENDWHILE
Or you can use For expression
some action
ENDFOR
Data Dictionaries
The pseudocode by itself doesn't provide enough information to be able to write program code. Data
Dictionaries are used to describe the data used in the PseudoCode. The standard data types used in
Pseudo Code are Integer, Double, String, Char and Boolean.
pg. 26
27 CoE 132 Programming and Problem Solving
Example 1:
We might usually specify the procedure of solving this problem as “add the three numbers and divide by
three”. Here, Read (or Ask) and Write (or Say) are implied. However, in an algorithm, these steps have to
be made explicit. Thus, a possible algorithm is:
Step 1 Start
Step 3 S = X + Y + Z
Step 4 A = S / 3
Step 6 Stop
Example 2:
Step1 Start
Step3 S = N * N
Step4 C = S * N
Step6 Stop
pg. 27
28 CoE 132 Programming and Problem Solving
Example 3:
Step1 Start
Step2 Read A, B
Step5 Stop
Exercises:
1. S = (A + B + C) / Y
9.2 Flowchart: A flowchart is a graphical representation of an algorithm. These flowcharts play a vital role in the
programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems.
Once the flowchart is drawn, it becomes easy to write the program in any high-level language.
pg. 28
29 CoE 132 Programming and Problem Solving
Arrows: Showing what's called "flow of control" in computer science. An arrow coming from one symbol
and ending at another symbol. Represents that control passes to the symbol the arrow points to.
Processing steps: Represented as rectangles. Examples: “Add 1 to X"; "replace iden fied part"; "save
changes" or similar
Conditional or decision: Represented as a diamond (rhombus). These typically contain a Yes/No question
or True/False test.
Display: Indicates a process flow step where information is displayed to a person (e.g., PC user, machine
operator).
pg. 29
30 CoE 132 Programming and Problem Solving
1- Communication: Flowcharts are better way of communicating the logic of a system to all
concerned.
2- Effective analysis: With the help of flowchart, problem can be analysed in more effective way.
3- Proper documentation: Program flowcharts serve as a good program documentation, which is
needed for various purposes.
4- Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and
program development phase.
5- Proper Debugging: The flowchart helps in debugging process.
6- Efficient Program Maintenance: The maintenance of operating program becomes easy with the
help of flowchart. It helps the programmer to put efforts more efficiently on that part.
pg. 30
31 CoE 132 Programming and Problem Solving
Example 1:
pg. 31
32 CoE 132 Programming and Problem Solving
Example 2:
Find the largest among three different numbers entered by the user.
pg. 32
33 CoE 132 Programming and Problem Solving
Example 3:
pg. 33
34 CoE 132 Programming and Problem Solving
pg. 34
35 CoE 132 Programming and Problem Solving
- Sequences of instructions. The programs flow from one step to the next in strict sequence.
- Branches. The program reaches a decision point and if the result of the test is true then the
program performs the instruc ons in Path 1, and if false it performs the ac ons in Path 2.
- Loops. The program steps are repeated continuously until some test condition is reached,
at which point control then flows past the loop into the next piece of program logic.
- Modules. The program performs an identical sequence of actions several times. For
convenience these common actions are placed in a module, which is a kind of mini-program
which can be executed from within the main program.
pg. 35