954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Tolerance

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.

shmay
Junior Poster in Training
55 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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 []).

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

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

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 

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++;
shmay
Junior Poster in Training
55 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 
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"

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

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.

shmay
Junior Poster in Training
55 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 
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 :)

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

Thank you.

shmay
Junior Poster in Training
55 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You