I. Experimental purpose and experimental requirements
(1) Proficient in the definition of parameterless function, parameterized function, function with no return value and function with return value.
(2) Understand the execution process of function calls and the value passing mode between formal parameters and actual parameters.
(3) Master the methods of nested function calls and recursive calls.
(4) Understand the idea of modular programming, and flexibly use the idea of functional modular design to solve practical problems in software development.
2. Experimental content
1, write two functions double getPer(double r); And double getArea(double r); Respectively find the circumference and area of the circle. Enter the radius of the circle from the main function, call two custom functions respectively to find the corresponding circumference and area, and output the complete information. (Note: The function declaration is given and must use the given function type, function name, and parameters).
#include <stdio.h> double getPer(double r); double getArea(double r); int main() { double r; double perimeter, area; printf("请输入圆的半径:"); scanf("%lf", &r); perimeter = getPer(r); area = getArea(r); printf("圆的半径为:%.2lf\n", r); printf("圆的周长为:%.2lf\n", perimeter); printf("圆的面积为:%.2lf\n", area); return 0; } double getPer(double r) { double perimeter = 2 * 3.14 * r; return perimeter; } double getArea(double r) { double area = 3.14 * r * r; return area; }
2, write the function fun, calculate s =1+1/2! + a third! +... +1/n! , where the value of n is determined by the user. Enter n in the main function to call the function.
#include <stdio.h> double factorial(int n); double calculateS(int n); int main() { int n; double s; int i; printf("请输入一个正整数n:"); scanf("%d", &n); s = calculateS(n); printf("s = %.5lf\n", s); return 0; } double factorial(int n) { double result = 1; int i; for (i = 1; i <= n; i++) { result *= i; } return result; } double calculateS(int n) { double s = 0; int i; for (i = 1; i <= n; i++) { double term = 1 / factorial(i); s += term; } return s; }
3. Write void DrawTriangle(int n, char c); To achieve the function of printing an isosceles triangle composed of n lines by the character variable c. The main function calls the function to print an isosceles triangle composed of 5 lines of '*' and 10 lines of '#' respectively.
#include <stdio.h> void DrawTriangle(int n, char c); int main() { int n; // 打印5行由字符‘*’组成的等腰三角形 printf("打印5行的等腰三角形:\n"); DrawTriangle(5, '*'); // 打印10行由字符‘#’组成的等腰三角形 printf("\n打印10行的等腰三角形:\n"); DrawTriangle(10, '#'); return 0; } void DrawTriangle(int n, char c) { int i, j; // 外层循环控制行数 for (i = 1; i <= n; i++) { // 内层循环控制每行的字符个数 for (j = 1; j <= i; j++) { printf("%c", c); } printf("\n"); // 换行 } }
4, write a function int digitSum(int n); Enters a non-negative integer and returns the sum of the numbers that make it up. For example, calling digitSum(1729) should return 1+7+2+9, whose sum is 19. (Note: can be modified from the textbook example 3.17 (circular implementation) or Example 4.10 (recursive implementation)).
#include <stdio.h> int digitSum(int n); int main() { int n; int sum; printf("请输入一个非负整数:"); scanf("%d", &n); sum = digitSum(n); printf("数字之和为:%d\n", sum); return 0; } int digitSum(int n) { int currDigit; // 当前数字 int remainingDigits; // 剩余数字 if (n == 0) { return 0; } currDigit = n % 10; remainingDigits = n / 10; return currDigit + digitSum(remainingDigits); }