Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

2022 Lab Test 2B

This document outlines instructions and questions for a lab test on algorithms and programming. It includes 5 questions of varying difficulty, from determining if a number is even or odd to calculating amicable number pairs. Students are instructed to write C++ programs to solve the problems and submit them online for evaluation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

2022 Lab Test 2B

This document outlines instructions and questions for a lab test on algorithms and programming. It includes 5 questions of varying difficulty, from determining if a number is even or odd to calculating amicable number pairs. Students are instructed to write C++ programs to solve the problems and submit them online for evaluation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

School of Computer Science & Applied Mathematics

Introduction to Algorithms & Programming (COMS1022A)


Lab Test 2

1 June 2022: 18h00 — 20h00

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
2
Question 1: Even or Odd (10 Marks)
Write a program that accepts a single integer and determines whether it is even or odd.

Input
Input is a single integer N

Output
Output Odd if N is odd; otherwise, output Even

Sample Output
Sample Input #1 Sample Input #2
1 2

Sample Output #1 Sample Output #2


Odd Even

3
Question 2: Upper Case (25 Marks)
The pseudocode below represents an algorithm that accepts a string as input and outputs that
string converted to upper case. Implement the corresponding C++ program.

Input
Input is a single line of text, which may contain spaces.

Output
Output the line of text converted to upper case.

Example Input-Output Pairs


Sample Input #1 Sample Input #2 Sample Input #3
hello hello, world! CAPITAL

Sample Output #1 Sample Output #2 Sample Output #3


HELLO HELLO, WORLD! CAPITAL

4
Question 3: Geometric Mean (25 Marks)
In mathematics, the geometric mean is a mean or average, which indicates the central tendency
or typical value of a set of numbers by using the product of their values (as opposed to the
arithmetic mean which uses their sum).
The geometric mean is defined as the nth root of the product of n numbers, i.e., for a set of
numbers x1 , x2 , . . . , xn , the geometric mean is defined as
1
(x1 × x2 × . . . × xn ) n
Write a C++ program that accepts many real numbers as input, and outputs their geometric
mean. Hint: you will need to include the cmath header file if you wish to use the pow function.
Also, be careful of any integer division!

Input
The first line of input is an integer n. n real-valued numbers follow, each on their own line.

Output
Print out the geometric mean of the n numbers.

Sample Output
Sample Input #1 Sample Input #2
2 3
2 1.5
8 2
9.0

Sample Output #1 Sample Output #2


4 3

5
Question 4: Permutations and Combinations (25 Marks)
A permutation is an ordered subset of a set. For example, say you wanted to pick a combination
to a vault. There are 60 possible numbers, and you need three different numbers for the com-
bination. There are then P (60, 3) permutations for the combination, where P is defined by the
formula

x!
P (x, y) = , (1)
(x − y)!
where ! is used as a suffix factorial operator. For example, 4! is 4 × 3 × 2 × 1.
Combinations are similar to permutations, except that the order of the numbers you pick does
not matter. The formula for combinations is

P (x, y)
C(x, y) = , (2)
y!
Download the source code provided for this question on Moodle, and complete the missing parts
to create a program that accepts two integers x, y and outputs C(x, y).

Note
Because factorials can become very large very quickly, the data type you need to store such large
numbers is long long.

Input
Input consists of two integers x, y, each on their own line. You may assume that 0 < y < x ≤ 20.

Output
Print the number of combinations C(x, y).

Example Input-Output Pairs


Sample Input #1 Sample Input #2 Sample Input #3
2 13 20
1 3 19

Sample Output #1 Sample Output #2 Sample Output #3


2 286 20

6
Question 5: Amicable Pairs (15 Marks)
In mathematics, an amicable pair is a set of two different numbers so related that the sum of the
proper divisors of each is equal to the other.
In order to define this more precisely, we first define the function s(n), which is the sum of all
proper divisors of n (i.e. all divisors of n other than itself). For example, the proper divisors of
12 are 1, 2, 3, 4, 6 and so s(n) = 1 + 2 + 3 + 4 + 6 = 16.
A set of two natural numbers a, b is then said to be amicable if s(a) = b and s(b) = a. For
example, the numbers 220 and 284 are amicable because:
• The proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110, and so s(220) = 284.
• The proper divisors of 284 are 1, 2, 4, 71, 142, and so s(284) = 220.
Given this definition, write a C++ program that determines whether a pair of numbers is ami-
cable.

Input
The first line of input is a single integer N . N lines of input follow, with each line containing a
two positive integers separated by a space.

Output
For each line, output Amicable if the pair of numbers on that line are amicable. Otherwise,
output Not amicable.

Sample Input
3
284 220
1184 1210
2621 2924

Sample Output
Amicable
Amicable
Not amicable

You might also like