Introduction To Computer Programming (Meng 1052) Worksheet 2: Functions, Arrays and Strings Functions
Introduction To Computer Programming (Meng 1052) Worksheet 2: Functions, Arrays and Strings Functions
(MEng 1052)
Worksheet 2: Functions, Arrays and Strings
Functions
1. Write a function QualityPoints that inputs a student’s average and returns 4 if a student’s
average is 90–100, 3 if the average is 80–89, 2 if the average is 70–79, 1 if the average is
60–69 and 0 if the average is lower than 60.
2. Write a function hypotenuse that calculates the hypotenuse of a right triangle when the other
two sides are given. The function should take two double arguments and return the
hypotenuse as a double.
3. An integer is said to be a perfect number if the sum of its divisors, including 1 (but not
the number itself), is equal to the number. For example, 6 is a perfect number, because 6
= 1 + 2 + 3. Write a function called isPerfect that determines whether parameter number
is a perfect number. Use this function in a program that determines and prints all the
perfect numbers between 1 and 1000.
4. An integer is said to be prime if it’s divisible by only 1 and itself. For example, 2, 3, 5
and 7 are prime, but 4, 6, 8 and 9 are not. Write a function called isPrime that determines
whether a number is prime. Use this function in a program that determines and prints all
the prime numbers between 2 and 10,000.
5. Write a function that prints the following shapes
(a) (b)
1
Arrays and Strings
6. Write a C++ program that accepts 10 integers from the user and finally displays the
smallest value and the largest value.
7. Write a program that accepts ten different integers from the user and display these
numbers after sorting them in increasing order.
8. Write a C++ program that calculates the letter grades of 20 students. The program should
accept the mid result and the final result from the students. Use the appropriate validity
control mechanism to prevent wrong inputs.
9. Write a C++ program that accepts a word from the user and then displays the word after
reversing it.
10. Develop a C++ program that accepts the name of a person and then counts how many
vowels the person’s name have.