1 / 59

INTRODUCTION TO PROGRAMMING

Chapter 1. INTRODUCTION TO PROGRAMMING. ALGORITHM. An algorithm is defined as the finite set of steps to be followed in order to solve the given problem. Basically, it gives the set of instructions which must be followed while solving the problem. Example 1:

kateb
Download Presentation

INTRODUCTION TO PROGRAMMING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 1 INTRODUCTIONTO PROGRAMMING

  2. ALGORITHM • An algorithm is defined as the finite set of steps to be followed in order to solve the given problem. • Basically, it gives the set of instructions which must be followed while solving the problem.

  3. Example 1: To find the largest of three numbers.Algorithm:Step 1: Read the three numbers, say A, B and C.Step 2: If (A>B) and (B<C) then A is the largest number, go to step 4.Step 3: If (B>A) and (A>C) then B is the largest else C is the largest number.Step 4: Stop.

  4. Example 2: To find the average score of a student, for the three test marks (i.e. three subject scores). Algorithm:Step 1: Read the three test scores, say T1, T2 and T3.Step 2:SUM = T1+ T2+ T3.Step 3:AVERAGE= SUM/3.Step 4: Display AverageStep 5: Stop.

  5. Example 3: To find the area of a circle. Algorithm: Step 1: Read the radius of the circle say r.Step 2:AREA = 3.14 * r * r.Step 3: Display Area.Step 5: Stop.

  6. Characteristics of an Algorithm Input: An algorithm must be provided with any number of input/data values. Output: As we go through the algorithm, step by step, processing statements will yield some result. This enables us to verify the algorithm. Hence at least one output must be produced.

  7. Definiteness: Each statement must be very much clear, and distinct. This ensures that the statements must be unambigious. Finiteness: The algorithm must terminate after a finite number of steps.

  8. Let us illustrate this point with the help of an example. Algorithm: Step 1: Let a = 10.Step 2: If (a>10) then go to step 5.Step 3: X = Y * Z.Step 4: Print X and go to step 2.Step 5: Stop.

  9. Let us not bother the purpose of above algorithm, but simply try to trace it. Here we notice that in the algorithm, nowhere the value of ‘a’ is changed, which happens to be controlling the flow and hence never terminates. Such statements must be avoided. Effectiveness: It must be possible in practice, to carry out each step manually (using paper and pencil).

  10. Example 4 Algorithm for dividing two numbers. Algorithm: Step 1: Read the two numbers say a, b. Step 2: c = a/b. Step 3: Print c. Step 4: Stop.

  11. Now what happens to this algorithm, if value of b is 0? • Yes, definitely your answer will be “infinite”, right? • Since the system doesn’t give you such answer, hence a care must be taken while you write the algorithm. One way is to provide an appropriate error message, like:

  12. Modified Algorithm Step 1: Read the two numbers say a, b. Step 2: If (b = 0) then print “denominator value is 0” and go to step 5. Step 3: c = a/ b Step 4: Print c Step 5: Stop

  13. An algorithm can be expressed in many ways. • One way is to express it in natural language (viz, English). • This way of representation helps to write a program in any language, as it needs only the conversion to respective languages. • Thus it appears more general. However, care must be taken in such case to avoid ambiguity. Another way, is to represent algorithm graphically – a flowchart.

  14. Algorithm Design Tools • First write the algorithm for a given problem as if we are solving problem on a paper. 2.Convert each statement of the algorithm into its equivalent program statements and then think of putting the control statement. We can make use of flowcharts for the algorithm design.

  15. Assignment Given a list of five numbers find the largest number in the list.

  16. Flowchart It is a diagrammatic way of representing the steps to be followed for solving the given problem. The main advantage of flowchart is that since it is in the form of diagram one can understand the flow of code very easily. While drawing the flowcharts we make use of certain symbols. These are shown below:

  17. Flowchart Notation used

  18. Flowchart Notation used

  19. Examples on Flowcharts Example 1 Draw a flowchart to add two numbers say ‘a’ and ‘b’. Store the result in sum. Display the Print “sum”.

  20. Start Read numbers ‘a’ and ‘b’ sum = a + b Print the result sum Stop

  21. Example 2 Draw a flowchart to find the factorial of a given number.

  22. Start Read the number = number Yes If number == 0 No Print “ factorial for 0 is 1” Initialize, Fact = 1 i = 1 Stop Fact = fact * i i = i +1 Yes If i<=number Print “factorial of number is “fact” Stop

  23. Assignment Draw a flow chart to search a number from a given list of n numbers.

  24. Pseudocode • Pseudocode generally does not actually obey the syntax rules of any particular language • There is no systematic standard form, although any particular writer will generally borrow style and syntax for example control structures from some conventional programming language.

  25. KEYWORDS Several keywords are used to indicate common input, output and processing operations: Input: Read, Obtain, Get Output: Print, Display, Show Compute: Compute, Calculate, Determine Initialize: Set, Init Add One: Increment, Bump

  26. Example 1 Draw the flowchart and also give the pseudocode to calculate the pay Start Read hours Read rate Pay = hours * rate End

  27. Pseudocode Begin input hours input rate pay = hours * rate print pay End

  28. Assignments Draw the flowchart and the pseudocode for the following problems: • To find the sum of two numbers. 2.To find the average of three numbers.

  29. IF-THEN-ELSE Binary choice on a given Boolean condition is indicated by the use of four keywords: IF, THEN, ELSE, and ENDIF. The general form is: IF condition THEN sequence 1 ELSE sequence 2 ENDIF If the condition is true, sequence 1 is performed, otherwise sequence 2 is performed.

  30. Example Draw the flowchart and also give the pseudocode to calculate the pay with overtime Start Read hours, rate Is hours <= 40 No Yes Pay = hours * rate Pay = 40 * rate + (hours – 40) *1.5 * rate End

  31. Pseudocode Begin input hours, rate if hours <=40 then pay = hours * rate else pay = 40 * rate + (hours – rate) * 1.5 * rate endif print pay End

  32. WHILE The WHILE is used to specify a loop with a test at the top. The beginning and ending of the loop are indicated by two keywords WHILE and ENDWHILE. The general form is WHILE condition sequence ENDWHILE

  33. Example Draw the flowchart and also give the pseudocode to find the average of 10 numbers. Start i=1, sum=0 While i<=10 No Avg = sum / 10 Yes Input x Print avg sum = x + sum Increment i Stop

  34. Pseudocode Begin i=1 sum=0 While i <=10 input x sum = sum + x i = i + 1 end while avg = sum/10 print avg End

  35. CASE A CASE construct indicates a multiway branch based on conditions that are mutually exclusive. Four keywords, CASE, OF, OTHERS, and ENDCASE, and conditions are used to indicate the various alternatives. The general form is: CASE expression OF condition 1 : sequence 1 condition 2 : sequence 2 ... condition n : sequence n OTHERS: default sequence ENDCASE

  36. Example CASE  Title  OF                Mr      : Print "Mister"                Mrs     : Print "Missus"                Miss    : Print "Miss"                Ms      : Print "Mizz"                Dr      : Print "Doctor"ENDCASE CASE  grade  OF                A       : points = 4                B       : points = 3                C       : points = 2                D       : points = 1                F       : points = 0ENDCASE

  37. REPEAT-UNTIL This loop is similar to the WHILE loop except that the test is performed at the bottom of the loop instead of at the top. Two keywords, REPEAT and UNTIL are used. The general form is: REPEAT sequence UNTIL condition

  38. FOR This loop is a specialized construct for iterating a specific number of times, often called a “counting” loop. Two keywords, FOR and ENDFOR are used. The general form is: FOR iteration bounds sequence ENDFOR

  39. Example Write a Pseudode to find the average of 10 numbers using FOR loop. Begin sum = 0 for i = 1 to 10 input x sum = sum + x end for avg = sum / 10 print avg End

  40. Programming Languages • Computers being electronic brains or having artificial intelligence, it is still necessary for humans to convey this sequence of instructions to the computer before the computer can perform the task. • Programming means designing or creating a set of instructions to ask the computer to carry out certain jobs which normally are very much faster than human beings can do. • In order to do programming, we need to use certain computer language to communicate with the computer.

  41. There are many computer languages out there, some of the examples are C, Visual Basic, Fortran, Cobol, Java, C++, Turbo Pascal, Assembly language and etc. • The set of instructions and the order in which they have to be performed is known as an algorithm.

  42. The result of expressing the algorithm in a programming language is called a program. • The process of writing the algorithm using a programming language is called programming, and the person doing this is the programmer.

  43. Storage • Holds the data that the computer will process and the instructions that indicate what processing is to be done. • In a digital computer, these are stored in a form known as binary, which means that each instruction is represented by a series of bits.

  44. Bits are conceptually combined into larger units called bytes (usually 8 bits each) and words (usually 8 to 64 bits each). Types of Storage device • Registers • Main memory • Secondary storage.

  45. Registers • Registers are the fastest and most costly storage units in a computer. • Normally contained within the processing unit • Registers hold data that are involved with the computation currently being performed.

  46. Main Memory • Computer Memory is the internal storage areas in the computer. • It comes in the form of chips capable of holding data. Two Types of Memory • ROM (Read Only Memory) • RAM (Random Access Memory)

  47. ROM (READ ONLY MEMORY) • Date is prerecorded for read only, which cannot be removed. • ROM is non-volatile. • Store critical programs such as the program that boots the computer.

  48. RAM (RANDOM ACCESS MEMORY) • Date can be read, write and remove in any order. • RAM is volatile. CachingA small, fast memory system contains the most frequently used words from a slower, larger main memory.

  49. Secondary Storage • Secondary storage is the slowest, lowest-cost, and highest-capacity computer storage area. • Programs and data are kept in secondary memory when not in immediate use, so that secondary memory is essentially a long-term storage medium.

  50. Machine Language • For the first machines in the 1940s, programmers had no choice but to write in the sequences of digits that the computer executed. • Machine language is a collection of binary digits or bits that the computer reads and interprets. Machine language is the only language a computer is capable of understanding. 

More Related