Uploaded by mc.selahle

LabTest2C

advertisement
U NIVERSITY
OF THE
W ITWATERSRAND , J OHANNESBURG
School of Computer Science & Applied Mathematics
Introduction to Algorithms & Programming (COMS1018A)
Lab Test 2
2 June 2022: 14h30 — 16h30
1
Instructions
• This is an closed-book test: you may not consult any written material, books or the
internet.
• Any form of communication with anyone other than test invigilators will be considered
cheating. This includes the sharing of files, emailing and chatting.
• No cellphones are permitted in the test venue.
• There is a strict deadline on submissions. Late submissions will not be accepted.
• There are five questions in total. Answering all five correctly will earn you 100%.
• Your solutions should work for any valid input for a given question, not just the sample
inputs that accompany each question.
• To submit your programs, log onto Moodle at http://courses.ms.wits.ac.za and submit
under the appropriate heading.
• You may submit as many times as you like. Only the last solution will be counted.
• You may only ask test invigilators for question clarifications, and not for code debugging
help.
• You may submit questions in any order you wish, but the questions are ordered roughly
from easiest to hardest.
• If you have a problem when submitting your code to Moodle, ask an invigilator for help.
• If the marker is taking a long time to evaluate your submission, do not wait. Move on to
the next question.
1
Question 1: Circle Area (10 Marks)
Write a program that accepts a single real-valued number r — the radius of a circle — and
22.0
outputs the area of the circle. Use
as an approximation to π.
7.0
Input
Input is a single real-valued number r ≥ 0.
Output
Output the area of the circle: πr2 , using the approximation π ≈
22.0
.
7.0
Sample Output
Sample Input #1
Sample Input #2
7
1
Sample Output #1
Sample Output #2
154
3.14286
2
Question 2: Factorial (25 Marks)
Write a C++ program that computes the factorial of N , where N is a non-negative number
entered by a user and the factorial of N is given by 1 × 2 × . . . × N )
1
2
3
4
5
6
7
product = 1
counter = 1
N = int(input())
while counter <= N:
product = product * counter
counter = counter + 1
print(product)
Input
Input is a single non-negative number N .
Output
Output N ! (where N ! = 1 × 2 × . . . × N )
Sample Input #1
5
Sample Output #1
120
Sample Input #2
0
Sample Output #2
1
3
Question 3: Harmonic Mean (25 Marks)
In mathematics, the three classical Pythagorean means are the arithmetic mean, the geometric
mean, and the harmonic mean. These means were studied with proportions by Pythagoreans
and later generations of Greek mathematicians because of their importance in geometry and
music. The harmonic mean is often appropriate for situations when the average rate is desired.
Let us assume that we have n positive real-valued numbers x1 , x2 , . . . , xn . The harmonic mean
of these numbers is defined as
P
n
1
x1
+
1
x2
n
+ ... +
1
xn
x−1
i
 i=1
=
 n
−1



Write a C++ program that accepts many real numbers as input, and outputs their harmonic
mean. Hint: you will need to include the cmath header file if you wish to use the pow function.
Input
The first line of input is an integer n. n real-valued numbers follow, each on their own line.
Output
Print out the harmonic mean of the n numbers.
Sample Output
Sample Input #1
Sample Input #2
3
0.5
0.25
0.1
2
0.5
0.75
Sample Output #2
Sample Output #1
0.6
0.1875
4
Question 4: Name Shortening (25 Marks)
In a telephone directory, the names of streets and suburbs are shortened by removing all vowels
(a, e, i, o, u), unless the vowel appears at the beginning of a word, in which case it is kept. Write
a program that will input an address and automatically shorten it in this manner. Punctuation
and numbers must be kept unchanged, as must any capitalisation of letters.
Input
Input consists of a single line of text, containing an address. The line may contain letters, digits,
punctuation and spaces.
Output
Output the shortened version of the address.
Sample Input
13 Observatory Road
Sample Output
13 Obsrvtry Rd
5
Question 5: Nice Primes (15 Marks)
A prime number is a positive integer that has only two factors (itself and 1). The first few prime
numbers are are 2, 3, 5, 7, 11 . . .. We will define a nice prime to be a prime number whose
square is greater than the product of the prime numbers immediately before and after it in the
sequence of primes.
For example, the number 5 is a nice prime since 52 > 3 × 7. However, 7 is not nice since
72 < 5 × 11.
Download and complete the code on Moodle to create a C++ program that reads in a series of
numbers and determines whether they are nice primes.
Input
The first line of input is N , which specifies the number of integers to be read in. N integers
follow, each on their own line. There is no guarantee that these integers are prime.
Output
For each integer, output True if it is a nice prime, and False otherwise.
Sample Input
3
5
7
11
Sample Output
True
False
True
6
Download