CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and...

31
CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1

Transcript of CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and...

Page 1: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

CHAPTER 1INTRODUCTION1st semester 1432-1433 H

King Saud University College Of Applied Studies and Community Services CSC 1101Computer Programming-1

Page 2: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

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

Page 3: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

3

Computer Organization

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

Page 4: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

4

What a computer program is?

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

Page 5: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

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 convert to machine language

High-level language Allows for writing more “English-like” instructions

Contains commonly used mathematical operations Compiler convert to machine language

Page 6: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

6

An Overview Of Computer Languages

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

Page 7: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

7

Levels of Program Development

Human thought Pseudo-Natural Language (English, Arabic) High Level Programming Language (C, C+

+, Java, …) Machine Code

Page 8: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

The Prorammer’s Algorithm

8

An algorithm is a finite sequence of instructions that produces a solution to a problem.

The programmer’s algorithm: Define the problem. Plan the problem solution. Code the program. Compile the program. Run the program. Test and debug the program.

Page 9: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

9

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

Page 10: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIR

10

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

Page 11: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIR

11

Example 1Area and Perimeter of a rectangle

Input Length width

Processing Area = length*width Perimeter = 2*( length + width)

Output Area Perimeter

Page 12: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIR

Page 12

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 13: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIR

Page 13

Example 3Area and Perimeter of a circle

Input Radius PI

Processing Area = PI * Radius * Radius Perimeter = 2 * PI * Radius

Output Area Perimeter

Page 14: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

2-Planning the Solution

Asma Alosaimi

14

When planning, algorithms are used to outline the solution steps using Englishlike statements, called pseudocode.

• A flowchart is useful for the graphical representation of an algorithm.• They are drawn using rectangles, diamonds,

ovals, and small circles.

Page 15: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

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

15

start

Read N1

Read N2

Sum = N1 + N2

Print Sum

End

Page 16: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

3-Coding the Program

16

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 “.c” extension.

Program

Coding

Algorithm Pseudocode

Source Code(The “.c”)

Translating

Page 17: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Why Coding in Programming Languages

Asma Alosaimi

17

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?

Page 18: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

18

4-Compiling Computer Programs

Asma Alosaimi

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

In C , Preprocessor program, execute automatically before the compiler translation phase begin Preprocessor indicate that certain manipulations are to be performed on the

program before compilation.

Machine code

Program Source code

Machine Code

TranslatingCompiling

Page 19: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

19

Programming Language Compiler

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.

Page 20: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

20

5-Running The Program

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

Page 21: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

21

6-Testing and Debugging the Program

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

Page 22: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

C Environment

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

C environment is consist f 6 phases :Edit, Preprocess , Compile, Link, Load, Execute

Page 23: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

Page 24: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

24

Asma Alosaimi

Pseudocode & flowcharts

Page 25: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

25

Pseudocode Example

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

Page 26: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Flow chart’s Symbols

Asma Alosaimi

26

Loops

Start/End

Read/Print

Arithmetic Operations

Decision

Connectors arrows

Start

Read n1

N2 = 5

End

Print n1

N2 = n1+3

n1 > 3

Page 27: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Example 1Area and Perimeter of a rectangle

Asma Alosaimi

27

Input Length width

Processing Area = length*width Perimeter = 2*( length + width)

Output Area Perimeter

start

Read L, W

LٍSurface =2* ( L * W )

Print area

End

area = L * W

Print perimeter

Page 28: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

28

Exercise 1

Write a program that calculates and prints the sum of the even integers from 2 to 30.

Page 29: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

29

Exercise 1- solution

I/P:no input Operations:sum=2+4+6+…..+30 O/p: the summation

Start the program Create a variable to hold the sum. Initialize the sum to zero. Create a variable to hold a counter from 2 to 30. Initialize the counter to 2. Loop While the counter is less-than-or-equal to 30

add the counter to the sum add two to the counter.

Now repeat Print the sum. End of program

Page 30: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

30

Exercise 1- solution

StartStart

EndEnd

Sum=0 ,counter=2Sum=0 ,counter=2

Counter≤30Counter≤30

Sum=sum+counterSum=sum+counter

Counter=counter+2Counter=counter+2

Print sumPrint sum

yesyes

nono

Page 31: CHAPTER 1 INTRODUCTION 1 st semester 1432-1433 H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.

Asma Alosaimi

31

Homework

Write a program that calculates the Zakat , where the user enter the amount of money then the program show the zakat.

Zakat =2.5 * amount. Zakat is not calculated if the amount is less

than 1000 S.R