Hello,
I am running Xcode 5.0.2 on OSX 10.9.1.
So today is my first day using the forum and posting, and this is already my second post. At first, I wasn't going to post this, but this occurrence is so strange I wanted to check with other people if they have the same thing happen.
I was coding for a C++ program that sums keyboard input numbers, returns the average, and returns the number of input elements that are greater than the average. While trying different inputs, I got a result I was not expecting. Here is my code (not from the program I was working on, but a separate file I created to test the abnormality):
I know this is not the most optimized code (i.e. I could use an array, a while loop, and a for loop) and that I am not following standard coding practices (variable names, etc); I just wanted to get the rough idea out.
When I input the three numbers 13.34, 14.34, and 15.34, I get the following output:
I get the correct average, but the output suggests that two of the three entries are above the average, which is not true in this case! The weird thing is that in the second comparison it seems that (14.34 > 14.34) evaluates to true!
Changing the input numbers to {10.34, 11.34, 12.34}, {11.34, 12.34, 13.34}, {12.34, 13.34, 14.34}, or {14.34, 15.34, 16.34} gives the same result. And changing the order of the comparisons doesn't seem to change anything either.
I have tried this both in Xcode and in Terminal.
I have no clue why this happens, does anyone have any ideas?
I am running Xcode 5.0.2 on OSX 10.9.1.
So today is my first day using the forum and posting, and this is already my second post. At first, I wasn't going to post this, but this occurrence is so strange I wanted to check with other people if they have the same thing happen.
I was coding for a C++ program that sums keyboard input numbers, returns the average, and returns the number of input elements that are greater than the average. While trying different inputs, I got a result I was not expecting. Here is my code (not from the program I was working on, but a separate file I created to test the abnormality):
Code:
// strange.cpp
#include <iostream>
int main()
{
using namespace std;
cout << "Enter three numbers: ";
double n1, n2, n3;
cin >> n1 >> n2 >> n3;
double total = 3.0;
double average = (n1 + n2 + n3) / total;
cout << "Average: " << average << endl;
int aboveAverage = 0;
if (n1 > average)
aboveAverage++;
cout << "After first comparison, aboveAverage = " << aboveAverage << endl;
if (n2 > average)
aboveAverage++;
cout << "After second comparison, aboveAverage = " << aboveAverage << endl;
if (n3 > average)
aboveAverage++;
cout << "After third comparison, aboveAverage = " << aboveAverage << endl;
cout << "Above average: " << aboveAverage << endl;
return 0;
}
I know this is not the most optimized code (i.e. I could use an array, a while loop, and a for loop) and that I am not following standard coding practices (variable names, etc); I just wanted to get the rough idea out.
When I input the three numbers 13.34, 14.34, and 15.34, I get the following output:
Code:
Enter three numbers: 13.34
14.34
15.34
Average: 14.34
After first comparison, aboveAverage = 0
After second comparison, aboveAverage = 1
After third comparison, aboveAverage = 2
Above average: 2
Program ended with exit code: 0
I get the correct average, but the output suggests that two of the three entries are above the average, which is not true in this case! The weird thing is that in the second comparison it seems that (14.34 > 14.34) evaluates to true!
Changing the input numbers to {10.34, 11.34, 12.34}, {11.34, 12.34, 13.34}, {12.34, 13.34, 14.34}, or {14.34, 15.34, 16.34} gives the same result. And changing the order of the comparisons doesn't seem to change anything either.
I have tried this both in Xcode and in Terminal.
I have no clue why this happens, does anyone have any ideas?
Last edited: