I must display the number of values that are above, equal to, and below the average, with "equal" being within a tolerance .01.

void RelToAve (double ave, int numRead, double *array)
{
	int abo = 0, equ = 0, bel = 0, i;

	for (i = 0; i < numRead; i++)
	{
		if (*array > ave)
			abo++;
		else if (*array < ave)
			bel++;
		else
			equ++;
		array++;
	}
	
	printf("\nAbove Average: %17i\n", abo);
	printf("Equal to Average: %14i\n", equ);
	printf("Below Average: %17i\n", bel);
}

How would I affect this function so that it would do that? I have an idea, but gotta make sure I get all of the points on this one. Thanks in advance.

Recommended Answers

All 7 Replies

You could post the "idea" as well.. Ppl will tell you if something is wrong.
One I can tell you is:

if (*array > ave)
else if (*array < ave)

Although this is inside the loop it'll always test only the first element of the array. To access other elements in the array you should use the subscript operator (operator []).

rather use C++
overload > < and == functions.

You could post the "idea" as well.. Ppl will tell you if something is wrong.
One I can tell you is:

if (*array > ave)
else if (*array < ave)

Although this is inside the loop it'll always test only the first element of the array. To access other elements in the array you should use the subscript operator (operator []).

that's why I have this sucker at the end: array++;

my idea was to do:

if (*array > (ave + .01))
     abo++;
else if (*array < (ave - .01))
     bel++;

that's why I have this sucker at the end: array++;

Sorry.. didn't see that one.. :)

Well, if this is an assignment I think the prof's idea is to teach floating point number comparison. See this thread I had posted. It has some description of the problems and has this link that describes in detail solutions and other problems.
After you've read it you'll understand that your problem stmt says you should use this method: "Comparing with epsilon – absolute error"

Thank you.

I've been toying around with fabs for a little bit, and cannot figure it out. It is late, and the assignment is due early.

Can you just tell me what I should put? I would probably learn it quicker that way anyhow.

Can you just tell me what I should put? I would probably learn it quicker that way anyhow.

If you had a look at the link this is what you would've seen:
-------------------------------------
Comparing with epsilon – absolute error

Since floating point calculations involve a bit of uncertainty we can try to allow for this by seeing if two numbers are ‘close’ to each other. If you decide – based on error analysis, testing, or a wild guess – that the result should always be within 0.00001 of the expected result then you can change your comparison to this:
if (fabs(result - expectedResult) < 0.00001)
The maximum error value is typically called epsilon.
-------------------------------------
I think that tells you exactly what to do.

It is late, and the assignment is due early.

It's always late and assignments are always due early :)

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.