1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program...

31
1 Topics Program Language Steps To Build a Program Arithmetic Operation Priorities Program Errors Types Sample Program

Transcript of 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program...

Page 1: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

1

Topics Program Language Steps To Build a Program Arithmetic Operation Priorities Program Errors Types Sample Program

Page 2: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

2

Introduction Software

Instructions to command computer to perform actions and make decisions

Hardware Standardized version of C++

United States– American National Standards Institute (ANSI)

Worldwide– International Organization for Standardization

(ISO) Structured programming Object-oriented programming

Page 3: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

3

Program Languages Three types of computer languages

1. Machine language– Only language computer directly understands– “Natural language” of computer– Defined by hardware design

Machine-dependent– Generally consist of strings of numbers

Ultimately 0s and 1s– Instruct computers to perform elementary operations

One at a time– Cumbersome for humans– Example:

+1300042774+1400593419+1200274027

Page 4: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

4

Program Languages Three types of computer languages

2. Assembly language– English-like abbreviations representing

elementary computer operations – Clearer to humans– Incomprehensible to computers

Translator programs (assemblers) Convert to machine language

– Example: LOAD BASEPAYADD OVERPAYSTORE GROSSPAY

Page 5: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

5

Program Languages Three types of computer languages

3. High-level languages – Similar to everyday English, use common

mathematical notations– Single statements accomplish substantial tasks

Assembly language requires many instructions to accomplish simple tasks

– Translator programs (compilers) Convert to machine language

– Interpreter programs Directly execute high-level language programs

– Example:grossPay = basePay + overTimePay

Page 6: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

6

Steps To Build a Program

Before writing a program Have a thorough understanding of problem Carefully plan your approach for solving it

While writing a program Know what “building blocks” are available Use good programming principles

Page 7: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

7

Steps To Build a ProgramThe sequence of steps to be performed in order to solve aproblem by the computer is known as an algorithm.

Flowchart is a graphical or symbolic representation of analgorithm. It is the diagrammatic representation of the

step-by-step solution to a given problem.

Program Design consists of the steps a programmer should do before they start coding the program in a specific language. Proper program design helps other programmers to maintain the program in the future

Page 8: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

8

AlgorithmConsider an example for finding the largest number in anunsorted list of numbers?.

The solution for this problem requires looking at every number in thelist, but only once at each.

1) Algorithm using natural language statements:a) Assume the first item is largest.b) Look at each of the remaining items in the list and if it is largerthan the largest item so far, make a note of it.c) The last noted item is the largest item in the list when the process is complete.

2) Algorithm using pseudocode:largest = L0for each item in the list (Length(L) >= 1), doif the item >= largest, thenlargest = the itemreturn largest

Page 9: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

9

Algorithms Computing problems

Solved by executing a series of actions in a specific order

Algorithm a procedure determining Actions to be executed Order to be executed Example: recipe

Program control Specifies the order in which statements

are executed

Page 10: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

10

Pseudocode Pseudocode

Artificial, informal language used to develop algorithms

Similar to everyday English Not executed on computers

Used to think out program before coding– Easy to convert into C++ program

Only executable statements– No need to declare variables

Page 11: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

11

if Selection Structure

Selection structure Choose among alternative courses of action Pseudocode example:

If student’s grade is greater than or equal to 60

Print “Passed” If the condition is true

– Print statement executed, program continues to next statement

If the condition is false– Print statement ignored, program continues

Indenting makes programs easier to read– C++ ignores whitespace characters (tabs, spaces, etc.)

Page 12: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

12

if Selection Structure Translation into C++

If student’s grade is greater than or equal to 60

Print “Passed”

if ( grade >= 60 ) cout << "Passed";

Diamond symbol (decision symbol) Indicates decision is to be made Contains an expression that can be true or false

– Test condition, follow path if structure

Single-entry/single-exit

 

Page 13: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

13

Flowchart Flowchart

Graphical representation of an algorithm Special-purpose symbols connected by arrows (flowlines) Rectangle symbol (action symbol)

– Any type of action Oval symbol

– Beginning or end of a program, or a section of code (circles)

Single-entry/single-exit control structures Connect exit point of one to entry point of the next Control structure stacking

Page 14: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

14

الهندسي الشكل المعني

النهاية - - البدايةالتوقفStart - End - Stop

المدخالت - المخرجاتInput - Output

العملياتProcesses

شرط أو قرارDecision

إتصال نقطConnector Point

التكرار حلفاتFor Or Loop

البيانات تدفق إتجاةData Flow Direction

Flowchart

Page 15: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

15

Example For example, consider that we need to find

the sum, average and product of 3 numbers given by the user.

Algorithm for the given problem is as follows:

Read X, Y, Z Compute Sum (S) as X + Y + Z Compute Average (A) as S / 3 Compute Product (P) as X x Y x Z Write (Display) the Sum, Average and Product

