🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Standard Methods of Solution_3.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

HPC Computer Science HPC INTRODUCTION TO COMPUTER PROGRAMMING 1. Definition of Key Terms 1.1 Program A program is a set of instructions which tell the computer to carry out a particular task and how to carry it out. Examples: Firefox, Microsoft word, Zoom, games 1.2 Software Software is a collec...

HPC Computer Science HPC INTRODUCTION TO COMPUTER PROGRAMMING 1. Definition of Key Terms 1.1 Program A program is a set of instructions which tell the computer to carry out a particular task and how to carry it out. Examples: Firefox, Microsoft word, Zoom, games 1.2 Software Software is a collective term referring to one or more programs 1.3 Programming Language A programming language is software that is used by programmers to write computer programs. Examples are: Visual Basic, Python, Java, C, C++ 1.5 Syntax Syntax refers to the rules of a particular programming language. Each programming language has its own rules that are specific to it. For example to output in Visual Basic we use the key words: Console.writeline whereas to output in C++ we use the keyword Cout= greater than or equal to not equal to The result of these operations is always of data type BOOLEAN. In complex expressions, it is advisable to use parentheses to make the order of operations explicit. Boolean operators The only Boolean operators used are AND, OR and NOT. The operands and results of these operations are always of data type BOOLEAN. Examples – Boolean operations IF Answer < 0 OR Answer > 100 THEN Correct ← FALSE ELSE Correct ← TRUE ENDIF String operations LENGTH() Returns the integer value representing the length of string. The identifier should be of data type string. LCASE() Returns the string/character with all characters in lower case. The identifier should be of data type string or char. UCASE() Returns the string/character with all characters in upper case. The identifier should be of data type string or char. SUBSTRING(, , ) Returns a string of length length starting at position start. The identifier should be of data type string, length and start should be positive and data type integer. Example – string operations LENGTH("Happy Days") will return 10 LCASE(ꞌWꞌ) will return ꞌwꞌ UCASE("Happy") will return "HAPPY" SUBSTRING("Happy Days", 1, 5) will return "Happy" Array: An array is a data structure for storage of multiple values under the same identifier/name. Values stored in an array must be of the same datatype. Array Terms: 1. Identifier/Name – An array has a name which is used for all the elements in the array. E.g StudentMarks 2. Element – the individual items in the array are called elements of an array 3. Index – each array space is identified with an integer value e.g StudentMarks(5). 5 is the index for the 5th or 6th space in the array depending on the lowerbound. 4. Lowerbound – Is the index of the first space or element in the array. It is usually a 0 or a 1 5. Upperbound – Is the index of the last space or element in the array. 6. Size - Number of elements in an array. You can obtain it by 1. Counting the number of elements, 2. It will be given, 3. Check the Lowerbound and the Upperbound 7. Dimension – number of rows and number of columns that an array has. e.g 1D Array, 2D Array 8. Datatype – Elements of an array must all be of the same datatype. Declaration of a 1D array When declaring an array you specify the name/identifier, the Lower and Upper Bounds and the Datatype. Psuedocode declaration of a Student Names array: DECLARE StudentNames : ARRAY[1:20] OF STRING DECLARE StudentNames : ARRAY[0:19] OF STRING DECLARE StudentNames[1:20] : STRING Inputting and Outputting values in an array Because an array stores multiple values we need a loop in order to read or write values onto it. A loop’s COUNT matches and therefore points to the array’s INDEX. For example: Write a program that asks the user to input 6 students names onto an array and display them. 3.2 Initialization: Initialisation is giving a starting (Initial) value to a variable that will be used in a calculation such as: Total, Count, Highest, Lowest, Average If an algorithm needs to read the value of a variable before it assigns input data or a calculated value to the variable, the algorithm should assign an appropriate initial value to the variable, known as Initialization. Initialising Total Total must always be initialised to 0 e.g Total0 Sum0 TotalAmount0 Initialising Count Count can be initialised to 0 or to 1 e.g: Count0 Count1 NoofPasses0 Initialising Maximum/Highest Maximum for a KNOWN RANGE must be initialised to the smallest/minimum possible value e.g for a percentage marks programme with a range of 0% to 100%, HighestMark must be initialised to 0. HighestMark0 Initialising Minimum/Lowest Minimum for a KNOWN RANGE must be initialised to the Largest/Maximum possible value e.g for a percentage marks programme with a range of 0% to 100%, LowestMark must be initialised to 1000. LowestMark0 Initialising Minimum and Maximum for an unknown Range Where the range is not known or not given, both minimum and maximum must be initialised to the first value input. Hence the first value must be input before the loop. OUTPUT “Enter the first student’s height” INPUT Student1Height TallestHeightStudent1Height ShortestHeightStudent1Height FOR Count1 TO 9 OUTPUT “Enter height of a student” INPUT StudentHeight IF StudentHeight>TallestHeight THEN TallestHeight 17 THEN OUTPUT "You are an adult" ELSE OUTPUT "You are a child" ENDIF Question 1: Write a program that prompts the user to input the name and computer science mark for a student and output the grade: 90-100 A* 80-89 A 70-79 B 60-69 C 50-59 D 30-49 E 0-29 U //USING IF…THEN…ELSE…ENDIF START DECLARE CompSciMark : INTEGER OUTPUT “Please enter your computer science mark” INPUT CompSciMark IF CompSciMark =90 THEN OUTPUT “A*” ELSEIF CompSciMark =80 THEN OUTPUT “A” ELSEIF CompSciMark =70 THEN OUTPUT “B” ELSEIF CompSciMark =60 THEN OUTPUT “C” ELSEIF CompSciMark =50 THEN OUTPUT “D” ELSEIF CompSciMark =40 THEN OUTPUT “E” ELSEIF CompSciMark =0 THEN OUTPUT “U” ELSE OUTPUT “You have entered an invalid mark” ENDIF END 2. CASE…OF…OTHERWISE…ENDCASE Case statements are used when there are multiple choices to be made. Different programming languages provide different types of statement to do this. Case statement examples CASE OF OpValue "+" : Answer ¨ Number1 + Number2 "-" : Answer ¨ Number1 - Number2 "*" : Answer ¨ Number1 * Number2 "/" : Answer ¨ Number1 / Number2 OTHERWISE OUTPUT "Please enter a valid choice" ENDCASE Question 1: Write a program that prompts the user to input the name and computer science mark for a student and output the grade: 90-100 A* 80-89 A 70-79 B 60-69 C 50-59 D 30-49 E 0-29 U //USING CASE…OF…OTHERWISE…ENDCASE START DECLARE CompSciMark : INTEGER OUTPUT “Please enter your computer science mark” INPUT CompSciMark CASE CompSciMark OF: 90 TO 100 : OUTPUT “A*” 80 TO 89 : OUTPUT “A” 70 TO 79 : OUTPUT “B” 60 TO 69 : OUTPUT “C” 50 TO 59 : OUTPUT “D” 40 TO 49 : OUTPUT “E” 0 TO 29: OUTPUT “U” OTHERWISE OUTPUT “You have entered an invalid mark” ENDCASE END NESTER IF…THEN…ELSE…ENDIF STATEMENTS When IF statements are nested, the nesting should continue the indentation of two spaces. Example – nested IF statements ITERATION/LOOPING/REPETITION It is a construct which is used when a section of programming code needs to be repeated under certain conditions. There are 3 types of loops: 1. Count-controlled loops: FOR…TO…NEXT - for a set number of iterations 2. Pre-condition loops: WHILE…DO…ENDWHILE – may have no iterations 3. Post-condition loops: REPEAT…UNTIL – always has at least one iteration. 1. Count Controlled Loop - FOR…TO…NEXT Loop FOR loops are used when a set number of iterations are required or when the number of iterations/repetitions is known. It is a count controlled loop – meaning it can move the counting variable on its own. “It can count on its own”. The identifier must be a variable of data type INTEGER, and the values should be expressions that evaluate to integers Example: write a program that displays the name “Irvin Mtungwazi” 10 times. START DECLARE Count : INTEGER Count0 FOR Count1 TO 10 OUTPUT “Irvin Mtungwazi” NEXT Count END Question 1: Write a program that prompts the user to input their name and age and outputs them 10 times. START DECLARE StudentName : STRING DECLARE StudentAge : INTEGER DECLARE Count : INTEGER Count0 OUTPUT “Enter a name for the student” INPUT StudentName OUTPUT “Enter the age of the student” INPUT StudentAge FOR Count1 TO 10 OUTPUT “The student name is “,StudentName OUTPUT “The student age is”, StudentAge NEXT Count END Question 2: Write a program that prompts the user to input 10 numbers and calculates and outputs the total. START DELCARE Number : INTEGER DECLARE Total : INTEGER DECLARE Count : INTEGER Count0 Total0 FOR Count1 TO 10 INPUT “Enter a number: “, Number TotalTotal+Number NEXT Count OUTPUT “The total is : “,Total END Question 3: Write a program that prompts the user to input 10 numbers and stores them in an ARRAY, and add the average them. START DECLARE Numbers[1:10] : INTEGER DECLARE Sum, Count : INTEGER DECLARE Average : REAL Sum0 Average 0 Count0 FOR Count1 TO 10 INPUT “Enter a number”, Numbers (Count) SumSum+Numbers(Count) NEXT Count FOR Count1 TO 10 OUTPUT “The numbers are:”, Numbers(Count) NEXT Count AverageSum/10 OUTPUT “The total is “, Sum OUTPUT “The average is “, Average END Condition-controlled loops When the number of iterations is not known, there are two options: » pre-condition loops which may have no iterations – WHILE…DO…ENDWHILE » post-condition loops which always have at least one iteration - REPEAT…UNTIL 2. REPEAT…UNTIL Repeats an action while the condition is FALSE until it becomes TRUE. It is a conditional loop, meaning it *CHECKS A CONDITION*. It tests the condition at the bottom (Bottom Tested loop/Post Condition loop) – meaning, the program will enter the loop at least once before a condition is tested. The condition must be an expression that evaluates to a Boolean. The statements in the loop will be executed at least once. The condition is tested after the statements are executed and if it evaluates to TRUE the loop terminates, otherwise the statements are executed again Example – REPEAT UNTIL statement REPEAT OUTPUT "Please enter the password" INPUT Password UNTIL Password = "Secret" EXAMPLE 1: Write a program that prompts the user to input a number and add it to the total until the total is greater than 200. No need to store the numbers, just store the total. START DECLARE Number, Total : INTEGER Total 0 REPEAT INPUT “Please enter a number”, Number TotalTotal+Number UNTIL Total>200 OUTPUT “The total of the numbers is: “,Total END Example 2: Write a program that prompts the user to input a PIN until they input a correct PIN = 1234 START CONSTANT Pin : STRING=”1234” DECLARE PinGuess : STRING REPEAT INPUT “enter the pin: “, PinGuess UNTIL PinGuess=Pin OUTPUT “You have finally entered the correct pin” END WHILE…DO…ENDWHILE It is a conditional loop. It is a (PRE CONDITION/TOP TESTED LOOP). It tests the condition at the top, meaning the program may never enter the loop at all if the condition is not met. It repeats while the condition is true until it becomes false The condition must be an expression that evaluates to a Boolean. The condition is tested before the statements, and the statements will only be executed if the condition evaluates to TRUE. After the statements have been executed the condition is tested again. The loop terminates when the condition evaluates to FALSE. The statements will not be executed if, on the first test, the condition evaluates to FALSE Example – WHILE loop WHILE Number > 9 DO Number ← Number – 9 ENDWHILE Example 1: Write a program that prompts the user to input a number and adds it to the total until the total is greater than 200. No need to store the numbers, just store the total. START DECLARE Number, Total : INTEGER Total 0 WHILE TotalTallest THEN TallestStudentHeight ENDIF Highest Mark from 20 students marks IF StudentMark>HighestMark THEN HighestMarkStudentMark ENDIF Minimum: Finding the lowest or smallest value in a list. Examples: Height of the shortest student in a class of 10 students: IF StudentHeight

Use Quizgecko on...
Browser
Browser