Also, you have several for loops - you could combine the loops in lines 20 -38 into 1. Then have
fun2code's loop after that.
The loop in lines 14 - 17 isn't necessary if you are going to put values into the array straight after.
Edit:
Initialisation of arrays is a good idea - not doing it is often a source of errors, but I don't see the point of initialising twice. Put a comment to say this is where initialisation is happening.
Rather than have the magic number 10 throughout your code, do this:
1 2 3 4 5 6 7 8 9 10
|
int main()
{
const int SIZE = 10; // if you need to change you can do it once here
// .... your code
int x; // x is still in scope when loop finishes
for (x = 0; x < SIZE; ++x) { // ++x is better than x = x +1
// .... your code
}
|
Now, Floating Point (FP include doubles & floats) are a pain because they are stored as binary fractions & cannot represent every real number exactly. Doubles have about 16 significant figures of precision. The value that is stored is almost always the number you want + / - a small number (see epsilon below)
The problem you might have is that 55.0 might be 54.99999999999997 & 60.0 might be 60.00000000000002, so values entered as 55.0 or 60.0 might still appear in the lists even though the criteria was strictly less than or strictly greater than respectively. I don't know whether this is actually the case for these particular numbers, but it best to use the method shown below so that it always works.
One way to fix this, is you need to have a PRECISION variable as well, and use it in a way that means absolute of ( number - expected value) is less than PRECISION means equal, and anything bigger than the number + PRECISION is greater than, and anything smaller than the number - PRECISION is less than:
1 2 3 4 5 6 7
|
const double PRECISION = 0.01;
const double MAXHEIGHT = 60.0;
const double MINHEIGHT = 55.0;
if ( height[x] > (MAXHEIGHT + PRECISION) ) { //always use braces - even for 1 statement
cout << height[x] << ", ";
}
|
The problem now is that the PRECISION number can suffer from exactly same problem as any other FP number !!! Meaning that 66.01 or 54.99 sometimes won't be included in the lists, although it does depend on the value of the PRECISION number as to how that behaves exactly :(
There is a constant called epsilon, such that 1.0 + epsilon is the next representable number above 1.0. For a double, epsilon is about 1e-16. Worse, the epsilon gets bigger along with the number because the gaps between representable numbers aren't constant, so at 1e16, epsilon is about 1.0.
You can use
std::numeric_limits<double>::epsilon()
to find out the exact value of epsilon on your system.
So, to do this absolutely correctly, one has to scale the epsilon, and use it as well as the PRECISION.
All this is probably way beyond what you thought was in the assignment, but if you include it - you ought to get some major brownie points :)
Hope all goes well.