Page 16: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

16

Flowchart

Page 17: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

17

Advantages of Using FlowchartsCommunication: Flowcharts are better way of communicatingthe logic of a system to all concerned. Effective analysis: With the help of flowchart, problem can beanalysed in more effective way. Proper documentation: Program flowcharts serve as a goodprogram documentation, which is needed for various

purposes. Efficient Coding: The flowcharts act as a guide or blueprintduring the systems analysis and program development

phase. Proper Debugging: The flowchart helps in debugging

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

Page 18: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

18

Limitations of Using Flowcharts

Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy.

Alterations and Modifications: If alterations are required the flowchart may require re-drawing completely

Page 19: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

19

والثوابت & Variables)المتغيراتConstant:)

مواقع / Variablesالمتغيرات عن عبارة هيمؤقت بشكل البيانات فيها تخزن الذاكرة في

البرنامج تنفيذ أثناء قيمتها تغيير ويمكنفي / Constant الثوابت مواقع عن عبارة هي

وال مؤقت بشكل البيانات فيها تخزن الذاكرةالبرنامج تنفيذ أثناء قيمتها تغيير يمكن

Page 20: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

20

الحسابية Arithmatic)العملياتOperations)

الجمع● : Additionعمليةعلي " + " الجمع عمليات إلجراء المعامل يستخدم

مثال : ، األرقامPrint 15.3 + 10.2

<25.5> الطرح ● :Subtractionعمليةعلي " – " الطرح عمليات إلجراء المعامل يستخدم

مثال : ، األرقامPrint 10 - 3 <7>

الضرب ● :Multiplicationعمليةمثال " * " : ، المعامل يستخدم

Print 15 * 2 <30>

Page 21: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

21

القسمة ● :Divisionعمليةمثال " / " ، المعامل :يستخدم

Print 10 / 2 <5>

األس ● :Exponentiationعمليةالمعامل " ^ مثال : " يستخدم ،

Print 3 ^ 2 <9>

● ، القسمة ناتج : Integer Divisionعمليةالرمز " \ رقم ) " يستخدم قسمة عملية إجراء عند علي ( 5،

بعرض ( 2) رقم بعرض ( 2.5) يقوم يقوم القسمة فناتج ،مثال ( :2) الرقم ، غير ال فقط

Print 5 \ 2 <2> Print 15 \ 3 <5>

Page 22: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

22

● ، القسمة باقي :Reminderعمليةالرمز " علي " Modيستخدم للحصول وتستخدم ،

مثال : ، الصحيح القسمة باقيX = 18 / 5 القسمة عملية إجراء

<3.6 > X = 3 * 5 في القسمة ناتج ضرب

الثاني الرقم <15>

X = 18 - 15 من األول الرقم 15طرح <3> النهائي القسمة ناتج

الرمز " ، " Modولكن السابقة العمليات كل يوفر ،:مثال

X = 18 Mod 5 <3 >

Page 23: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

23

Priorities) الحسابيةالعمليات تنفيذ أولويات Operations):

التالي ) ( - 1 المثال أنظر ، الخارج الي الداخل من األقواس:

X = 15 * (10 + (12 / 2))X = 15 * (10 + 6)X = 15 * 16X = 240

مثال : ( األس ) ^- 2 ،X = 12 + (2 ^ (5 + 1))X = 12 + (2 ^ 6)X = 12 + 64X = 76

مثال :- 3 ، Rأوال ينفذ Rأوال يأتي وما ، والقسمة الضربX = 2 * 3 / 2X = 6 / 2X = 3

4 -. السالسل وجمع والطرح الجمع

Page 24: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

24

EX:1Y = 5 + 6 * 2Y = 5 + 12Y = 17

EX:2Y = (5 + 6) * 2Y = 11 * 2Y = 22

Page 25: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

25

Sample program

Page 26: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

26

باستخدام الدائرة ومساحة محيط لحساب برنامج Q Basicأكتب

Rem circle program

Input r

Pi = 3.14

P = 2 * pi * r

A = pi * r ^ 2

Print P , A

End

Page 27: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

27

ارقام / ثالثة جمع برنامج

Rem three numbers program

Input a , b , c

D = a + b + c

Print d

End

Page 28: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

28

المثلث / مساحة حساب برنامج

Rem triangle program

Input a , b

D = 0.5 * a * b

Print d

End

Page 29: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

29

مساحة / و محيط حساب برنامج المربع

Rem square programInput x P = 4 * xA = x ^ 2Print P , AEnd

Page 30: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

30

Errors Types

Syntax Errors Logic Errors Executable Errors

Page 31: 1 Topics l Program Language l Steps To Build a Program l Arithmetic Operation Priorities Program Errors Types l Sample Program.

31

The End

Lecture 0

C/C++ programming

2014