notes2

notes2 - CMPS 10 Introduction to Computer Science Lecture...

This preview shows pages 1–4. Sign up to view the full content.

5 CMPS 10 Introduction to Computer Science Lecture Notes Chapter 2: Algorithm Design How should we present algorithms? Natural languages like English, Spanish, or French which are rich in interpretation and meaning are not ideal for this purpose. We need a more precise notation to present algorithms that is not subject to differing interpretations. At the other extreme formal programming languages (such as C, C++, Java, Perl, ADA, Python, etc.) are very inflexible in their syntax, and require much attention to detail. These details may be irrelevant in the initial design phases of an algorithm. Instead we use an informal and flexible language called pseudo-code which uses English language constructs and conventions modeled to look like commands available in most computer languages. Detailed & Exact Expressive & Abstract Computer Languages Pseudo-Code Natural Languages Pseudo-code is a language in which all necessary operations can be expressed, but which is independent of any computer language, or of any computer for that matter. Sequential Operations are of three basic kinds: computation , input , and output . The instruction for performing a calculation and storing the result is written as expression variable This instruction tells the computing agent to evaluate the expression on the right and assign its value to the variable on the left. The left arrow in this context is called the assignment operator . Example carry + + i i i b a c Example a ac b b x 2 4 2 - + - Unless otherwise stated, we will assume that the computing agent is at least as capable as any scientific calculator, so that the above expressions are considered primitive operations.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Input and output operations allow the computing agent to receive data values from, and send results to the outside world, which we also call the “user”. Input Computing Outside Agent World Output The operations used for input and output are get and print respectively. get variable print variable print expression Example get x Example print x Example print 5 + x Text to be printed will be placed in single quotes. Example print ‘error: division by zero’ Example print ‘the value of x is: ’ x ‘and the value of y is: ’ y An algorithm which uses only sequential operations is sometimes called a straight line algorithm. Example 1) get a , b , c , d 2) d c b a + + + sum 3) 4 / sum average 4) print average What does this algorithm do? Conditional operations (also called branching operations ) are expressed using the if statement or the if-else statement. The if statement is written as if condition do something do something .. do something do something ..
7 In this structure “condition” stands for a logical (or Boolean ) expression, i.e. an expression that can be evaluated as true or false. If condition is true, then the indented lines are executed, followed by the unindented lines. If on the other hand condition is false, then the indented lines are skipped, and execution picks up at the next unindented line. The indented commands are

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/15/2011 for the course CMPS 10 taught by Professor Tantalo during the Fall '08 term at UCSC.

Page1 / 11

notes2 - CMPS 10 Introduction to Computer Science Lecture...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online