Computer Programming 1 PDF
Document Details
Uploaded by Deleted User
FEU Alabang, FEU Diliman, FEU Tech
2018
Tags
Summary
These are lecture notes on computer programming, including discussions on algorithms, pseudocode, flowcharts and source code. The document covers various programming concepts and includes practical examples to illustrate the topics.
Full Transcript
COMPUTER PROGRAMMING 1 MODULE 2 PROGRAM LOGIC DESIGN AND FORMULATION Upon completion of this module, you will be able to: Write algorithmic solutions to problems Apply the different program logic design tools in solving computing problems Design, read, understand and construct pro...
COMPUTER PROGRAMMING 1 MODULE 2 PROGRAM LOGIC DESIGN AND FORMULATION Upon completion of this module, you will be able to: Write algorithmic solutions to problems Apply the different program logic design tools in solving computing problems Design, read, understand and construct program flowchart Express algorithms using pseudocode and flowcharts ALGORITHM STEP-BY-STEP METHOD OF SOLVING A PROBLEM Algorithm is a procedure or formula for solving a problem, based on conducting a sequence of specified actions. Algorithm can be expressed using pseudocode or flowchart. Plan the algorithm that will transform the problem’s input into its output. Write the algorithm in the Processing column of the Input- Process-Output (IPO) chart. Each instruction in the algorithm will describe an action that the computer needs to take. Thus, each instruction should start with a verb. Most algorithms begin with instruction to enter the input items into the computer. Next, record instructions to process the input items to achieve the problem’s output. The processing typically involves performing one or more calculations using the input items. Most algorithms end with instruction to display, print, or store the output items. Display, print, and store refer to the computer screen, printer, and a file on a disk, respectively. Example: IPO chart PSEUDOCODE FALSE CODE Pseudocode is called false code because, it has no syntax like any of the programming language and thus can’t be compiled or interpreted by the computer. It is simply an implementation of an algorithm in the form of annotations and informative text written in plain English. It is not standardized. Every programmer has his or her own version, but you will find some similarities among other versions. HOW TO WRITE A PSEUDOCODE? FALSE CODE In Pseudocode, keywords are used to indicate common input- output and processing operations. They are written fully in uppercase. It also used mathematical, relational and logical operators to express arithmetic and logical operations. When writing pseudocode, we assume that the order of execution of the statements is from top to bottom. This changes when using control structures, functions and exception handling. KEYWORDS: START: This is the start of your pseudocode. INPUT, ENTER, READ / GET : This is data retrieved from the user through typing or through an input device. PRINT, OUTPUT, DISPLAY, SHOW: This will show your output to a screen or the relevant output device. KEYWORDS: COMPUTE, CALCULATE, DETERMINE: This is used to calculate the result of an expression. SET, INIT: To initialize values INCREMENT, BUMP: To increase the value of a variable DECREMENT: To reduce the value of a variable COMMON OPERATORS: Assignment: ← or := Comparison (Relational): Example: =, ≠, , ≤, ≥ c ← 2πr, c := 2πr Example: Arithmetic: +, −, ×, /, mod a = b, x ≠ y, m > n, s < t, n1 ≤ n2 Example: Logical: and, or a + b, x – y, m x n, Example: s / t, num mod 2 m > n and s < t m > n or s < t KEYWORDS in CONDITIONAL: During algorithm development, we need statements which evaluate expressions and execute instructions depending on whether the expression evaluated to True or False. Here are some common conditions used in Pseudocode: IF — THEN – ENDIF IF – ELSE IF — ENDIF IF — ELSE IF — ELSE – ENDIF CASE – OTHERS – ENDCASE KEYWORDS in CONDITIONAL: Examples: KEYWORDS in ITERATION/LOOPING: To iterate is to repeat a set of instructions in order to generate a sequence of outcomes. WHILE condition FOR iteration bounds DO sequence sequence sequence ENDWHILE ENDFOR WHILE condition Example: Example: Example: num := 1 FOR num = 1 TO 10 num :=1 WHILE num secondNumber THEN DISPLAY firstNumber(highest), secondNumber(lowest) ELSE DISPLAY secondNumber(highest), firstNumber(lowest) ENDIF Input Processing Output Problem Specification 3: Make a program that will ask letter input letter letter the user to choose from letters matching interpretation A to C then display the Pseudocode equivalent fruit starting from the selected letter using case INPUT letter statement. CASE letter of A or a: PRINT “A is for apple" B or b: PRINT “B is for banana“ C or c: PRINT “C is for cranberry" OTHERS PRINT “Input a valid letter (A, B, C) “ ENDCASE Input Processing Output Problem Specification 5: Make a program that will number sum := sum + sum display the sum of three input number numbers using for loop Pseudocode statement SET sum = 0 FOR numberCounter = 1 to 3 INPUT number CALCULATE sum := sum + number ENDFOR PRINT sum Input Processing Output Problem Specification 4: Make a program that will number sum := sum + sum display the sum of three input number numbers using while loop Pseudocode statement SET numberCounter = 0, sum = 0 WHILE numberCounter < 3 INPUT number CALCULATE sum := sum + number INCREMENT numberCounter ENDWHILE PRINT sum Problem Specification 6: Input Processing Output Make a program that will display number sum := sum + sum the sum of three input numbers number using do-while loop statement Pseudocode SET numberCounter = 0, sum = 0 DO INPUT number CALCULATE sum := sum + number INCREMENT numberCounter WHILE numberCounter < 3 PRINT sum FLOWCHART Flowchart is a pictorial representation of an ordered, step-by-step solution to a problem. Program Flowchart is a diagram that uses a set of standard graphic symbols to represent the sequence of coded instructions fed into a computer, enabling it to perform specified logical and arithmetical operations. SYMBOL MEANING SYMBOL MEANING Initialization Input / Output Decision Flow Lines On-page Terminal Connector Off-page Process Connector Flowcharts can be used to express different structures: sequence selection/conditional loop. Selection Loop Sequence START INPUT PROCESS OUTPUT END In drawing a proper flowchart, all necessary requirements should be listed out in logical order. The flowchart should be clear, neat and easy to follow. There should not be any room for ambiguity in understanding the flowchart. The usual direction of the flow of a procedure or system is from left to right or top to bottom. Only one flow line should come out from a process symbol. Only one flow line should enter a decision symbol, but two or three flow lines, one for each possible answer, should leave the decision symbol. Ensure that the flowchart has a logical start and finish. If the flowchart becomes complex, it is better to use connector symbols to reduce the number of flow lines. Avoid the intersection of flow lines if you want to make it more effective and better way of communication. FLOWCHART EXAMPLES START Ms. Noreen wants a program that calculates and INPUT display the amount she should tip a waiter at a TotalBill, tipPercentage restaurant. Tip can be computed by multiplying the total bill and the tip (using a percentage). tip = TotalBill x Input Processing Output TipPercentage TotalBill tip = TotalBill x tip TipPercentage TipPercentage OUTPUT Pseudocode tip ENTER TotalBill, TipPercentage COMPUTE tip =: TotalBill x TipPercentage DISPLAY tip END START INPUT Input Processing Output firstNumber, secondNumber firstNumber firstNumber > highest number secondNumber secondNumber and lowest number FALSE firstNumber > OUTPUT Pseudocode secondNumber? secondNumber Make a program (highest) firstNumber that will display ENTER firstNumber, secondNumber (lowest) IF firstNumber > secondNumber the lowest and TRUE THEN highest of two DISPLAY firstNumber(highest), OUTPUT integers entered firstNumber A secondNumber(lowest) (highest) by the user. ELSE secondNumber (lowest) DISPLAY secondNumber(highest), A firstNumber(lowest) END ENDIF START Make a program that will determine and display whether the input number is POSITIVE, INPUT N NEGATIVE or ZERO. FALSE FALSE OUTPUT N>0? N y; Input average = (x + y) /2 ; Computation (Process) OUTPUT cout