COMS1018 - Department of Computer Science
Transcription
COMS1018 - Department of Computer Science
School of Computer Science Lab Handout Introduction to Algorithms & Programming (COMS1018) 12 March, 2015 Lab Handout #2 Teaching Staff: • Lab Assistants: Ritesh Ajooda, email: [email protected], Mafora Lebogang, email: [email protected], Andile Nxongo, email: [email protected], Hlayisani Chauke, email: [email protected], Tshepo Molefe, email: tshepomolefe2@ gmail.com Useful Terminal Commands Make sure you are comfortable working with terminal commands such as: ls cd rm mkdir If you are unfamiliar with using terminal then please watch the following YouTube videos as they provide provide a good introduction to using terminal: http://www.youtube.com/watch?v=2FiQSLdnBqA and http: //www.youtube.com/watch?v=_JWj6u8mI7k. You also need to know how to add arguments to commands and that includes redirects. Always consult the internet for help! Objective The aim of this lab session is to understand what a pseudocode/algorithm is and to develop the corresponding flowchart representation together with C++ source code. 1 Finding the minimum of two numbers In the class, you learned how to write an algorithm and draw the flowchart for finding and displaying the maximum of two numbers entered by a user. In this lab session, this algorithm will be modified to find the minimum of two numbers instead of the maximum. Please compile and test C++ code. Algorithm/Pseudocode Flowchart C++ Code START x 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: START INPUT x INPUT y IF (x <= y) THEN minimum = x ELSE minimum = y ENDIF PRINT minimum STOP y FALSE x <= y minimum = y TRUE minimum = x minimum STOP 1 of 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // F i l e : FindMinimumOfTwoNumbers . cpp #i n c l u d e <i o s t r e a m > u s i n g namespace s t d ; i n t main ( void ) { double x ; double y ; double minimum ; c i n >> x ; c i n >> y ; if { ( x <= y ) minimum = x ; } else { minimum = y ; } c o u t << minimum c o u t << ” \n ” ; T return ( 0 ) ; } ; School of Computer Science 2 Lab Handout Finding the minimum of three numbers Using the above algorithm, develop a new algorithm for finding and displaying the minimum of three numbers entered by a user. To ease your task, pseudocode and flowchart is given below. Your task is to complete and successfully compile the corresponding C++ source code. Algorithm/Pseudocode Flowchart START C++ Code x y z 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: START INPUT x INPUT y INPUT z IF (x <= y) THEN minimum = x ELSE minimum = y ENDIF IF (z <= minimum) THEN minimum = z ENDIF PRINT minimum STOP FALSE TRUE x <= y minimum = y minimum = x 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // F i l e : FindMinimumOfThree . cpp #i n c l u d e <i o s t r e a m > u s i n g namespace s t d ; i n t main ( void ) { double x = 0 . 0 ; double y = 0 . 0 ; double z = 0 . 0 ; double minimum = 0 . 0 ; // ? // ? // ? c o u t << minimum ; c o u t << ” \n ” ; return ( 0 ) ; } z <= minimum FALSE TRUE minimum = z minimum STOP 3 Statistical application Use the pseudocode provided below to write a C++ program that computes and displays the mean, variance and standard deviation of four numbers entered by a user. Before you implement the program be sure to draw a flowchart of all processors: Hint: To compute the mean (mean), variance (var), and standard deviation (std) of four numbers a, b, c, d entered by a user, use the following three equations respectively. mean = (a + b + c + d)/4 (1) var = (mean − a)2 + (mean − b)2 + (mean − c)2 + (mean − d)2 /4 (2) std = √ var (3) NB: In order to use the square root function in C++ you must first include the #include <cmath> package below the #include <iostream> package in your program. The sqrt() function returns the square root value √ of a number. For example if we wanted to compute 8 we will write it as sqrt(8) in C++. 2 of 5 School of Computer Science Lab Handout Use the following pseudocode to develop your program: 1: START 2: PRINT "Enter the 1st number: " 3: INPUT a 4: PRINT "Enter the 2nd number: " 5: INPUT b 6: PRINT "Enter the 3rd number: " 7: INPUT c 8: PRINT "Enter the 4th number: " 9: INPUT d 10: mean = (a + b + c + d)/4 2 2 2 2 11: var = (mean − a) + (mean − b) + (mean − c) + (mean − d) /4 √ var 12: std = 13: PRINT "The mean is: " 14: PRINT mean 15: PRINT "The variance is: " 16: PRINT var 17: PRINT "The standard deviation is: " 18: PRINT std 19: STOP 4 Mathematical application START a b c d = b2 − 4ac d<0 FALSE a 6= 0 TRUE no solution no solution TRUE FALSE FALSE d=0 FALSE TRUE x = −b/ (2a) √ x1 = −b + d / (2a) one root: x √ x2 = −b − d / (2a) b 6= 0 TRUE x = −c/b two roots: x1 , x2 one root: x STOP Figure 1: Flowchart Write a C++ program based on the flowchart given to you in Figure 1. Your program must take in each coefficient individually and output the roots of the equation as described in the flowchart. Use the hint in section 3 to handle square roots and other mathematical operations. 3 of 5 School of Computer Science Lab Handout Run your program on the following input coefficients: • (x + 1)(x − 3) = 0 • x2 − x = 4 • x2 − 3x − 4 = 0 • x2 − 4 = 0 • 6x2 − 11x − 35 = 0 • x2 − 48 = 0 • x2 − 7x = 0 5 Computer Science application In Basic Computer Organisation (BCO) you learned how to compute the base ten value given any other base value. This can be done by using the following instruction: for any number dn dn−1 ...d2 d1 where dn to d1 are the digits of the number (from left to right) in base R, the base 10 representation is given by dn Rn−1 + dn−1 Rn−1 + ... + d2 R1 + d1 . (4) Use this formula to write a program that will take in a four digit number in any base and will compute its base 10 value. Your program should input the base value first then the four digit number digit by digit. The output of the program will be the value of the four digit number in base 10. 6 The Grading machine Write a C++ program that will take a numerical input (0-100) and will output the grading symbol scored in a mathematics class. If the user scored a 80% then the machine will notify the user that he had received an A+. Use Table 1 as a guide to match marks with the Wits grading system. Mark A++ A+ A B C D F Symbol 90-100 80-89 75-79 70-74 60-69 50-59 0-49 Table 1: The Wits Grading System 7 A Vending machine Write a C++ program that presents the user with a choice of your 5 favourite beverages (Apple Juice, Coke, Water, Sprite, ... , Whatever). Then allow the user to choose a beverage by entering a number 1-5. Output which beverage they chose. Change your the program so that if the user enters a choice other than 1-5 then it will output "Error-choice was not valid, here is your money back." 4 of 5 School of Computer Science 8 Lab Handout Laboratory self test Circle the correct answer in each question. Every question has only one correct answer. When you have completed you may leave the lab after showing your progress to the lab assistants. 1. What is the only function all C++ programs must contain? • start() • system() • main() • program() 2. What is the correct value to return to the operating system upon the successful completion of a program? • -100 • 1 • 0 • a value cannot be returned by a program 3. What punctuation is used to signal the beginning and end of code blocks? • { } • -> • BEGIN and END • ( ) 4. What punctuation ends most lines of C++ code? • . • ; • : • ’ 5. Which of the following is a correct comment? • */ Comments */ • ** Comment ** • /* Comment */ • ( Comment ) 6. Which of the following is not a correct variable type? • float • real • int • double 7. Which of the following is the correct operator to compare two variables? • := • = • ~= • == 5 of 5