Upload
elizabeth-montgomery
View
215
Download
2
Embed Size (px)
Citation preview
CHAPTER 1INTRODUCTION1st Semester 1433-1434 H
King Saud University College Of Applied Studies and Community Services CSC 1101Computer Programming-1Done By: Asmal AlosaimiEdited By: Noor Alhareqi
What Is a Computer?
Computer Executes statements (computations/logical
decisions) Hardware :Physical devices of computer system Software: Programs that run on computers
Control Unit
Arithmetic/Logic Unit
Memory Unit
Input device Output Device
Central Processing Unit
My ProgamMy data
Asma Alosaimi
2
Computer Organization
Asma Alosaimi
3
Six logical units of computer system Input unit
Mouse, keyboard Output unit
Printer, monitor, audio speakers Memory unit
Retains input and processed information Arithmetic and logic unit (ALU)
Performs calculations Control unit
Supervises operation of other devices Secondary storage unit
Hard drives, floppy drives
What a computer program is?
Asma Alosaimi
4
For a computer to be able to perform specific tasks (i.e. print what grade a student got on an exam), it must be given instructions to do the task.
The set of instructions that tells the computer to perform specific tasks is known as a computer program
Machine Languages, Assembly Languages and High-Level Languages
Machine language “Natural language” of computer component Machine dependent
Assembly language English-like abbreviations represent computer
operations Translator programs (Assemblers) convert to machine
language High-level language
Allows for writing more “English-like” instructions Contains commonly used mathematical operations
Compiler convert to machine language
An Overview Of Computer Languages
6
1-Machine language
2-Assembly language
3-High-level language
representation
Collection of binary numbers
Symbolic form of machine language (I.e. symbolic names are used to represent operations, registers & memory locations)
combines algebraic expressions with symbols taken from English language(ex. C, Pascal, FORTRAN, …etc)
examples 10100001 00000000 0000000000000101 00000100 0000000010100011 00000000 00000000
MOV AX,AMOV A , AXADD AX,
A=A+4 *c
how does the computer understand ?
Directly understood by a computer
Assembler convert to machine language
Compiler or (interpter )convert to machine language
Easier to use
More powerful
Levels of Program Development
Asma Alosaimi
7
1. Define the problem. Human thought
2. Plan the problem solution. writing the algorithm [pseudo-natural language (English, Arabic)]
3. Code the program. High Level Programming Language (C, C++, Java, …)
4. Compile the program. Machine Code
5. Run the program.6. Test and debug the program.
8
1-Defining the Problem
The problem must be defined in terms of: Input: Data to be processed. Output: The expected result.
Look for nouns in the problem statement that suggest output and input.
and processing: The statements to achieve. Look for verbs to suggest processing steps.
Keyboard ScreenProcessing
input data output data
9
Input and Output
Inputs Can come from many sources, such as
users, files, and other programs Can take on many forms, such as text,
graphics, and sound
Outputs Can also take on many forms, such as
numbers, text, graphics, sounds, or commands to other programs
10
Example 1Area and Perimeter of a rectangle
Input Length width
Processing Area = length*width Perimeter = 2*( length + width)
Output Area Perimeter
Page 11
Example 2Sum and Average of 5 numbers
Input five number X1, X2, X3, X4, X5
Processing Sum = X1+X2+X3+X4+X5 Average = Sum/5
Output Sum Average
Page 12
Example 3Area and Perimeter of a circle
Input Radius PI
Processing Area = PI * Radius * Radius Perimeter = 2 * PI * Radius
Output Area Perimeter
2-Planning the Solution
Asma Alosaimi
13
When planning, algorithms are used to outline the solution steps using English like statements, called pseudocode.
• A flowchart is useful for the graphical representation of an algorithm.• They are drawn using rectangles, diamonds,
ovals, and small circles.
Write a Program to Print the Sum of two integer Numbers
1. Start the program2. Read the first number and
save in the variable ( N1 )3. Read the second number
and save in the variable ( N2 )
4. Sum the both numbers and save the result in the variable ( Sum ) Sum = N1 + N2
5. Print the variable ( Sum ) 6. End the program
Asma Alosaimi
14
start
Read N1
Read N2
Sum = N1 + N2
Print Sum
End
3-Coding the Program
Asma Alosaimi
15
Coding is writing the program in a formal language called Programming Language.
Programming Language : A set of rules, symbols and special words used to write statements.
The program is written by translating the algorithm steps into a programming language statements.
The written program is called Source code and it is saved in a file with “.cpp” extension.
Program
Coding
Algorithm Pseudocode
Source Code(The “.cpp”)
Translating
Why Coding in Programming Languages
Asma Alosaimi
16
We write computer programs (i.e. a set of instructions) in programming languages such as C, C++, and Java.
We use these programming languages because they are easily understood by humans
But then how does the computer understand the instructions that we write?
4-Compiling Computer Programs
Asma Alosaimi
17
Computers do not understand programs written in programming languages such as C, C++ and Java
Programs must first be converted into machine code that the computer can run
A Software that translates a programming language statements into machine code is called a compiler
Machine code
Program Source code
Machine Code
TranslatingCompiling
Programming Language Compiler
Asma Alosaimi
18
A compiler is a software that: Checks the correctness of the source
code according to the language rules. Syntax errors are raised if some rules were
violated. Translates the source code into a
machine code if no errors were found.
5-Running The Program
Asma Alosaimi
19
Before running.. links the object code with the code for the missing
functions to produce an executable image (with no missing pieces) , this is called link phase .
If the program compiles and links correctly, a file called a.out is produced.
Before a program can be executed, the program must first be placed in memory this called load phase .
the computer, under the control of its CPU, executes the program one instruction at a time
6-Testing and Debugging the Program
Asma Alosaimi
20
Testing Be sure that the output of the program conforms
with the input. There are two types of errors:
Logical Errors: The program run but provides wrong output.
Runtime errors: The program stop running suddenly when asking the OS executing a non accepted statement (divide by zero, etc).
Debugging Find, Understand and correct the error
21
Basics of a Typical C++ Environment
Phases of C++ Programs:1. Edit2. Preprocess3. Compile4. Link5. Load6. Execute
Loader
PrimaryMemory
Program is created inthe editor and storedon disk.
Preprocessor programprocesses the code.
Loader puts programin memory.
CPU takes eachinstruction andexecutes it, possiblystoring new datavalues as the programexecutes.
CompilerCompiler createsobject code and storesit on disk.
Linker links the objectcode with the libraries,creates a.out andstores it on disk
Editor
Preprocessor
Linker
CPU
PrimaryMemory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
Disk
Disk
Asma Alosaimi22