此程序旨在采用3个整数并按升序排列。当我输入" 12 8 2"等组合时,我得到" 1995099040 8 12"的输出。任何有关我为什么得到这个答案的帮助?
#include <stdio.h>
#include <stdlib.h>
int findBig(int d, int e, int f) {
if ((d >= e) && (d >= f)) {
return d;
} else
if ((e >= d) && (e >= f)) {
return e;
} else
if ((f >= e) && (f >= d)) {
return f;
}
}
int main(void) {
const int MAX_NUM = 3;
int userArray[MAX_NUM];
int bigNum;
int midNum;
int smallNum;
int j = 0;
int a;
int b;
int c;
printf("Please enter three integers separated by spaces:\n");
for (j = 0; j < MAX_NUM; ++j) {
scanf("%d", &userArray[j]);
}
a = userArray[0];
b = userArray[1];
c = userArray[2];
bigNum = findBig(a, b, c);
if (bigNum == a) {
midNum = findBig(0, b, c);
} else
if (bigNum == b) {
midNum = findBig(a, 0, c);
} else
if (bigNum == c) {
midNum = findBig(a, b, 0);
}
if ((midNum == a) && (bigNum == b)) {
smallNum = c;
} else
if ((midNum == b) && (bigNum == c)) {
smallNum = a;
} else
if ((midNum == c) && (bigNum == a)) {
smallNum = b;
}
printf("%d %d %d\n", smallNum, midNum, bigNum);
return 0;
}
答案 0 :(得分:7)
smallNum
保留其创建时获得的垃圾值,因为您没有涵盖bigNum
和midNum
可能的值的所有情况,因此它不会t在某些情况下会被分配一个值 - 就像你遇到问题的那样 - 并保持未初始化状态。
确保检查所有可能的情况:
if((midNum == a) && (bigNum == b)){ smallNum = c; }
else if((midNum == b) && (bigNum == c)){ smallNum = a; }
else if((midNum == c) && (bigNum == a)){ smallNum = b; }
else if((midNum == b) && (bigNum == a)){ smallNum = c; }
else if((midNum == c) && (bigNum == b)){ smallNum = a; }
else if((midNum == a) && (bigNum == c)){ smallNum = b; }
另外,考虑使用内置函数来节省时间和代码 - 您可以使用max
和min
函数来帮助您。
要查找最多三个号码,您可以使用max(max(a, b), c)
- 这样我们就可以将代码缩短为
smallNum = min(min(a, b), c);
bigNum = max(max(a, b), c);
midNum = (a + b + c) - smallNum - bigNum;
(这会使代码减半并使findBig
无需渲染)
答案 1 :(得分:1)
确定smallNum需要考虑6个案例。
if((midNum == a) && (bigNum == b) || ((midNum == b) && (bigNum == a))){
smallNum = c;
}
else if((midNum == b) && (bigNum == c) || ((midNum == c) && (bigNum == b))){
smallNum = a;
}
else if((midNum == c) && (bigNum == a) || ((midNum == a) && (bigNum == c))){
smallNum = b;
}
垃圾是由于遗失的情况造成的。
答案 2 :(得分:0)
您获得垃圾输出,因为您的代码调用了未定义的行为:
对于输入12 8 2
,案例(midNum == b) && (bigNum == a)
未在代码中处理,smallNum
保持未初始化。
您可以通过使用3个测试和交换重新排列数组来简化代码:
#include <stdio.h>
int main(void) {
const int MAX_NUM = 3;
int userArray[MAX_NUM];
int x;
printf("Please enter three integers separated by spaces:\n");
for (int j = 0; j < MAX_NUM; ++j) {
if (scanf("%d", &userArray[j]) != 1)
exit(1);
}
if (userArray[0] > userArray[1]) {
x = userArray[0]; userArray[0] = userArray[1]; userArray[1] = x;
}
if (userArray[1] > userArray[2]) {
x = userArray[1]; userArray[1] = userArray[2]; userArray[2] = x;
}
if (userArray[0] > userArray[1]) {
x = userArray[0]; userArray[0] = userArray[1]; userArray[1] = x;
}
printf("%d %d %d\n", userArray[0], userArray[1], userArray[2]);
return 0;
}
答案 3 :(得分:0)
我同意其他答案 - 您的代码并没有考虑可以订购价值的所有6种可能性。
解决此问题的一种可行方法:首先,修改您的&#34;找到最大值&#34;函数,因此它返回索引而不是值:
int findBig(int d, int e, int f) {
if ((d >= e) && (d >= f)) {
return 0; // index of largest element is 0
} else
if ((e >= d) && (e >= f)) {
return 1; // index of largest element is 1
} else
if ((f >= e) && (f >= d)) {
return 2; // index of largest element is 2
}
}
然后,找出哪个数字是大,小和中位数:
bigIndex = findBig(a, b, c);
smallIndex = findBig(-a, -b, -c); // a hack to find the minimum instead of maximum
if (bigIndex == smallIndex)
{
// Edge case - all numbers are equal
// Doesn't matter which index is which; just choose 3 different ones
bigIndex = 0;
midIndex = 1;
smallIndex = 2;
}
else
{
// Choose the index that is neither big nor small
midIndex = 3 - bigIndex - smallIndex;
}
然后打印出来:
printf("%d %d %d\n", userArray[smallIndex], userArray[midIndex], userArray[bigIndex]);
这有助于您拥有一个阵列。否则,仍然可以将索引转换为值很难看的值:
printf("%d %d %d\n",
smallIndex == 0 ? a : smallIndex == 1 ? b : c,
midIndex == 0 ? a : midIndex == 1 ? b : c,
bigIndex == 0 ? a : bigIndex == 1 ? b : c);