Tải bản đầy đủ (.pdf) (9 trang)

Supplementary tasks functions

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (319.38 KB, 9 trang )

Chapter 6: Functions
A. Review
A.1. What is a function in the C programming language? Make a short comparison between a function in
the C programming language and the one in mathematics. Give some examples for illustration.
A.2. Describe the main components of a function and then describe its declaration and use in a specific
context for program modularization. Give at least three examples of functions.
A.3. Discuss the advantages and disadvantages of defining and using functions in a C program.
A.4. What is a recursive function? Give an example of a recursive function and its corresponding nonrecursive version.
A.5. What is function call by value? Give an example to illustrate function call by value.
A.6. Distinguish call-by-value and call-by-reference. Give at least 2 examples for illustration.
B. Practice
B.1. Modularize the programs you have written for the problems in chapter 1.
B.2. Find errors in the following programs. Fix them so that the programs can be executed. Described
what is returned by each program.
B.2.1.

1


B.2.2.

2


B.2.3.

3


B.2.4.


B.3. Write a program that is defined with a non-recursive function and another one with a recursive one.
B.3.1. Estimate a value of sinx

B.3.2. Print star-based natural numbers
Input: 12304
Output:
1

*

2

**

3

***

0
4

****
4


B.4. Write a function to compute the number of k-combinations of a set of n elements:
C(n, k) = n!/(k!*(n-k)!).
Each k-combination of a set of n elements is a way of selecting k distinct elements from the set. It is
noted that the order of selection does not matter. A function to compute the factorial of a number can
be used. Write a program to use the resulting function with n and k input from the keyboard.

Input: n = 4, k = 2
Output: C(4,2) = 6
B.5. Write a function to generate a 9-digit-based mask of a given positive integer. Write a program to ask
a user to input a number and generate a mask for the given number if it is valid. If the user inputs zero
(0), the program will be ended.
Input 1: 113789
Output 2: 999999
Input 2: 10987
Output 2: 90999
Input 3: 0
Output 3: the program is terminated.
B.6. Let's examine quadratic polynomial functions. Each quadratic polynomial function has 3 coefficients
a, b, and c. Write some following functions to support addition and subtraction on two polynomial
functions to obtain another quadratic polynomial function.
- Addition
- Subtraction
Input: f1 = 2*x^2 + 3*x; f2 = -4.5*x^2 + x + 0.8
Output:
f1 + f2 = -2.5*x^2 + 4*x + 0.8
f1 - f2 = 6.5*x^2 + 2*x - 0.8
B.7. Given a triangle with three coordinates (x1, y1), (x2, y2), and (x3, y3) in a 2-D space, write some
following functions to describe the triangle:
- Circumference

5


- Area
- Type
Write a program to ask a user to input three coordinates of a triangle in a 2-D space. If the given triangle

is valid, display its information in the form of a table with a header such as: Circumference\tArea\tType.
B.8. Given the information about each student in a structure with 3 subjects: Mathematics, Physics, and
Chemistry, update their averaged grade after each semester.
Input: student1 = (A Nguyen, 9, 8.7, 8.1, 0)
Output: student1 = (A Nguyen, 9, 8.7, 8.1, 8.6)
B.9. Given a positive number, change it to its reversed number.
Input 1: 1234567
Output 1: 7654321
Input 2: 11
Output 2: 11
B.10. Write a program to ask a user to input a sequence of N positive numbers and then generate a table
of counts and percentages for the characteristics of each number such as:
- How many numbers are floating point numbers/integer numbers
- How many numbers start with 1, with 2, ..., or with 9
- How many numbers are odd/even
- How many numbers are in [0, 10), [10, 1000), or [1000, +infinity)
Input: N = 5
Sequence of 5 positive numbers: 3.4

5

8

2.59

17

Output:
1. Number type:
- Floating point numbers: 2

- Integer numbers: 3
2. Starting with:
- 1: 1
6


- 2: 1
- 3: 1
- 4: 0
- 5: 1
- 6: 0
- 7: 0
- 8: 1
- 9: 0
3. Odd/even:
- Odd: 2
- Even: 1
- N/A: 2
4. Range:
- [0, 10): 4
- [10, 1000): 1
- [1000, +infinity): 0
B.11. Write a program to ask a user to input a sequence of N digits and then generate a positive number
with the digits rearranged in descending order.
Input 1: N = 6
Sequence of 6 digits: 3 9 0 2 4 1
Output 1: 943210
Input 2: N = 7
Sequence of 7 digits: 0 0 1 6 5 2 1
Output 2: 6521100

B.12. Number merging is performed to combine two positive numbers by put their digits all together in
descending order. Write a program to receive two positive numbers, do number merging, and then
display a resulting number.
7


Input: Number 1 = 1204
Number 2 = 37502
Output: 754322100
B.13. Write a program to ask a user to input a positive number and then display the sum of its even
digits and the other sum of its odd digits returned by recursive functions.
Input 1: 283069
Output 1:
Even sum = 16
Odd sum = 12
Input 2: 137
Output 2:
Even sum = 0
Odd sum = 11
B.14. Write a program to receive a positive integer number and a digit and then return all the positions
(counting at 1 from the left to the right) where the given digit exists in the given positive number using
recursive functions.
Input 1: A positive number: 129825
A digit: 2
Output 1: 2, 5
Input 2: A positive number: 348029
A digit: 1
Output 2: 0
B.15. Given a structure to represent a binary operator as follows:
struct operator {

char anOp; //+, -, *, /, %, ^
double aValue;
struct operator lOperand;
8


struct operator rOperand;
};
If an element is an operand of an operator, its member aValue is determined.
Given an expression which is (9-2.2)*2 + (4/2) - 1, write a program to represent this expression using the
given structure and then evaluate this expression.
B.16. Check your coding style with the programs you have written. Should anything be changed with
your programs so that they can be not only executable but also readable? Rewrite your programs if yes.

9



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×