1 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 examp[.]
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 examples for illustration B Practice B.1 Modularize the programs you have written for the problems in chapter 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 B.2.2 B.2.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: * ** *** **** 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 = Output: C(4,2) = 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: Output 3: the program is terminated B.6 Let's examine quadratic polynomial functions Each quadratic polynomial function has 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 - 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 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 - How many numbers are odd/even - How many numbers are in [0, 10), [10, 1000), or [1000, +infinity) Input: N = Sequence of positive numbers: 3.4 2.59 17 Output: Number type: - Floating point numbers: - Integer numbers: Starting with: - 1: - 2: - 3: - 4: - 5: - 6: - 7: - 8: - 9: Odd/even: - Odd: - Even: - N/A: Range: - [0, 10): - [10, 1000): - [1000, +infinity): 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 = Sequence of digits: Output 1: 943210 Input 2: N = Sequence of digits: 0 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, number merging, and then display a resulting number Input: Number = 1204 Number = 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 = 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 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: Output 1: 2, Input 2: A positive number: 348029 A digit: Output 2: B.15 Given a structure to represent a binary operator as follows: struct operator { char anOp; //+, -, *, /, %, ^ double aValue; struct operator lOperand; 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