Chapter 1. Overview of Computer Programming 1 Overview of Computer Programming Three major operations in programs: • Input : Data items enter the computer system and are put into memory, where they can be processed. • Process : processing data items may involve organizing or sorting them, checking them for accuracy, or performing calculations with them. • Output : after data items have been processed, the resulting information usually is sent to a printer, monitor, or some other output device so people can view, interpret, and use the results. Input Process Output 2 Program Development Cycle • Understand the problem • Plan the logic • Write the code • Translate the code • Test the program • Put the program into production • Maintain the program 3 Understand the problem & Plan the logic • Problem – multiply the user input with 2. • Input -> number • Process -> number * 2 • Output-> result Accept number from the user Multiply the number with 2 Display the result 4 Plan the logic • Use one of two tools • Pseudocode - is an English-like representation of the logical steps it takes to solve a problem. • Flowchart - is a pictorial representation of the same thing. Pseudocode Flowchart Start input number set answer = number * 2 output answer stop 5 Pseudocode Statements • Pseudocode : is an English-like representation of the logical steps it takes to solve a problem. • Sample pseudocode is Start input number set answer = number * 2 output answer stop 6 Flowchart and its symbols • Flowchart is a pictorial representation of the logical steps it takes to solve a problem. • Flowchart symbols are: 1) Input symbol input number 2) Processing symbol set answer = number * 2 3) Output symbol output answer 4) Flowline : use arrow to connect the steps, most of a flowchart should read from top to bottom or from left to right on a page. 7 Flowchart and its symbols (cont’d) • Existing flowchart symbols are – • Terminal symbols • Decision symbol Start Stop number = 0? • Other symbols are presented in next Topic. 8 Exercise : 1 1. Input A. 2. Processing B. 3. Output C. 4. Decision D. 5. Terminal E. 9 Exercise : 2 1. Draw a flowchart and write pseudocode to represent the logic of a program that allows the user to enter a value. The program multiplies the value by 10 and outputs the result. 2. Draw a flowchart and write pseudocode to represent the logic of a program that allows the user to enter a value for the radius of a circle. The program calculates the diameter by multiplying the radius by 2, and then calculates the circumference by multiplying the diameter by 3.14. The program outputs both the diameter and the circumference. 10 Working with Variables • Variables are named memory locations whose contents can vary or differ over time. • A declaration is a statement that provides a data type and an identifier for a variable. • A data item’s data type is a classification that describes the following: • What values can be held by the item • How the item is stored in computer memory • What operations can be performed on the data item 11 Initializing the variable • Declaring a starting value is known as initializing the variable. Pseudocode Start Flowchart Declarations num number num answer input number set answer = number * 2 output answer stop 12 Naming variable • The variable names used throughout this book following rules: • Variable names must be one word. Eg. hourly_wage, num, answer, hour1, hour2, etc. • Variable names should have some appropriate meaning. Eg. rate_of_return, interest • Variable names must be started with letter, and then follows letters, digits and (_). • The variable should be used camel casing Eg. hourlyWage, rateOfRetrun, etc. 13 Data types of Variables • Variables can also be different data types – • A numeric variable is one that can hold digits and have mathematical operations performed on it. Eg. num age; • A string variable can hold text, such as letters of the alphabet, and other special characters, such as punctuation marks. Eg. string name Problem 1 • Adding two numbers. • Input -> number1, number2 • Process -> answer = number1 + number2 • Output -> answer 14 Problem 1 (con’t) Pseudocode Start Flowchart Declarations num number1, number2 num answer input number1, number2 set answer = number1 + number2 output answer stop 15 Writing clear Prompts and Echoing input Pseudocode Start Sample output Declarations -> Enter a name : Mya Mya string name -> Enter age for Mya Mya 34 num age output “Enter a name : ” Prompt input name output “Enter age for ”,name Echoing input input age stop Draw a flow chart for given Pseudocode. 16 Declaring Named Constants • Pseudocode • Besides variables, most programming languages Start allow you to create Declarations named constants. num radius, area • A named constant is Constant similar to a variable, num PI=3.14 variable except it can be assigned output “Enter a radius for a a value only once. Circle:” input radius set area = PI*radius*radius output “Circle’s Area: ”,area stop Draw a flow chart for given Pseudocode. 17 Operators • Arithmetic operators +, -, *, /, %, • Assignment operator = • Arithmetic Assignment operator +=, -=, *=, /=, %= • Relational operators >, >=, <, <=, ==, <> (not equal) Operators Assignment Symbol Precedence Associativity *, /, % Highest Left to right +, - Left to right Relational >, >=, <, <=, ==, <> Left to right Assignment = Lowest Right to left Eg. a = 20, b= 45, c=23, d=4, e= 5, f=13 answer = a + b + c * d / e – f = 20 + 45 + 23 * 4 / 5 – 13 = 20 + 45 + 92 / 5 – 13 = 20 + 45 + 18 – 13 = 65 + 18 – 13 = 83 – 13 = 70 18 Exercise 3 Assume that cost = 10 and price = 12. What is the value of each of the following expressions? a. price — cost * 2 b. 15 + price — 3 * 2 c. (price + cost) * 3 d. 4 — 3 * 2 + cost e. cost * ((price — 8) + 5) + 100 19 Exercise 4 1. Draw a flowchart and write pseudocode to represent the logic of a program that allows the user to enter a Celsius degree, calculate the corresponding Fahrenheit degree and outputs the result. 2. Draw a flowchart and write pseudocode to represent the logic of a program that calculate the area of triangle and then output the result. 3. Draw a flowchart and write pseudocode to represent the logic of a program that allows the user to enter two numbers, calculate the quotient and remainder and then output the result. 20 Chapter 2. Making Decision 21 Making Decision Understanding the Three Basic Structures 22 Selection (Decision) Structures • With this structure, you ask a question and, depending on the answer, you take one of two courses of action. • It has • Single – alternative selection (if statement) • Dual – alternative selection (if-then-else statement) • Multiple – alternative selection (if-else if-….- else statement) 23 Single – alternative selection structure Pseudocode if condition is true then Flowchart do process endif Condition ? Process 24 Single – alternative selection structure (Eg.) • Problem – guessing a number that is predefined in the program. Pseudocode Flowchart start Declarations num number output “Enter a number:” input number if number == 34 then output “You Win” endif stop 25 Dual – alternative selection structure Pseudocode Flowchart if condition is true then do Process1 else Condition ? do Process2 endif Process2 Process1 26 Dual – alternative selection structure (Eg.) • Problem – accept two numbers and then display the maximum number. Pseudocode Flowchart start Declarations num, num1, num2 output “Enter two numbers : ” input num1, num2 if num1>num2 then output “Maximum number is ”,num1 else output “Maximum number is ”,num2 endif stop 27 Multiple – alternative selection structure Pseudocode Flowchart if condition1 is true then do Statement 1 else if condition2 is true then do Statement2 else do Statement3 endif 28 Multiple – alternative selection structure (Eg.) Pseudocode start Declarations num age output “Enter Age : ” input age if age <= 0 then output “Wrong input” else if age > 150 then output “Wrong input” else output “Your age : ”,age endif Stop • Draw a flowchart for given pseudocode. 29 Exercise 5 1. Draw a flowchart and write pseudocode to represent the logic of a program that ask the user to enter a mark and then calculate the grade based on the following condition. Mark >= 80 – “Distinction” Mark < 80 and Mark >= 65 – “Credit” Mark < 65 and Mark >=40 – “Pass” Otherwise – “Fail” 2. Draw a flowchart and write pseudocode to represent the logic of a program that calculate salary for one week based on following factsTotal working hours for one week – 40 hours Basic payment for one hour- $3.5 Overtime rate for one hour – 1.5 of basic payment of one hour 30 Exercise 6 1. Draw a flowchart and write pseudocode to represent the logic of a program that accept number from the user and then display that number is even or odd. The number : 13 Your entered number is odd. 2. Draw a flowchart and write pseudocode to represent the logic of a program that accept two numbers and operator from the user and then display the corresponding result. First number : 23 Second number : 10 Operator : / 23 / 10 = 2.3 31 Looping Loop structure Pseudocode Flowchart while condition is true do Process endwhile Condition ? Process 32 Loop structure (Eg.) • Problem – Addition of first ten numbers. 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 start step(+1) sum = 0; sum = sum + 1; // sum =1 sum = sum + 2; // sum =3 and so on. end start i = 1 stop i <= 10 step i=i+1 (increment) sum = 0 sum = sum + i 33 Loop structure (Eg.) Pseudocode Flowchart start Declarations start num i = 1 num sum = 0 stop while i <= 10 do sum = sum + i step (increment) i = i + 1 end while output “Sum of first ten numbers is ”,sum stop 34 Loop structure (Eg.) Pseudocode (using while loop) Pseudocode (using for loop) start start Declarations Declarations num i = 1 num i num sum = 0 num sum = 0 while i <= 10 for i=1 to 10 step 1 sum = sum + i sum = sum + i i = i + 1 end for end while output “Sum of first output “Sum of first ten numbers is ”, sum stop ten numbers is ”, sum stop 35 Loop structure (Eg.) • Pseudocode (using while loop) Desk - checking Start Declarations num i = 1 num sum = 0 while i <= 10 sum = sum + i i = i + 1 end while output “Sum of first ten numbers is ”, sum stop 36 Problem 2 • Addition of two numbers until user wants • Sample output is Enter two numbers : 23 34 Sum of your numbers : 57 Do you want to continue (yes or no) : yes Enter two numbers : 3 24 Sum of your numbers : 27 Do you want to continue (yes or no) : no End of the program 37 Problem 2 • Pseudocode start Declarations num num1, num2, ans string choice = “yes” while choice == “yes” do output “Enter two numbers : ” input num1, num2 set ans = num1 + num2 output “Do you want to continue (yes or no) : ” input choice end while output “End of the program” Stop 38 Problem 2 • Flowchart 39 Exercise 7 1. Draw a flowchart and write pseudocode to represent the logic of a program that accept two numbers and operator from the user, calculate the result based on operator and then, ask the user to continue next time to calculate or not. Sample output is First number : 33 Second Number : 6 Operator : % 33 % 6 = 3 Continue or not (y/n) : n 2. Draw a flowchart and write pseudocode to represent the logic of a program that ask the user to enter a number and then calculate the factorial for that number. 3. Draw a flowchart and write pseudocode to represent the logic of a program that ask the user to enter a number and power data and then calculate the power of that number. 40 start Declarations num num1, num2, result string op string choice = "y" while choice == "y" do output "First number : " input num1 output "Second Number : " input num2 output "Operator : " input op if op == "+" then set result = num1 + num2 else if op == "-" then set result = num1 - num2 else if op == "*" then set result = num1 * num2 else if op == "/" then set result = num1 / num2 else if op == "%" then set result = num1 % num2 else output "Invalid operator" endif output num1, " ", op, " ", num2, " = ", result output "Continue or not (y/n) : " input choice end while output "End of the program" stop start Declarations num number num i = 1 num factorial = 1 output "Enter a number : " input number while i <= number do set factorial = factorial * i set i = i + 1 end while output "Factorial of ", number, " is ", factorial stop start Declarations num base, exponent num i = 1 num result = 1 output "Enter a number : " input base output "Enter power : " input exponent while i <= exponent do set result = result * base set i = i + 1 end while output base, " ^ ", exponent, " = ", result stop 41 Chapter 3. Data Structure 42 Data Structure • “Data” is information handled inside a computer. • “Data structure” is how to handle the data systematically. • In system development, a design of data structure is the foundation of everything. • It is necessary to examine and design data structure beforehand in order to execute target tasks. • Basic data structure is as follows: (1) Variable • “Variable” is an area to temporarily store data handled in a program. • When a variable is defined, a variable name is given by using alphanumeric letters and symbols in order to distinguish it from other data. 43 Data Structure • Also, when a variable is used, a value is assigned to the variable. For example: a=10 y= a + 10 (2) Field type • “Field type” is a data type to be stored. • It is also called a “data type.” • For data handled in a program, a field type such as a numeric value or a character string is defined. • When a field type is defined to a variable, the program’s accuracy improves because only appropriate data can be assigned. 44 Data Structure (3) Array • In order to handle a large amount of data, it is convenient to utilize a data structure called an “array” instead of variables. • A variable stores a data; however, an array can store the same type of multiple data in one or more rows. • Generally, data are consecutively stored in an array, with a “subscript” to identify individual data. • Therefore, specific data can be searched and data can be extracted in the order of a specified sequence. 45 Data Structure (4) List • A list is a data structure where sparse multiple data are linked together. • Unlike an array, data may not be stored consecutively. • Along with data, each member of a list has information called a “pointer” that indicates the stored location of the next member. • When a link is switched or a new member is added, the way members are linked can be redefined by changing a pointer. 46 Stack and Queue • Two ways of thinking for data insertion and deletion in a list are shown below. (1) Stack • “Stack” is a method to insert data into the end of a list, and to delete data inserted last. • It is also called a “LIFO list.” (“LIFO” is the last-in first-out method.) • The basic syntax of stack is as follows. 47 Stack and Queue (2) Queue • “Queue” is a method to insert data into the end of a list, and to delete data inserted first. • It is also called “scheduled queue” or “FIFO list.” (FIFO” is a first-in first-out method.) • The basic syntax of queue is as follows. 48 Stack and Queue Example: As shown in the figure, boxes marked with a number are piled up. These boxes are piled up according to Operations 1 through 4. After all operations have been performed, what is the total when the numbers marked on all boxes are added? Note that 5 boxes are always in the piled condition, and a box can be extracted from the bottom only. Also, when putting a new box on the top, discard the box at the bottom. 49 Stack and Queue 50 Stack and Queue 51 Stack and Queue 52 Chapter 4. Algorithm 53 Algorithm • “Algorithm” are processing steps to solve a problem. • When system development or work analysis is conducted, the algorithm is the first thing to figure out. • By clarifying steps through an algorithm, a program can be created efficiently. • “Flowchart” is a diagram to graphically describe workflow or program operations by using symbols and arrows. • In a flowchart, a data route and control can be also shown in addition to a program operation. • Therefore, it is utilized as a method to visualize algorithms. 54 Algorithm (1) Symbols in flowchart • Symbols in a flowchart are defined by ISO (International Standardization Organization). • Representative symbols are as follows. 55 Algorithm (2) Basic structures of algorithm • There are “sequential structure,” “selection structure,” and “repetition structure” in the basic structures of algorithm. • By combining them, a complicated algorithm can be described. (i) Sequential structure • The “sequential structure” shows a flow in sequential order. 56 Algorithm (ii) Selection structure • The “selection structure” shows a flow to select a process on the basis of a given condition. 57 Algorithm Example: What does a flowchart of a date input system considering a leap year look like? A leap year is defined as follows: When an AD year can be divided by 4 without a remainder, the year is a leap year. However, when the year can be also divided by 100 without a remainder, the year is not a leap year. When the year can be divided by 100 and 400 without a remainder, the year is a leap year. A flowchart created on the basis of the definition on a leap year will be as follows. 58 Algorithm 59 Algorithm (iii) Repetition structure • The “repetition structure” shows a flow to repeat a process while or until a condition is satisfied, e.g. a specified number of times is reached. 60 Algorithm Example: When the variable a is 3 and the process shown in the flowchart below is executed, what is the value of y? 61 Algorithm When the process is actually executed: [1] Assign 3 to x. [2] 3 × 2 = 6, therefore assign 6 to x. [3] x is 12 or less, go to [4]. [4] 6 + 1 = 7, therefore assign 7 to x. [2] 7 × 2 = 14, therefore assign 14 to x. [3] x is more than 12; therefore, go to [5]. [5] 14 − 12 = 2, therefore assign 2 to y. Then the value of y becomes 2. 62
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )