ENSC 2192 COMPUTER FUNDAMENTALS AND PROGRAMMING DATA vs. INFORMATION DATA – a collection of facts relating to events that take place. These facts could be in the form of numbers, letters, special symbols or combination of these. INFORMATION – data that has been organized and processed so that it is meaningful. It is essential for effective performance of management functions of planning, organizing, directing, and controlling for effective internal operations and external reporting. DATA PROCESSING CYCLE The process of converting data into a useful and meaningful information. INPUT PROCESS OUTPUT THE PROGRAMMING CYCLE 1.) Defining the Problem 2.) Developing Software Requirements 3.) Designing the Program 4.) Coding the Program 5.) Checking out the program by Testing and Debugging 6.) Installing and Maintaining the Program 7.) Documentation Program Elements & Structure Program Elements and Structure 1.) Constants – Literal Value that does not change during program execution. It can be of the numeric or string type. Numeric Constant - may be an integer (whole number) or a real number (with a decimal place). It may be preceded by a positive or negative sign. Examples: 10 -28 +3.4 -0.413 0 201 Note: No other characters must or can appear together with the constants. The following are examples of incorrect numeric constants: 10,000 $14.3 10mi/hr 1/2 String Constant – A character or group of characters enclosed in double quotation marks ( “ ). The numbers 0-9, letters a-z or A-Z and all special characters like a space, % $ - _ & etc. may be included. Examples: “A” “Ace” “.” “1225” “B & B” Note: The entire string constant must always be enclosed in double quotation marks, one before the first character and one after the last character (no more no less). The following are examples of incorrect string constants: “Dia Mahaguay SMART” “Avenger Assemble” 2.) Variables – Is a portion of the computer’s main memory used to store a numeric or string constant. The value stored in the variable must be given a unique variable name but it can only hold one value at a given time. Variable names must conform to the following requirements: a.) Must contain only letters, numbers and/or an underscore. b.) Must begin with a letter. c.) Must end either with a letter or a number. d.) Must be unique that is, not a name of another variable. e.) Must not be a keyword like ACCEPT, WHILE and the like. A variable may be one of the following types: Numeric, String, Array Note: Variable names are not case sensitive, that is, upper and lower case letters are not differentiated. For example the variable TOTAL is the same as total. The following are example of incorrect variable name: Year code 10ctr #5 Total= 3.) Expressions – is a group of program elements consisting of operands and operators. Operands can be a constant, variable, and or an array element. Operators can be arithmetic, logical, relational and/or string. Expressions always yield a value of a certain type depending on the type of expressions used. Arithmetic Expressions Hierarchy or Priority of Operators 1.) Expressions in parenthesis 2.) * / % 3.) + - Examples: 1.) 10 + 4 * 8 32 42 2.) 50 % 6 – 5 2 -3 3.) (20 / 4) * 9 5 45 String Expressions Examples: 1.) “3” + “4” = “34” 2.) “Ja ” + “red” = ”Jared” Examples of incorrect usage of the + operator: “3” + 4 100 + “Peso” Relational Expressions Greater than Less than Greater than or equal to Less than or equal to Not equal Is equal to > < >= <= != == Examples: Ctr1 > 10 3 <= 5 Temp1 != Temp2 Examples of incorrect Relational Expressions: 30 <= X <= 40 43 > < 50 Logical Expressions Hierarchy or Priority of Operators 1.) 2.) 3.) 4.) Expressions in parenthesis Not (!) And (&&) Or (||) TRUTH TABLE FOR NOT(!) X FALSE TRUE !X TRUE FALSE TRUTH TABLE FOR AND(&&) X FALSE FALSE TRUE TRUE Y FALSE TRUE FALSE TRUE X && Y FALSE FALSE FALSE TRUE TRUTH TABLE FOR OR(||) X FALSE FALSE TRUE TRUE Y FALSE TRUE FALSE TRUE X || Y FALSE TRUE TRUE TRUE Examples: 15 >= 10 && 15 <= 20 T && T T 4.) Statements – are keywords, which perform which a predefined procedure. The following statements are available: 1.) 2.) 3.) 4.) 5.) DISPLAY Statement ACCEPT Statement IF Statement WHILE Statement FOR Statement Exercises: 1.) 2.) 3.) 4.) 5.) 6.) 7.) 8.) 9.) 10.) 11.) 12.) 13.) 14.) 15.) 11 % 2 + 3 * 7 –2 100 % 5 % 3 % 2 – (45 / 9) * 6 + 4 (5 + 7) % (10 * 2 – 5) / (15 – (8 – 6 % 4)) (8 + 21 % (18 – 5 * 3)) % (17 – (6 + 13 % 5)) (12 == 12 % 2 * 2) && !(( 5 > 3) && (8<3)) (6 > 3) && (8 != 8) || !(10 <= 10) 15 / 3 == 15 % 3 5 != 11 / 2 Number of enrollees exceeded 5,000 students. HEIGHT is at least 170 cm. REVENUE is at most 80% of SALES. X is either 6, 8 , or 10 X is any number except 1, 2, and 3. X is between 100 and 200. SEATWORK Check if Valid (V) or Invalid (I) 1. Numeric Constant: “0.12345” 2. String Constant: HELLO_TRISHA 3. Arithmetic Expression: (45.9)%(5*2%3) 4. Variable Name: _J_A_R_E_D 5. Relational Expression: (5<=2*2) =10+1 6. Logical Expression: (!(!(!(F&&T)))) 7. Variable Name: G10R3N 8. String Constant: “*&****hello##” 9. Numeric Constant: 1,000,000.98 10. Logical Expression: (!T&&!F)||(!F) Evaluate the Following expressions: 1. 12 % 6 + (2 + 12 * 3) – ( 12 % 13) 2. (10 / 3 + (16 + 3) )– ((5 * 2 % 10) + 2) 3. (T || F) && (!(!F && (6 > 8) || (5< 10) || F) || !T) 4. (T || !F) || (!(!(!F || (3 < 15) || F) || !F) && T) 5. !(!T && !F) || (!T || (12 <= 12) || (T && F)|| F) FLOWCHARTS - A graphic form of an algorithm. - A diagram of a sequence of steps that a program must perform to produce the desired output. - Traditional tool of programmers which allow flexibility in terms of detail to be included. TYPES OF FLOWCHARTS 1.) System Flowchart – present the total picture without worrying about taking care of every detail. 2.) Program Flowcharts – depicts a main segment of a complete computer program. FLOWCHARTING GUIDELINES 1.) Always use the ANSI standard symbols. 2.) Construct your flowcharts to read from top to bottom and left to right. 3.) Do not crisscross flowlines (used labeled connectors and use arrowheads to indicate direction of the flow. 4.) Make the messages and labels in the flowchart symbol direct and descriptive. 5.) Strive for neatness, clarity and simplicity. BASIC FLOWCHARTING SYMBOLS Terminal Symbol Preparation Symbol Processing Symbol Decision Symbol Input/Output Symbol Predefined Process Symbol Onpage Connector Offpage Connector Flow Direction Lines Punched Card Magnetic Tape Online Storage Document Printout Sequential Program Structure For Flowchart, symbols used are the following: Terminal symbol I/O symbol Preparation symbol Direction lines For Pseudocode, keywords used are the following: 1.) Accept Syntax: Accept <variable> Accept <variable1>, …, <variableN> 2.) Display Syntax: Display “<string>” Display <variable> Display <variable1>, …, <variableN> Sequential structure #1] Input a temperature in Celsius and output the corresponding temperature in Fahrenheit. The formula is a follows: Fahrenheit = 9 (Celsius) + 32 5 Flowchart Start F=0 Input C Pseudocode Variables Used: F, C are numeric Begin F = 0 Display ” Input temperature in Celsius: ” F = (9.0/5.0) * C + 32 Accept C F = (9.0 / 5.0) * C + 32 Output F Display “Equivalent temperature in Fahrenheit:”, F End End Sequential structure #2] Pepperoni++ Pizza House charges 10% service charge and 5% sales tax on the gross bill of the customer. Create a flowchart and a pseudocode that would input the gross bill of the customer and the amount given by the customer to the waiter. It must output the customer’s total bill and change (if there’s any). Pseudocode Flowchart Start TBill = 0 change = 0 Variables Used: GBill, Amt, change, TBill are numeric Begin TBill = 0 change = 0 Display ” Input Gross Bill and Amount Given: ” Input GBill, Amt Accept GBill, Amt TBill = Gbill + Gbill *0.1 + Gbill * 0.05 change = Amt - TBill Output TBill, change TBill = GBill + GBill * 0.1 + GBill * 0.05 change = Amt - TBill Display “Total Bill = ”, Tbill Display “Change = ”, change End End Sequential structure #3] Ten young men agreed to purchase a gift worth Php 10,000.00 for their idol: Jessica Sanchez. In addition, they agreed to continue with their plan even if at least one of them drop out. Create a flowchart a pseudocode that would input the number of men who dropped out (assume 0 to 9 only) and output how much more will each have to contribute toward the purchase of the gift. Flowchart Pseudocode Start Variables Used: AC = 0 DO, AC are numeric Begin AC = 0 Input DO Display ” Input number of men who dropped out: ” AC = (1000.00 * DO) / (10-DO) Accept DO AC = (1000.00 * DO) / (10 – DO) Output AC End Display “Additional Contribution =”, AC End Sequential structure #4] Create a flowchart and a pseudocode that would input an integer number and then output its one’s digit or the right most digit. Flowchart Start Pseudocode Variables Used: num,RDigit are numeric RDigit = 0 Begin RDigit = 0 Input num Display ” Input an integer number: ” Accept num RDigit = num % 10 RDigit = num % 10 Display “It’s rightmost digit is ”, RDigit Output RDigit End End Laboratory Exercise: Sequential structure #5] Using Visual Logic, Draw and execute a FCD for Workers at a particular company were given a 15.5% salary increase. Moreover, the increase was retroactive for 2 months, that is, effective two months ago. Create a flowchart and a pseudocode that takes the employee’s old salary as input and then output the amount of retroactive pay (balance) due the employee and his new salary as well. Sequential structure #6] Create a flowchart and a pseudocode that would input numeric values to A, B, C, D and exchange their values such that A goes to B, B goes to C, C goes to D, and D goes to A. SELECTION /CONDITIONAL PROGRAM STRUCTURE For Flowchart, symbols used are the following: Terminal symbol I/O symbol Preparation symbol Direction lines Decision symbol For Pseudocode, keywords used are the following: 1.) Accept Syntax: Accept <variable> Accept <variable1>, …, <variableN> 2.) Display Syntax: Display “<string>” Display <variable> Display <variable1>, …, <variableN> 3.) if-then-else Syntax #1: if <condition> then begin statements . . . end else begin statements . . . end Syntax #2: if <condition1> then begin statements end else if <condition2> then begin statements end else if <conditionN> then begin statements end else begin statements end Selection/Conditional Structure #1] Create a flowchart and a pseudocode that will prompt the user to input two number and output the highest number entered. Flowchart Start Input N1, N2 If N1 > N2 Y Output N1 N Output N2 A End A Pseudocode Variables Used: N1, N2 are numeric Begin Display ” Input two numbers: ” Accept N1, N2 if (N1 > N2) then Display N1 else Display N2 End Selection/Conditional Structure #2] Create a flowchart and a pseudocode that would input an integer number and then indicate whether the number is an even or an odd number. Flowchart Start Input num If num % 2 == 0 Y N Output “odd number” A End Output “even number” A Pseudocode Variable Used: num is numeric Begin Display ” Input an integer number: ” Accept num if (num % 2 == 0) then Display “ The number you’ve entered is an even number” else Display “ The number you’ve entered is an odd number” End Selection/Conditional Structure #3] Juan dela Cruz Restaurant is offering a 20% discount to all customers whose last name is also dela Cruz. Input the last name of the customer and the total amount due to the customer and then output the amount to be paid. Start Flowchart Input Lname, AmtDue Y If Lname == “dela Cruz” N AmtPd = AmtDue - Amtdue*0.2 Output “You have a 20% discount”, AmtPd AmtPd = Amtdue A Output “You don’t have a 20% discount”, AmtPd A End Pseudocode Variables Used: Lname is string AmtPd, AmtDue are numeric Begin Display ” Input customer’s last name and amount due: ” Accept Lname, AmtDue if (Lname == “dela Cruz”) then begin AmtPd = AmtDue – AmtDue *0.2 Display “ You are entitled to a 20% discount ” Display “ Amount to be paid = ”, AmtPd end else begin AmtPd = AmtDue Display “ You are not entitled to a 20% discount ” Display “ Amount to be paid = ”, AmtPd end End Selection/Conditional Structure #4] Workers at Kookaburra Factory have a regular working hours of 40 hours per week and are paid Php 50.00 per hour. However, if the workers rendered more than 40 hours per week, the excess hours are paid 75% more. Create a flowchart and a pseudocode that would input the number of hours rendered by a worker in one week and output his net salary. Flowchart Start Input NHrs If NHrs > 40 N Salary = NHrs *50 Y Salary = 2000 + (NHrs – 40) * 87.50 A Output Salary End A Pseudocode Variables Used: NHrs, Salary are numeric Begin Display ” Input number of hours worked: ” Accept NHrs if (NHrs > 40) then Salary = 2000 + (NHrs – 40) * 87.50 else Salary = NHrs * 50.00 Display “ Your salary = ”, Salary End #5] The fine for an over-speeding violation depends on the speed of the erring driver, as follows: 60 to 75 mph = Php 2000.00 76 mph and above = Php 4000.00 Create a flowchart and a pseudocode to input the car’s speed and then output the fine, if any. #6] In the game of JACK N POY each of the two player choose either a scissors (code X), paper (code P), or stone (code S). If one chooses scissors and the other chooses stone then stone wins. If one chooses paper and the other stone then paper wins. If one chooses paper and the other scissors then scissors wins. If they both choose the same then the result is a tie. Create a flowchart or a pseudocode that will input two character codes corresponding to the object selected and then output either the message “PLAYER 1 WINS” or “PLAYER 2 WINS” #7] Bongga Ka Day Inc. gives additional performance bonuses to its employees every year. The amount is dependent on the number of years of service of the employee as follows: Years of Service Amount of Bonus 5 years and below 50% of monthly salary 6 years to 10 years 100% of monthly salary 11 years to 25 years Php 30,000.00 or twice the monthly salary, whichever is higher. ITERATIVE PROGRAM STRUCTURE For Flowchart, symbols used are the following: Terminal symbol I/O symbol Preparation symbol Direction lines Decision symbol For Pseudo code, keywords used are the following: 1.) Accept 2.) Display 3.) if-then, if-then-else 4.) while-do Syntax: while <condition> do begin statements; end . . . Iterative Program Structure #1] Create a flowchart/pseudo code that will generate the following number series: 10, 5, 9, 10, 8, 15, 7, 20, 6, and 25 Flowchart Start X = 10 Y=5 Output X, Y X=X–1 Y=Y+5 If X >= 6 N End Y Pseudo code Variables Used: X, Y are numeric Begin X = 10 Y=5 while (X >= 6) do begin Display X, Y X=X–1 Y=Y+5 end End Iterative Program Structure #2] Create a flowchart/pseudo code that will generate the following number series: 1, 1, 2, 3, 5, 8, 13, 21, 34, and 55 Flowchart Start X=0 Y=1 Sum = 1 Output Sum Sum = X + Y X=Y Y = Sum If Sum <= 55 N End Y Pseudo code Variables Used: X, Y, Sum are numeric Begin X=0 Y=1 Sum = 1 while (Sum <= 55) do begin Display Sum Sum = X + Y X=Y Y = Sum end End Iterative Program Structure #3] Create a flowchart/pseudo code that will generate the following number series: 1, 2, 4, 7, 11, 16, 22, 29, 37, and 46 Flowchart Start X=1 Y=1 Output Y Y=Y+X X=X+1 If Y <= 46 N End Y Pseudo code Variables Used: X, Y are numeric Begin X=1 Y=1 while (Y <= 46) do begin Display Y Y=Y+X X=X+1 end End Iterative Program Structure #4] Create a flowchart/pseudo code that will input five numbers and output how many of the numbers entered are odd or even. Flowchart A Start ctr = 1 ctre = 0 ctro = 0 ctr = ctr + 1 Y B If ctr <= 5 Accept num N Y If num % 2 == 0 N ctro = ctro + 1 A Output ctro, ctre ctre = ctre + 1 A End B Pseudo code Variables Used: num, ctr, ctro, ctre are numeric Begin ctr = 1 ctro = 0 ctre = 0 while (ctr <= 5) do begin Accept num if (num % 2 == 0) then ctre = ctre + 1 else ctro = ctro + 1 ctr = ctr + 1 end Display ctro, ctre End Iterative Program Structure #5] Create a flowchart/pseudo code that will input five numbers and output how many of the numbers entered are positive or negative. Flowchart A Start ctr = 1 ctrp = 0 ctrn = 0 ctr = ctr + 1 Y B If ctr <= 5 Accept num N Y If num > 0 N ctrn = ctrn + 1 A Output ctrp, ctrn ctrp = ctrp + 1 A End B Pseudo code Variables Used: num, ctr, ctrp, ctrn are numeric Begin ctr = 1 ctrp = 0 ctrn = 0 while (ctr <= 5) do begin Accept num if (num > 0) then ctrp = ctrp + 1 else ctrn = ctrn + 1 ctr = ctr + 1 end Display ctrp, ctrn End Iterative Program Structure #6] Create a flowchart/pseudo code that will input 10 alphabetic characters and output how many of the characters entered were vowels and consonants. Disregard characters that are not alphabetic. #7] Create a flowchart/pseudo code that will input 5 numbers and output the highest number entered. #8] Create a flowchart/pseudo code that will input 6 score for quizzes (0-100). Eliminate the lowest quiz and compute and output the average of the five remaining quizzes.