CS2310 Lab 8-Functions Solved

35.00 $

Category:

Description

5/5 - (1 vote)

Please test the correctness of your programs in Q1, Q2a, Q2b, and Q3 using PASS.

Q1. Download calc.cpp. Complete the function definitions such that it computes addition, subtraction, multiplication, and division of two real numbers.

Output the result in 2 decimal places. Example inputs and outputs are as follows.

Expected Outputs:

 

Example-1 Example-2
Enter an Expression consist of two operands and one operator. 1+2

3.00

Enter an Expression consist of two operands and one operator.

2.5-1

1.50

Example-3 Example-4
Enter an Expression consist of two operands and one operator. 2*3

6.00

Enter an Expression consist of two operands and one operator.

 

Example-5 Example-6
Enter an Expression consist of two operands and one operator. 1-2.5

-1.50

Enter an Expression consist of two operands and one operator.

 

 

Q2a. Download and complete the program printNum.cpp, which intends to print integers in English, corresponding to user input (range: 1 to 19).

 

The program consists of the following functions:  print1to9: accepts an integer from 1 to 9 as input and print the number in English. This function has been defined for you. (but not 100% correct !!)

 

Let’s fix the bug(s) in function print1to9. After that, you create another function called print10to19. The function accepts an integer from 10 to 19 as input and prints the number in English.

 

The user input should be done in the main function. After the number is read, the appropriate functions should be called for output.

 

Expected Outputs:

 

Example-1 Example-2
Enter a number in Range [1–19].

0

0 is not in range from 1 to 19

Enter a number in Range [1–19]. 20

20 is not in range from 1 to 19

Example-3 Example-4
Enter a number in Range [1–19].

4

Four

Enter a number in Range [1–19]. 15

Fifteen

 

 

Q2b. Modify the program in Q2a such that it accepts an integer (1 to 99) from the user and prints the English representation accordingly.

 

You should define an extra function as follows.

print20to99: accepts an integer from 20 to 99 as input and print the number in English.

 

(Hint: You may break down the number into units digit and tens digit and call the print1to9 and print10to19 functions you defined in Q2a to generate the output.)

 

Similar to Q2a, the user input should be done in the main function. After the number is read, the appropriate function(s) should be called for printing the result.

 

Expected Outputs:

 

Example-1 Example-2
Enter a number in Range [1–99].

0

0 is not in range from 1 to 99

Enter a number in Range [1–99]. 20

Twenty

 

Example-3 Example-4
Enter a number in Range [1–99].

21

Twenty One

 

Enter a number in Range [1–99]. -21

-21 is not in range from 1 to 99

Example-5 Example-6
Enter a number in Range [1–99].

78

Seventy Eight

Enter a number in Range [1–99]. 100

100 is not in range from 1 to 99

Q3. Download sortArray.cpp. The program intends to use bubble sort to arrange the numbers in array Num[] in an ascending order. As in typical bubble sort, the program compares number pairs repeatedly and it swaps the numbers if they’re placed out-of-order.

 

Your task is as follows:

− Complete the Swap() function which exchanges the values of two integers using pass-byreference.

 

− Complete the bubble sort by filling out correct indices in the two for loops for array elements.

 

Expected Outputs:

 

Example-1 Example-2
Enter 10 numbers:

1 3 2 5 4 7 6 8 9 10

The sorted numbers:

1 2 3 4 5 6 7 8 9 10

Enter 10 numbers:

1 2 3 4 5 6 7 8 9 10

The sorted numbers:

1 2 3 4 5 6 7 8 9 10

Example-3 Example-4
Enter 10 numbers:

10 9 8 7 6 5 4 3 2 1

The sorted numbers:

1 2 3 4 5 6 7 8 9 10

Enter 10 numbers:

3 44 38 5 47 15 36 26 27 2

The sorted numbers:

2 3 5 15 26 27 36 38 44 47

 

Interested students may also modify the program such that the elements are sorted in descending order, rather than ascending. (Note: No need to upload this program to PASS)

 

  • Lab_08-xqhjyy.zip