2301_80263022 2024-05-22 08:03 采纳率: 90%
浏览 2
已结题

c语言函数应用的简单问题

img

img


图片上的几个函数应用题 用简单dec语言程序运行 简单易懂 不复杂 不炫技

  • 写回答

4条回答 默认 最新

  • 檀越@新空间 2024-05-22 08:03
    关注

    上午好☀️☀️☀️️
    本答案参考ChatGPT-3.5

    问题描述:

    有多个关于函数应用的问题,需要编写简单易懂的 C 语言程序来解决这些问题

    解决方案:

    3.编写 3 个求一元二次方程根的函数,通过主函数输入 3 个参数,调用子函数求一元二次方程的根,并在子函数中输出结果

    • 主函数输入三个参数,即一元二次方程的 3 个系数 a、b、c。
    • 在主函数中调用子函数 quadratic_equation() 来计算一元二次方程的根,并将计算结果赋值给变量 x1 和 x2。
    • 子函数 quadratic_equation() 中使用公式计算一元二次方程的根,并输出结果。

    示例代码:

    #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 在主函数中通过键盘输入

    • 首先,在主函数中输入两个整数,即 n 和 m。
    • 计算阶乘,再根据组合数公式,计算 n!/m!(n-m)! 的值。
    • 计算出结果后,在主函数中输出结果。

    示例代码:

    #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 数列的值

    • 在 b1() 函数中,使用循环语句计算 n 项的 Fibonacci 数列,并将最后一项的值作为函数的返回值。
    • 主函数中输入要计算 Fibonacci 数列的项数 n,然后调用 b1() 函数计算结果,将计算得到的值输出。

    示例代码:

    #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 列整数方阵,然后求两条对角线上各元素之和,返回此和数

    • 定义一个二维数组 arr,将所给的数组元素赋值给数组 arr。
    • 定义一个变量 sum,初始值设为 0,用来统计两条对角线上各元素之和。
    • 在 fun() 函数中输出一个 M 行 M 列的整数方阵,根据二维数组的下标访问数组元素,并将访问的元素加入 sum 中。
    • 函数 fun() 返回 sum。

    示例代码:

    #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.输入某年某月某日,判断这一天是这一年的第几天?

    • 在主函数中输入年月日,然后调用函数 day_of_year() 计算这一天是这一年的第几天。
    • 在函数 day_of_year() 中使用数组来储存每个月的天数,根据输入的日期,计算这一天是这一年的第几天。

    示例代码:

    #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 函数输入矩阵元素。将二维数组名作为函数实参,在执行函数的过程中实现矩阵转置,函数调用结束后在主函数中输出已转置的矩阵

    • 定义一个二维数组 matrix,使用 scanf() 函数输入矩阵元素。
    • 使用函数 transpose() 对矩阵进行转置,转置后的矩阵存放在原矩阵中。
    • 在主函数中输出转置后的矩阵。

    示例代码:

    #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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 6月1日
  • 已采纳回答 5月24日
  • 创建了问题 5月22日