计算并找出 c 中成对值的差异

标签 c

我真的需要帮助解决这个问题。

Write a program that accepts six(6) pairs of values from the user and then calculates and stores the difference of each pair of values in an array.The array of calculated values should then be sorted into ascending order and printed on the screen.

我输入了六对值,我遇到的麻烦是差异和按升序存储。

如有任何帮助,我们将不胜感激。

#include <stdio.h>
main()
{
    int arr[12], num1, num2, i;
    for (i = 1; i < 7; i++) {
        printf("Enter first number for pair ");
        scanf("%d", &num1);

        printf("Enter  second number for pair ");
        scanf("%d", &num2);

    }
    if (num1 > num2)

        printf("arr[i-1=num1-num2 ");
    else

        printf("arr[i-1]=num2-num1 ");
    {

        for (i = 1; 1 < 7; i++)
            printf("%7d\n", arr[i]);
    }

    return (0);
}

最佳答案

读取两个值并存储值后,您需要立即检查差异。

    #include <stdio.h>
    main()
    {
        int arr[7], num1, num2, i;
        for (i = 0; i < 7; i++) {
            printf("Enter first number for pair ");
            scanf("%d", &num1);

            printf("Enter  second number for pair ");
            scanf("%d", &num2);

            //check differences now
            if(num1>num2)
            {
                    arr[i]=num1-num2;
                    }
            else
            {
                    arr[i]=num2-num1;
                    }
        }
    }

对于 vector 的排序,您可以使用冒泡排序算法。 http://www.algorithmist.com/index.php/Bubble_sort.c

更改 vector 和 for 限制的原因是因为 vector 位置从 0 到 n 而不是 1 到 n,这意味着 for 应该从 0 到 6(< 7 或 <= 6)。

关于计算并找出 c 中成对值的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6171212/

相关文章:

C: printf 中的 SEEK_END 显示意外值

c - 共享内存如何与数组一起使用?

c++ - 如何快速测试 C 或 C++ 片段?

c++ - 明显的 CUDA 魔法

c - asn1c 编译器不编译 asn 文件

c - 带有 Popover 菜单示例代码的 GTK3 C 菜单按钮(仅使用 .c,不使用 .ui)

c - 线程问题

c - 并行运行两个程序,每个程序都有无限循环

c++ - Opencv:用 imwrite 保存双矩阵

c - 使用 fork() 获取 child 的 child