Finding the average in C

THIS CONGRATULATIONS. Thought I'd let you know. if you could point me in a direction that will help me find out why this error occurs, I would appreciate it. http://pastebin.com/hDUpfrsu is my current code (see below). why does it return ONE when I enter (in that 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 lowest; 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; 

}

+4
source share
9 answers

A few questions:

  • low is defined as bool , it must be int with how you use it

  • myNumbers[2] is an array of size 2, it can contain only 2 numbers. Resize the declaration to 3.

  • Consider what happens if two or more values ​​are equal ...
+3
source

Replace

 bool lowest; 

 int lowest; 

since you want to store the integer in "lower", not a boolean.

+2
source

lowest is bool . Shouldn't that be something big?

+1
source

The answer to your question is that, as many others have pointed out, you are using the lowest type of boolean instead of the number type, for example int , which would be in harmony with the rest of your program.

 bool lowest; 

Boolean types, as expected, can contain basically two states: true and false . For historical reasons (i.e., mainly because of C inheritance), logical values ​​are associated with integers for which 0 means false, and any other value means true.

That's why the boolean type is still compatible with integers (this is the way to say it), and when you assign it to a null value, then it is false. If you assign it any other int value, it will act correctly. This happens on lines like this:

 lowest = myNumbers[0]; 

Finally, when you do:

 printf("\n%d", lowest); 

The reverse 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 is set: (int) true (in your program: (int) lowest ).

As you can imagine, in more than 90% of cases the input integer values ​​will be different from zero, so you get 1, regardless of the input.

+1
source

Adding to other answers.

You have:

 int myNumbers[2]; ... myNumbers[2] = GetInteger(); 

this is not true. myNumbers is an array of two elements, and the actual indices of the array are 0 and 1 . Since you want to keep elements 3 in the array, change its size to 3 .

0
source

Warning: your array is not large enough. When you declare an array, the index is not the maximum index of the array, but the number of elements, which is the maximum index + 1.

0
source

You can use the terminal operator (? :) . To find the lowest number or the highest b / w three numbers using the 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); 
0
source

Others pointed out obvious flaws in the program (array size, data type of the result). Here is a little bit that will allow you to understand the indexing of arrays, in particular w do they start from scratch ?

PS: I like the highest rated answer, but I advise you to read the whole post.

0
source

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 a lot of conditions!

0
source

Source: https://habr.com/ru/post/1338201/


All Articles