Está en la página 1de 3

CSE 1243 Programming Paradigms

Labsheet 2 Imperative Programming (in Python)

Question 1 Write a program that reads a value, n, and calculates and displays the result of subtracting the sum of all odd numbers from 1 up to and including n from the sum of all even numbers within the same range.

Question 2 Write a program that reads a value, n, and determines if it is a prime number.

Question 3 The Gregorian Epact is the number of days between 1st January and the previous 1st quarter moon phase. This value is used to figure out the date of Easter. It is calculated by these formulae (using integer arithmetic): C = year/100, epact = (8 + (C/4) C + ((8C + 13)/25) + 11 (year%19))%30 Write a program that prompts the user for a 4-digit year and then outputs the value of the epact. Note: If the user enters a year which is not of 4-digit, your program must display an error message and continuously prompt the user for a 4-digit year until a valid year is entered.

Question 4 Write a program which continuously asks the user to enter a set of characters (a String) and provides the user with the following menu:

1.

Display in uppercase

2. Capitalise first letter of each word 3. Display first character in uppercase 4. Count the number of occurrences of a particular set of characters 5. Replace all occurrences of a particular set of characters with another set of characters 6. Exit

When the user selects one option (1 5), the appropriate steps are performed, the output displayed and the menu displayed again until he/she selects option 6. Note that if the user selects options 4 or 5, the program will prompt the user for further input(s), namely the set(s) of characters.

Question 5 Consider the following algorithm that generates a sequence of numbers: It starts with an integer num. If num is even, it is divided by 2. If num is odd, it is multiplied by 3 and 1 is added to the product. This process is repeated with the new value of num and terminates when num = 1. For example, the following sequence of numbers will be generated for num = 13: 13 40 20 10 5 16 8 4 2 1

Write a function called generateSquence in Python that implements the algorithm described above. The function takes as parameter the number num, generates and outputs the sequence of numbers from num to 1, and returns the number of elements in the sequence to the calling program (e.g. the number of elements in the sequence generated when num = 13 is 10).

Question 6 Write a function called increasingSequence in Python that takes as parameter a one-dimensional array of integers and returns True if the array represents an increasing sequence of values and False otherwise. For example, 10, 30, 178 is an increasing sequence whereas 11, 30, 5, 70, 110 is a non-increasing sequence.

Question 7 The math library contains a function that computes the square root of numbers. You are to write your own algorithm for computing square roots. One way to solve this problem is to use a guessand-check approach. You first guess what the square root might be and then see how close your guess is. You can use this information to make another guess and continue guessing until you have found the square root (or a close approximation to it). One particular good way of making guesses is to use Newtons method. Suppose x is the number you want the root of and guess is the current guessed answer, the guess can be improved by using the following as the next guess:
x guess + guess 2

Write a Python program that implements Newtons method. Your program should prompt the user for the value to find the square root of, x, and the number of times to improve the guess. Starting with a guess value of x/2, your program should loop the specified number of times applying Newtons methods and report the final value of guess. You might also print out the value of the square root of x obtained by using the sqrt function from the math library.

Prepared by Anuja Meetoo-Appavoo, January 2011

También podría gustarte