Array assistance please?

Hello, I was working on this problem, and I just need a point in the right direction....We're supposed to show the number of students taller than 60 inches and shorter than 55 inches along with the heights of the students that are taller than 60 inches and 55 inches, but all I get when I compile for the heights is the last height entered that matches the criteria... what am I doing wrong? Thanks so much!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <iostream>

using namespace std;

int main()
{
   	double height[10];
	int numHeight60 = 0;
	int numHeight55 = 0;
	double height60 = 0.0;
	double height55 = 0.0;

	for (int x = 0; x < 10; x = x + 1)
	{
		height[x] = 0.0;
	}

	cout << "You are asked to enter heights of 10 students. "<< endl;
	for (int x = 0; x < 10; x = x + 1)
	{
		cout << "Enter height of a student: ";
		cin >> height[x];
	}

	for (int x = 0; x < 10; x = x + 1)
	{
		if (height[x] > 60)
		{
			numHeight60 = numHeight60 + 1;
			height60 = height[x];
		}
		else if (height[x] < 55)
		{
			numHeight55 = numHeight55 + 1;
			height55 = height[x];
		}
	}
	
    cout << "Number of students taller than 60 inches: " << numHeight60 << endl;
    cout << "With heights: " << height60 << endl;
    cout << "Number of students shorter than 55 inches: " << numHeight55 << endl;
    cout << "With heights: " << height55 << endl;
    
    system("pause"); 
	return 0;
}
Last edited on
heigth60 is a variable that only can hold one (integer) number. You are hoping it will "remember" every value that matched the criteria, but in fact it just holds the last one. To store several numbers, you need several variables or an array or a vector.
I see! So in that case, should I make a second array to store height60 and another to store height55?
Exactly! Vectors are a better options. I guess at this point you don't know what they are, but keep in mind to grab the concept in the future.
One drawback of using arrays: you must create them with a size and can not use an index beyond its borders. Example:

1
2
3
4
int myArray[10];

myArray[10] = height[x];


this will cause the program to finish. And it's a subtle error for the untrained eye. I'm trying to acces eleventh place (yes 11th!) of the array (remember they start at index zero), which doesn't exist. It's certainly some place in the memory, but not allowed to acces just like that...
Vector save you a lot of truoble in exchange of learning some new synthax (a bit tricksome at first).
You already have an array with all the heights in it. Just loop through all the elements and cout only for the heights > 60 or < 55.
Example:
1
2
3
cout << "List of heights > 60 is: "
for (int x = 0; x < 10; x = x + 1)	
	if (height[x] > 60) cout << height[x] << ", ";

You can do it without any other arrays.
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.
Last edited on
Thank you all so much for your extensive assistance, this is wonderful, it helps so much!! Thank you again! =)
Topic archived. No new replies allowed.