CSEC INFORMATION TECHNOLOGY



INFORMATION TECHNOLOGY PROBLEM SOLVING AND COMPUTINGCOTROL STRUCTURESSELECTION STATEMENTSIF-THEN and IF-THEN-ELSELESSON OBJECTIVESselect and use appropriate selection statements to solve problemsrepresent selection statements using pseudocode and flowchartuse selection statements to determine the highest and lowest value in a set of valuesdeclare array variables implement arrays using FOR loop ACTIVITY 1Pair Activity: Consider the following fragment of pseudocode, write the output of each IF statement when it would have been executed by the program.a = 100, b = 500, c = 400 IF (a > b) PRINT bIF (c <> b) PRINT “Values are unequal” ELSE PRINT “Values are equal”IF (b >= c) PRINT aIF (a <c) AND (c > b) PRINT “Hello” IF (a <c) AND (c > b) PRINT “Hello” ELSE “HurrayIF (a <c) OR (c > b) PRINT “Hello”ACTIVITY 2: Develop a psuedocode read in TWO unique integer values, call n1, n2. The program should determine and print the largest number entered. ACTIVITY 3: Develop a psuedocode read in FOUR unique integer values, call n1, n2,n3, n4. The program should determine and print the largest number entered. HOMEWORKWrite a program to accept the name and test score of 10 students. The program must print the average score. The algorithm must prompt and read in each student’s data separatelyPOSSIBLE SOLUTION TO ACTIVITY 3Develop a psuedocode read in FOUR unique integer values, call NUM1, NUM2, NUM3, NUM4. The program should determine and print the largest number entered.Most Efficient Solution is to set one of the values to be the largest and then compare the other values to largest.Pseudocode FINDINGLARGESTThis program will determine and print the largest of four numbersDeclare variables NUM1, NUM2, NUM3, NUM4, largest as integers;BEGIN PRINT “Enter four numbers separated by a space and then press the enter key”READ NUM1, NUM2, NUM3, NUM4//set one of the numbers to be the largest largest = NUM1 IF (NUM2 > largest) THEN largest = NUM2 ENDIF IF (NUM3 > largest) THEN largest = NUM3 ENDIF IF (NUM4 > largest ) THEN largest = NUM4 ENDIFPRINT largest, “is the largest value”END //end of programExplaining the logics of the program above: Let say the user enters 3, 8, 5, 10The program will first see the first number, which is 3, as the largest. The program will then move to first IF statement and compare the second number, 8, to what is assigned as the largest, which is 3. If num2, which is 8, is greater than the largest, then it will be the largest. In this example, 8 will become the new largest because it greater than 3.The program then moves to the second IF statement, and compares the third number to the largest, if the third number is larger, then it will be the largest. In this example, the third number is not larger, so the second number, 8, remains the largest. The program then moves to the third IF statement and compares the fourth number to what is assigned as the largest value. If the fourth number is larger, then it will be stored as the largest. In this example, the fourth is larger, so it will be stored as the largest. The numbers 3, 8, 5, 10 are sample values that a user may enter into the program. Please note that it can be any four random numbers. INFORMATION TECHNOLOGY PROBLEM SOLVING AND COMPUTINGCOTROL STRUCTURESREPETITION OR LOOPINGVARIABLE INITIALIZATION / ASSIGNMENTAssignment is the physical act of placing a value into a variable. Assignment can be shown usingset = 5;set = num + set;The left side is the variable a value is being stored in and the right side is where the variable is being accessed. When a variable is assigned a value, the old value is written over with the new value so the old value is gone. x = 5 does not mean that x is equal to 5; it means set the variable x to have the value 5. Give x the value 5, make x equal to 5.ARRAY DATA STRUCTUREUSING ARRAYS – An array is a structure which acts as a large variable that can store a fixed number of data of the same data type.When to use arrays – Instead of declaring multiple variables to store a series data on the same thing(entity), use one big storage location called array. For example, you are ask to write a program to accept the id_number of 50 employees. Which would you use? 50 individual variables or one variable that will make space for the 50 id_numbers? Use the most efficient solution. Declaring a variable arraySyntax: Declare arrayname: array[startvalue two dots endvalue] of data typeThe array name is like a variable name, which should match what the array will store.E.g. You are asked to store the firstname and lastname of 30 students in a class.Declare array as firstname: array [1..30] of string //all first name are store in the same columnDeclare array as lastname: array [1..30] of string //all first name are store in the same columnREPITITION/LOOPING Repetition is a construct that allows instructions to be executed multiple times (IE repeated).The repetitive performance of the same statements is called looping. Examples of repetitive constructs are:FOR – This is used when the number of iterations or the number of input is known to the programmerWHILE - This is used when the number of iteration is known or when the number of inputs is known.In a repetition problem– Count is initialized– Tested– incremented – increase by 1The structure of the FOR constructThe FOR construct consist of:1. The word FOR2. The Loop variable3. The Equal sign4. An initial (start) value5. The word TO6. The final (END) value7. The word DO8. One or more statements to be executed each time through the loop9. The word ENDFOR indicating the end of the construct.NOTE: The part of the construct between the words FOR and DO is called the control part. This is what determines how many times the loop will be executed. The loopvariable is also called the control variable.386715145780PSEUDOCODE STUDENTThis pseudocode will accept the name and score of 10 studentsDeclare array as names: array [1..10] of string Declare array as scores: array [1..10] of realDeclare variable count as integerBEGINFOR (count = 1 TO 10) DOBEGINPRINT “Enter name”READ names[count]PRINT “Enter score”READ scores[count] ENDFOR ENDPSEUDOCODE STUDENTThis pseudocode will accept the name and score of 10 studentsDeclare array as names: array [1..10] of string Declare array as scores: array [1..10] of realDeclare variable count as integerBEGINFOR (count = 1 TO 10) DOBEGINPRINT “Enter name”READ names[count]PRINT “Enter score”READ scores[count] ENDFOR END-97155203930PSEUDOCODE STUDENTThis pseudocode will accept the name and score of 10 studentsDeclare variable N1,N2,N3,N4,N5,N6,N7,N8,N9,N10 as string Declare variable S1,S2,S3,S4,S5,S5,S7,S8,S9,S10 as realBEGINPRINT “Enter name 1:”READ N1PRINT “Enter score 1”READ S1PRINT “Enter name 2:”READ N2PRINT “Enter score 2”READ S2PRINT “Enter name 3:”READ N3PRINT “Enter score 3”READ S3PRINT “Enter name 2:”READ N4PRINT “Enter score 4”READ S4PRINT “Enter name 5:”READ N5PRINT “Enter score 5”READ S5PRINT “Enter name 6:”READ N6PRINT “Enter score 6”READ S6PRINT “Enter name 7:”READ N7PRINT “Enter score 7”READ S7PRINT “Enter name 8:”READ N8PRINT “Enter score 8”READ S8PRINT “Enter name 9:”READ N9PRINT “Enter score 9”READ S9PRINT “Enter name 10:”READ N10PRINT “Enter score 10”READ S10 END00PSEUDOCODE STUDENTThis pseudocode will accept the name and score of 10 studentsDeclare variable N1,N2,N3,N4,N5,N6,N7,N8,N9,N10 as string Declare variable S1,S2,S3,S4,S5,S5,S7,S8,S9,S10 as realBEGINPRINT “Enter name 1:”READ N1PRINT “Enter score 1”READ S1PRINT “Enter name 2:”READ N2PRINT “Enter score 2”READ S2PRINT “Enter name 3:”READ N3PRINT “Enter score 3”READ S3PRINT “Enter name 2:”READ N4PRINT “Enter score 4”READ S4PRINT “Enter name 5:”READ N5PRINT “Enter score 5”READ S5PRINT “Enter name 6:”READ N6PRINT “Enter score 6”READ S6PRINT “Enter name 7:”READ N7PRINT “Enter score 7”READ S7PRINT “Enter name 8:”READ N8PRINT “Enter score 8”READ S8PRINT “Enter name 9:”READ N9PRINT “Enter score 9”READ S9PRINT “Enter name 10:”READ N10PRINT “Enter score 10”READ S10 ENDThe program in the box on the right solves the long program codes shown on the left.313499526711PSEUDOCODE STUDENTThis pseudocode will accept the name and score of 10 studentsDeclare array as names: array [1..10] of string Declare array as scores: array [1..10] of realDeclare variable count as integerBEGINFOR (count = 1 TO 10) DOBEGINPRINT “Enter name”READ names[count]PRINT “Enter score”READ scores[count] ENDFOR ENDPSEUDOCODE STUDENTThis pseudocode will accept the name and score of 10 studentsDeclare array as names: array [1..10] of string Declare array as scores: array [1..10] of realDeclare variable count as integerBEGINFOR (count = 1 TO 10) DOBEGINPRINT “Enter name”READ names[count]PRINT “Enter score”READ scores[count] ENDFOR ENDPRINT “Enter name 1:”READ N1PRINT “Enter score 1”READ S1PRINT “Enter name 2:”READ N2PRINT “Enter score 2”READ S2PRINT “Enter name 3:”READ N3PRINT “Enter score 3”READ S33131928129784Which solution would you use?The one on the right or the one on the left?00Which solution would you use?The one on the right or the one on the left?PRINT “Enter name 2:”READ N4PRINT “Enter score 4”READ S4PRINT “Enter name 5:”READ N5PRINT “Enter score 5”READ S53249038116461Programming tip:When you need to develop programs to process data for multiple entity, (for example, name and age of 20 people, size and weight of 20 puppies, etc.) do not waste time declaring multiple variables, use an ARRAY. Additionally, an array needs a FOR loop to keep track of its index of location.DON’T FORGET YOUR FOR LOOP STRUCTURE00Programming tip:When you need to develop programs to process data for multiple entity, (for example, name and age of 20 people, size and weight of 20 puppies, etc.) do not waste time declaring multiple variables, use an ARRAY. Additionally, an array needs a FOR loop to keep track of its index of location.DON’T FORGET YOUR FOR LOOP STRUCTUREREADING VALUES INTO AN ARRAYYou must use a FOR loop when you use an array. Input values in an array is similar to inputting values in a single variable. Each location in array is called an index or position.For example, when you create an array to store 10 scores which are integers, you are telling the memory make space for 10 values, which will be beside each other. A logical view of the memory will the array would be Score: array [1..10] of integer1234567891050907889675789901256 Index ElementWhen you want to read or store values in a location in an array, you must specify the name of the array and the index. See the format below:READ array_name [index] E.g. READ score [count]Since a FOR loop will lessen the number of instructions by repetition, the loop control variable in the array is storing each iteration. When you instruct the computer to repeat from 1 to 10 by writing FOR (count = 1 TO 10) DO, whatever is written between DO and ENDFOR will be executed x times. In this case, the instructions will be repeated 10 times. And on each repetition, the user has the opportunity to enter different values.When the FOR loop executes the first time, the value 1 will be in the variable count, which is pointing to the first index of the array and the code READ score[count] will have the value 50. When the FOR loop executes the second time, the value 2 will be in the variable count, which is pointing to the seconding index of the array and the code READ score[count] will have the value 90. This process continues until it stops after the loop has finish its 10th repetition. Since a loop (FOR or WHILE) will keep a count of the repetition, the array would then depend on the loop control variable to indicate the index or the location in which to store the value entered by the user. PRINTING VALUES STORED IN AN ARRAYSyntax:PRINT arrayname [index] //the loop control variable is usually the indexExamplePRINT “THE LIST OF SCORES IS:”FOR (count = 0 TO 10) DOPRINT score[count]ENDFOROnce you have accepted the values in an array using the FOR loop, you would end that FOR loop and then implement another FOR loop to print the elements. See example below:-369800-595576PSEUDOCODE STUDENTThis pseudocode will accept the name and score of 10 studentsDeclare array as names: array [1..10] of string Declare array as scores: array [1..10] of realDeclare variable count as integerBEGIN//accepting the data in the list FOR (count = 1 TO 10) DOBEGINPRINT “Enter name”READ names[count]PRINT “Enter score”READ scores[count] ENDFOR// printing the list PRINT “The list is:”FOR (count = 1 TO 10) DO PRINT names[count], “ ……….” , scores[count] ENDFOR END00PSEUDOCODE STUDENTThis pseudocode will accept the name and score of 10 studentsDeclare array as names: array [1..10] of string Declare array as scores: array [1..10] of realDeclare variable count as integerBEGIN//accepting the data in the list FOR (count = 1 TO 10) DOBEGINPRINT “Enter name”READ names[count]PRINT “Enter score”READ scores[count] ENDFOR// printing the list PRINT “The list is:”FOR (count = 1 TO 10) DO PRINT names[count], “ ……….” , scores[count] ENDFOR ENDCOUNTING350103914348Did you know?You can keep track of a process each time a set of instruction in repeated?For example:counting the number of persons in a classcounting the number of person who failscalculating the sum of scorescalculating the average scor00Did you know?You can keep track of a process each time a set of instruction in repeated?For example:counting the number of persons in a classcounting the number of person who failscalculating the sum of scorescalculating the average scorCounting involves increasing the value by a fixed amount repeatedly. The counting is done by the computer and not by the user. A counter instruction performs a count. The instruction is written as counter_variable = counter_variable + 1The counter variable must be given an initial value before the counter instruction is carried out so as to facilitate calculation. The initial value is 0 because we start counting from 1. Variable names for counters should be chosen to reflect what is being counted. So if you were asked to count the number of students’ name entered in the program, you would use student_counter as a variable. If you were asked to calculate the sum of scores entered for 20 students, you could usetotal = 0total = total + score;Let’s look at how you calculate a sum of 20 scores in a program080294PSEUDOCODE STUDENTThis pseudocode will accept the name and score of 10 studentsDeclare array as names: array [1..10] of string Declare array as scores: array [1..10] of realDeclare variable count, total as integerDeclare variable avg as realBEGINtotal = 0 //initializing total to 0FOR (count = 1 TO 10) DOBEGINPRINT “Enter name”READ names[count]PRINT “Enter score”READ scores[count]total = total + score [count] ENDFOR PRINT “The total is:”, totalEND00PSEUDOCODE STUDENTThis pseudocode will accept the name and score of 10 studentsDeclare array as names: array [1..10] of string Declare array as scores: array [1..10] of realDeclare variable count, total as integerDeclare variable avg as realBEGINtotal = 0 //initializing total to 0FOR (count = 1 TO 10) DOBEGINPRINT “Enter name”READ names[count]PRINT “Enter score”READ scores[count]total = total + score [count] ENDFOR PRINT “The total is:”, totalENDSUMMARYUse IF-THEN selection statement when you only have an action to perform if the condition in trueUse IF-THEN-ELSE selection statement when you have an action to perform if the condition in true and another action to be performed in the condition is false.Use IF-THEN-ELSE-IF when you have multiple conditions to be tested. Use an array to lessen the number of individual elements for an entityUse a FOR loop to keep track of the array indexUse a FOR loop to lessen the number of instructions in your program Use a FOR loop when you know the exact number of data to collect. For example, if you are asked to write a program to accept the age of 15 students as opposed to being as to write a program to accept the age of a number of students.Initialize counter variables to zero, since it will accumulated after each loop. The words repetition, loop and iteration are used interchangeably ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download