finding the middle number in C
THIS IS HOMEWORK. thought i'd let you know. if you could point me in the direction that would help me discover why this error is happening, i'd appreciate it.
http://pastebin.com/hDUpfrsu is my current code (included below). why does it return ONE when i enter (in this order) 5, 6, 7
or other sequences?
#include <stdio.h>
#include <simpio.h>
#include <genlib.h>
/* finds the minimum among three integers using minimal amount of relational operations */
int main()
{
int myNumbers[2];
bool l开发者_StackOverflowowest;
printf("Enter the first integer...\t");
myNumbers[0] = GetInteger();
printf("Enter the second integer...\t");
myNumbers[1] = GetInteger();
printf("Enter the third integer...\t");
myNumbers[2] = GetInteger();
if (myNumbers[0] < myNumbers[1] && myNumbers[0] < myNumbers[2])
{
lowest = myNumbers[0];
}
if (myNumbers[0] > myNumbers[1] && myNumbers[1] < myNumbers[2])
{
lowest = myNumbers[1];
}
if (myNumbers[0] > myNumbers[2] && myNumbers[1] > myNumbers[2])
{
lowest = myNumbers[2];
}
printf("\n%d", lowest);
getchar();
return 0;
}
A few issues:
lowest is defined as a
bool
, it should be anint
with how you are using itmyNumbers[2]
is an array of size 2, it can only hold 2 numbers. Change the size declaration to 3.- Consider what would happen if two or more of the values were equal...
Replace
bool lowest;
by
int lowest;
since you want to store an integer number in 'lowest', not a boolean.
lowest
is a bool
. Shouldn't it be something larger?
The answer to your question is that, as many others have pointed out, you're using lowest as a boolean type, instead of a number type, such as int, which would be in harmony with the rest of your program.
bool lowest;
Boolean types, as expected, can hold basically two states: true and false. For historical reasons (i.e., mainly because of the C inheritance), boolean values have been associated with integer numbers, for which 0 meant false and any other value meant true.
That's why the Boolean type is still compatible with integers (that's a way of saying this), and when you assign it to a zero, then it holds false. If you assign to it any other int value, it will hold true. This is happening in lines such as this one:
lowest = myNumbers[0];
Finally, when you execute:
printf("\n%d", lowest);
The inverse process takes place, and true is converted to int (since you specified %d in the printf format string), and true is converted to 1, which is the default integer value for true in the bool type when its integer value: (int) true
(in your program: (int) lowest
) is asked.
As you can imagine, more than 90% of times the input integer values are going to be different from zero, that's why you are obtaining 1, no matter the input.
Adding to other's answers.
You have:
int myNumbers[2];
...
myNumbers[2] = GetInteger();
this is incorrect. myNumbers
is an array of 2 elements and valid array indices are 0
and 1
. Since you want to store 3
elements in the array change its size to 3
.
Warning: Your array isn't big enough. When you declare an array, the index isn't the maximum index of the array, but the number of elements, which is the maximum index + 1.
You can use Ternary Operator ( ? :) To find lowest number or highest b/w three numbers with minimum number of relational operators(As you said).
int low, lowest;
low = (number[0] < number[1]) ? number[0] : number[1];
lowest =(low < number[2]) ? low : number[2];
printf("%d",lowest);
Others have pointed about the obvious flaws in the program (viz size of array, data type of the result). Here is a bit which will make you understand the indexing of arrays, specifically why they start from zero?
PS: I like the top rated answer but i advise you to read the entire post.
This question has already been answered so I will post a solution that is much simpler:
int lowest = myNumbers[0];
if (myNumbers[1] < lowest)
{
lowest = myNumbers[1];
}
if (myNumbers[2] < lowest)
{
lowest = myNumbers[2];
}
printf("lowest value is: %d\n", lowest);
You only need that many conditions!
精彩评论