开发者

calculating and finding difference in pairs of values in c

开发者 https://www.devze.com 2023-03-09 07:16 出处:网络
I really need help with this question. Write a program that accepts six(6) pairs of values from the user and then

I really need help with this question.

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.

I got through with inputting the six pairs of values, what I'm getting trouble with is the difference and storing in ascending order.

Any help given would be greatly appreciated.

#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);
}


You need to check the difference immedialy after you read both values and store the value.

    #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;
                    }
        }
    }

For the ordering of the vector, you could use a bubble-sort algorythm. http://www.algorithmist.com/index.php/Bubble_sort.c

The reason for changing the vector and for limits is because vector positions go from 0 to n and not 1 to n, meaning that the for should go from 0 to 6 ( < 7 or <= 6 ).

0

精彩评论

暂无评论...
验证码 换一张
取 消