2024_Spring_Programming_Final
2024_Spring_Programming_Final
學號:_____________ 姓名:________________
Take an array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest
number using bubble sort. In each step, elements written in bold are being compared.
Three passes will be required;
First Pass
( 5 1 4 2 8 ) → ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps
since 5 > 1.
( 1 5 4 2 8 ) → ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) → ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) → ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5),
algorithm does not swap them.
Second Pass
(14258)→(14258)
( 1 4 2 5 8 ) → ( 1 2 4 5 8 ), Swap since 4 > 2
(12458)→(12458)
(12458)→(12458)
Now, the array is already sorted, but the algorithm does not know if it is completed. The
algorithm needs one additional whole pass without any swap to know it is sorted.
Third Pass
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
1
Implement the Bubble Sort as a function that takes an argument of a ”vector<string>
reference“ representing an unsorted list of strings to turn it into a list of strings in an
alphabetical order (10%) and print each comparison with or without a swap (5%) in the
whole process and finally also the total number of “comparisons” to accomplish this task
(5%). Test the function with a main function (5%).
Input Output
2
2. The Sieve of Eratosthenes is a highly efficient way to generate a list of primes. It works
by recognizing that all non-prime numbers are divisible by a prime number. We start with
a list of all the numbers up through some value max. First, we cross off all numbers
divisible by 2. Then, we look for the next prime (the next non-crossed off number) and
cross off all numbers divisible by it. By crossing off all numbers divisible by 2, 3, 5, 7,
11, and so on, we wind up with a list of prime numbers from 2 through max.
Write the code that implements the Sieve of Eratosthenes with the following prototype
that returns an array of boolean values indicating if the corresponding integer is a prime
number or not (20%). Test the function with a main function (5%).
Input Output
10 [0, 1, 1, 0, 1, 0, 1, 0, 0, 0]
5 [0, 1, 1, 0, 1]
3
3. (25%) Create a class called Complex for performing arithmetic involving imaginary
numbers. Use fractional variables to represent the private data of the class – the real part
and the imaginary part. Provide a constructor that enables an object of this class to be
initialized when it is declared. For example, the complex number 2+4i would be stored in
the object as 2 in the real part and 4 in the imaginary part. Provide public member
functions that perform each of the following tasks:
(a) (5%) Add a Complex number and the other Complex number that is taken as a
function argument.
(b) (5%) Let a Complex number be subtracted by the other Complex number that is taken
as a function argument.
(c) (5%) Print a Complex number in the form a+bi, where a is the real part and b is the
imaginary part.
(d) (5%) Square a Complex number. Note that (a+bi)^2= a^2-b^2+2*a*b.
Please also write a main function to prompt the user to input two Complex numbers (5%).
● Add one number and the other (from these two numbers) using (a) and then print
the result using (c).
● Let one number be subtracted by the other (from these two numbers) using (b)
and then print the result using (c).
● Square each of these Complex numbers using (d) and then print the result using
(c).
Input/Output Format :
○ Input: Enter two sets of numbers twice, each representing the real and
imaginary parts of two complex numbers.
■ 2314
● 2 : the real parts of the first complex number
● 3 : the imaginary parts of the first complex number
● 1 : the real parts of the second complex number
● 4 : the imaginary parts of the second complex number
4
○ Output: The program will produce four outputs in the following formats:
■ Addition: The sum of the two complex numbers.
■ Subtraction: The difference between the first and the second
complex number.
■ Square of the first number: The result of squaring the first complex
number.
■ Square of the second number: The result of squaring the second
complex number.
Input Output
2314 Addition:3+7i↵
Subtraction:1-1i↵
Square of first number:-5+12i↵
Square of second number:-15+8i↵
-1 2 3 -3 Addition:2-1i↵
Subtraction:-4+5i↵
Square of first number:-3-4i↵
Square of second number:0-18i↵
5
4. Given a string with an arbitrary number of characters, write a function to check
recursively if the string is a palindrome, e.g., acbebca, (10%) and another function to check
iteratively if the string is a palindrome (10%). A palindrome here is a string that is the same
forwards and backwards. Test both functions by giving any user input string using the main
function (5%).
● Input/Output Format
○ Input: Enter a string to check if it is a palindrome.
○ Output: The program will produce two outputs:
■ Recursive: The result of checking the string using the recursive method
(Palindrome or Not Palindrome).
■ Iterative: The result of checking the string using the iterative method
(Palindrome or Not Palindrome).
● Follow the format as the following examples
Input Output
racecar Recursive:Palindrome↵
Iterative:Palindrome↵