Starting from:

$25

GEDT018 - Computer Programming for Engineers - Homework Assignment #1 - Solved



Exercise 1.

(25 Points) Write a program that takes 10 numbers from a user and store them into an array in q1.cc. The program manipulates the array in a way that it stores all the odd numbers in an ascending order, followed by all the even numbers in a descending order. Once complete, display all elements in the array. Listing 1 shows an output example.

(1)         If all 10 numbers are not entered from an input, exit the program with the message: “Enter 10 numbers”. (3 points deducted if missing)

(2)         If any number from an input contains a negative number, exit the program with the message: “Numbers must be between 0-9” (3 points deducted if missing).

(3)         Input numbers must be ranging from 0 to 9. In case of any number that is out of range, exit the program with the message: “Enter the numbers [0-9]”. (3 points deducted if missing) (4) Use the range-based for loop (C++11). (4 points deducted if missing)

// function prototype void odd_even_order();

// output example with 10 numbers

1 2 3 4 5 6 7 8 9 0

1 3 5 7 9 8 6 4 2 0

// output example less than 10 numbers

1 2

Enter 10 numbers

// output example with a negative number -1 10

Numbers must be between 0-9

// output example with a number that is out of

1 2 3 4 5 6 7 8 97 0

Enter the numbers [0-9]
range
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Listing 1: Function prototype for Exercise 1 and its output example.

Exercise 2.

(30 points) The factorial of a non-negative integer n , denoted by n!, is the product of all positive integers less than or equal to n. Write a program that calculates a factorial in q2.cc. In this exercise, you have to implement three different ways for the factorial.

(1)         The function factorial() takes two parameters where the second argument method is optional. The argument is 1 by default, which means the first method as in Listing 2. A user should be able to choose three different ways to compute a factorial given n. The function must have a switch/case statement to invoke three different methods for calculating a factorial (8 points). If there is a method other than [1 − 3], your program should print out “Wrong method” (2 points).

(2)         For the first method, you have to implement the function named factorial1() using an iteration such as a do-while or for loop (5 points).

(3)         For the second method, you have to implement the function named factorial2() in a recursive way (7 points).

(4)         For the third method, you have to implement the function named factorial3() with the following formula for factorial approximation (8 points).

                                                                                  where n> 0                                                                       (1)

// function prototypes unsigned long int factorial(int x, int unsigned long int factorial1(int n); unsigned long int factorial2(int n); unsigned long int factorial3(int n);
method=1);
1

2

3

4

5

Listing 2: Function prototypes for Exercise 2.

•   Note that an input number will not exceed 15.

•   The skeleton code will be given, which should not be modified. Listing 2 shows function prototypes to write your own code.

•   Note that the values of π and natural e are statically defined.

Exercise 3.

(45 Points) Reading a given text file, words.txt in Listing 3, write a program to emit an output in q3.cc, answering several questions below.

(1)   How many words are there in the file? (5 points)

(2)   What is the longest word of all in length? (5 points)

(3)   A word may include special characters such as “.” or “-”. What is the ratio of the words that consist of pure alphabets (i.e., [a − zA − Z])? The ratio should end with % rounded to two decimal places (e.g., 33.1865% →− 33.19%) (10 points)

(4)   A word may have pairs of consecutive double letters. For example, the word “tooth” has one pair of double letters where the word “committee” has three pairs of consecutive double letters. Your task is to count the words that contain such consecutive double letters. (15 points)

(5)   By taking a single string, count the number of words that includes that string. (10 points)

// function prototype

void counter_box(string file_path="words.txt");
1


More products