HiLCoE School of Computer Science and Technology CS221: Computer Programming I (Using C++) Autumn 2015 Worksheet I 1. Don’t you love triangles? A number N is called “triangular” if you can take N little asterisks and make an equilateral triangle from them. For example, here are the first few triangular numbers: * * * 3 * * * * * * 6 * * * * * * * * * * 10 * * * * * * * * * * * * * * * 15 * * * * * * * * * * * * * * * * * * * * * 21 Here is an algorithm that is attempting to detect triangularity in the input. But it has a bug. Fix the algorithm and draw the flowchart for the algorithm you fixed. START OUTPUT “Enter a positive integer” INPUT N i = 1 S = 0 WHILE (S < N) i = i + 1 S = S + i END WHILE IF (S = N) OUTPUT “Triangular” ELSE OUTPUT “Not triangular” END IF STOP HiLCoE School of Computer Science and Technology CS221: Computer Programming I (Using C++) Autumn 2015 2. The oscillating sign Write an algorithm in pseudo-code that accepts a positive integer N and outputs a 1(one) if N is even and -1 (minus one) if N is odd. Hint: -1 multiplied by itself an even number of times is +1, while -1 multiplied by itself an odd number of times is -1. 3. De-Gauss this! Gauss’s formula for the computation of the Nth triangular number is given as: 1 + 2 + 3 + ... + N = N(N + 1) 2 Here is another algorithm attempting to detect triangularity of the input. Do you think this one is OK? If not, fix it. START OUTPUT “Enter a positive integer” INPUT N i = 1 WHILE [(i × (i + 1))/2 < N] i = i + 1 END WHILE IF [(i × (i + 1))/2 = N] OUTPUT “Not triangular” ELSE OUTPUT “Triangular” END IF STOP HiLCoE School of Computer Science and Technology CS221: Computer Programming I (Using C++) Autumn 2015 4. Here is an algorithm in pseudo-code: START OUTPUT “Enter a positive integer” INPUT N i = 1 WHILE (i ×i < N) i = i + 1 END WHILE IF (i × i = N) OUTPUT “YES” ELSE OUTPUT “NO” END IF STOP For which of the following inputs will the algorithm say “YES”? (a) (b) (c) (d) 28 36 54 81 What property of the input is the algorithm determining? 5. Summing the squares Describe an algorithm in either pseudo-code or flowchart form, to sum the squares o f the integers up to and including some N, where N is an input to the algorithm. That is, S = 12 + 22 + 32 + · · · + N2 Where S is the sum. You must express your algorithm in terms of a loop that is traversed N times. (There is a closed mathematical expression S = N (N + 1) (2N + 1) / 6 that you must not exploit in your algorithm. Pretend the closed mathematical expression does not exist.) Make sure to declare and initialize all the variables you use. HiLCoE School of Computer Science and Technology CS221: Computer Programming I (Using C++) Autumn 2015 6. Summing an oscillating series Describe an algorithm in both pseudo-code and flowchart form, to perform the following sum S = 1− x + x2 − x3 + x4 · · · +(-x)N Where x and N are inputs and S is the sum, an output. You must express your algorithm in terms of a loop that is traversed N times. Express your algorithm in terms of pseudo-code and a flowchart. 7. Solve for x: ax2 + bx + c = 0 where a, b, c are arbitrary constants The quadratic equation has real solutions: x = −b ± √b2 – 4ac if a = 0, and b2 – 4ac ≥ 0 2a Under some circumstances, (a = 0, b2 – 4ac > 0), we have two solutions: x = −b ± √b2 – 4ac 2a 2 What if a = 0, b – 4ac = 0? We have one solution: x = −b 2a What if b2 – 4ac < 0? No real solution. What if a = 0? Re-analyze! bx + c = 0 (y = bx + c is an equation for a straight line!) x = −c N.B. b = 0 b This is the place where a straight line crosses the x-axis. Give the solution to the quadratic equation both in pseudocode and flow chart.