我真的需要帮助解决这个问题。
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/