

图片上的几个函数应用题 用简单dec语言程序运行 简单易懂 不复杂 不炫技
上午好☀️☀️☀️️
本答案参考ChatGPT-3.5
问题描述:
有多个关于函数应用的问题,需要编写简单易懂的 C 语言程序来解决这些问题
解决方案:
3.编写 3 个求一元二次方程根的函数,通过主函数输入 3 个参数,调用子函数求一元二次方程的根,并在子函数中输出结果
示例代码:
#include <stdio.h>
#include <math.h>
void quadratic_equation(float a, float b, float c, float *x1, float *x2)
{
float delta = b * b - 4 * a * c;
if(delta < 0) {
printf("There is no real root.\n");
} else if(delta == 0) {
*x1 = -b / (2 * a);
printf("There is one root: %.2f\n", *x1);
} else {
*x1 = (-b + sqrt(delta)) / (2 * a);
*x2 = (-b - sqrt(delta)) / (2 * a);
printf("There are two roots: %.2f and %.2f\n", *x1, *x2);
}
}
int main(void)
{
float a, b, c, x1, x2;
printf("Enter the coefficients a, b and c: ");
scanf("%f%f%f", &a, &b, &c);
quadratic_equation(a, b, c, &x1, &x2);
return 0;
}
4.编写一个阶乘函数,通过主函数调用阶乘函数函数,求组合数 n!/m!(n-m)! 的值,12 和 8 在主函数中通过键盘输入
示例代码:
#include <stdio.h>
int factorial(int n)
{
int result = 1;
for(int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main(void)
{
int n, m;
printf("Enter two integers: ");
scanf("%d%d", &n, &m);
int result = factorial(n) / (factorial(m) * factorial(n - m));
printf("%d\n", result);
return 0;
}
5.编写一个函数 b1(),求 Fibonacci 数列的值,结果由函数返回,其中 Fibonacci 数列 F(n) 定义为:F(0) = 1,F(1) = 1,F(n) = F(n-1) + F(n-2)。在主函数中输入当 n 为 40 时,输出 Fibonacci 数列的值
示例代码:
#include <stdio.h>
int b1(int n)
{
int f0 = 1, f1 = 1, fn = 1;
for(int i = 2; i <= n; i++) {
fn = f0 + f1;
f0 = f1;
f1 = fn;
}
return fn;
}
int main(void)
{
int n = 40;
int result = b1(n);
printf("The %dth term of the Fibonacci sequence is %d.\n", n, result);
return 0;
}
6.给定数组 {1,2,3,4,5},{4,3,2,1,0},{6,7,8,9,0},{9,8,7,6,5},{3,4,5,6,7},设计一个函数 fun(),其功能是:输出 M 行 M 列整数方阵,然后求两条对角线上各元素之和,返回此和数
示例代码:
#include <stdio.h>
int fun(int arr[][5], int M)
{
int sum = 0;
printf("The matrix is:\n");
for(int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
printf("%d ", arr[i][j]);
if(i == j || i + j == M - 1)
sum += arr[i][j];
}
printf("\n");
}
return sum;
}
int main(void)
{
int arr[][5] = {{1,2,3,4,5},{4,3,2,1,0},{6,7,8,9,0},{9,8,7,6,5},{3,4,5,6,7}};
int M = 5;
int sum = fun(arr, M);
printf("The sum of the elements on the diagonals is %d.\n", sum);
return 0;
}
7.输入某年某月某日,判断这一天是这一年的第几天?
示例代码:
#include <stdio.h>
int day_of_year(int year, int month, int day)
{
int days_of_month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int days = 0;
for(int i = 1; i < month; i++) {
days += days_of_month[i];
}
days += day;
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
if(month > 2) {
days++;
}
}
return days;
}
int main(void)
{
int year, month, day;
printf("Enter the date (yyyy-mm-dd): ");
scanf("%d-%d-%d", &year, &month, &day);
int days = day_of_year(year, month, day);
printf("Today is the %dth day of %d.\n", days, year);
return 0;
}
8.将一个 3×3 的矩阵转置,用一函数实现之。在主函数中用 scanf 函数输入矩阵元素。将二维数组名作为函数实参,在执行函数的过程中实现矩阵转置,函数调用结束后在主函数中输出已转置的矩阵
示例代码:
#include <stdio.h>
void transpose(int matrix[][3])
{
for(int i = 0; i < 3; i++) {
for(int j = i + 1; j < 3; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
int main(void)
{
int matrix[3][3];
printf("Enter the matrix (3x3):\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
scanf("%d", &matrix[i][j]);
}
}
transpose(matrix);
printf("The transposed matrix is:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}