1 topics l program language l steps to build a program l arithmetic operation priorities program...

Post on 02-Jan-2016

217 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Topics Program Language Steps To Build a Program Arithmetic Operation Priorities Program Errors Types 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

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

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

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

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

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

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

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

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

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

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

 

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

14

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

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

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

العملياتProcesses

شرط أو قرارDecision

إتصال نقطConnector Point

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

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

Flowchart

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

16

Flowchart

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

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

19

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

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

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

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

20

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

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

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

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

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

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

Print 15 * 2 <30>

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>

22

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

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

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

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

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

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

X = 18 Mod 5 <3 >

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 -. السالسل وجمع والطرح الجمع

24

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

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

25

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

27

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

Rem three numbers program

Input a , b , c

D = a + b + c

Print d

End

28

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

Rem triangle program

Input a , b

D = 0.5 * a * b

Print d

End

29

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

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

30

Errors Types

Syntax Errors Logic Errors Executable Errors

31

The End

Lecture 0

C/C++ programming

2014

top related