using ceil and floor

Pages: 12
Nov 11, 2011 at 10:27pm
Hello can anyone help me on using ceil and floor. For example, when the grade is 85.678 it will round up to 86 or if its 85.324 it will round down to 85? I'm having trouble figuring it out with the code I have written.
Last edited on Nov 11, 2011 at 11:38pm
Nov 11, 2011 at 10:33pm
Your code doesn't contain ceil or floor.
Nov 11, 2011 at 10:47pm
yes, I know im asking how would use it in this code so I can get the correct answer. I'm not too familiar with it so I'm not sure how to use it.
Nov 11, 2011 at 10:48pm
You can do it like this:

percentage = ceil(percentage);

similarly for floor.

Be aware that with floating point numbers, 65.0000 == 65 is not always true, so you might have trouble with this sort of line:

else if (percentage == '65')


http://floating-point-gui.de/ and in particular http://floating-point-gui.de/errors/comparison/
Last edited on Nov 11, 2011 at 10:51pm
Nov 11, 2011 at 10:50pm
hmm if the program sample I have one of the scores is 85.783 and the letter grade is computed as B+. When I run my it computes it as a B. Thats why I was thinking I needed to use ceil and/or floor
Nov 11, 2011 at 10:57pm
1
2
3
else if (percentage == '65')

else if (percentage == '75')


In these two lines, '75' and '65' are not numbers - they are multi-character constants. I supect you meant these lines to be

1
2
3
else if (percentage == 65)

else if (percentage == 75)



Anyway, the following code gives B+ with a percentage value of 85.783

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
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>

using std::cout;
using std::endl;
int main()
{
  double percentage = 85.783;


if (percentage >= 95)
	{
		cout << "You got an A" << endl;
	}
	else if (percentage >= 90 && percentage < 95)
	{
		cout << "You got a A-" << endl;
	}
	else if (percentage > (85) && percentage < 90)
	{
		cout << "You got a B+" << endl;
	}
	else if (percentage == 85)
	{
		cout << "You got a B" << endl;
	}
	else if (percentage >= 80 && percentage < 85)
	{
		cout << "You got a B-" << endl;
	}
	else if (percentage > 75 && percentage < 80)
	{
		cout << "You got a C+" << endl;
	}
	else if (percentage == 75)
	{
		cout << "You got a C" << endl;
	}
	else if (percentage >= 70 && percentage < 75)
	{
		cout << "You got a C-" << endl;
	}
	else if (percentage > 65 && percentage < 70)
	{
		cout << "You got a D+" << endl;
	}
	else if (percentage == 65)
	{
		cout << "You got a D" << endl;
	}
	else if (percentage >= 60 && percentage < 65)
	{
		cout << "You got a D-" << endl;
	}
	else
	{
		cout << "You got a F" << endl;
	}

 return 0;
}
Last edited on Nov 11, 2011 at 10:58pm
Nov 11, 2011 at 11:04pm
hey thanks for the help. Not sure if this matters but in the sample the number is 85.9785 not 85.783 as i previously mentioned. Either way when I run my program i still get B instead of B+
Nov 11, 2011 at 11:05pm
Then you must have made a mistake in your code. If you show us your code, we can help you fix it.
Nov 11, 2011 at 11:08pm
OK i messed up again lol. When I run my program I get B+ but with the sample program I have been given it is B.
Last edited on Nov 11, 2011 at 11:09pm
Nov 11, 2011 at 11:17pm
When I try to use floor I get the error message: more than one instance of overloaded function floor matches the argument list
Nov 11, 2011 at 11:21pm
There is more than one floor function. Here they are:

http://cplusplus.com/reference/clibrary/cmath/floor/

Three of them. One takes a float, one take a double, and one takes a long double.

What kind of object are you passing into the floor function?
Nov 11, 2011 at 11:27pm
I am passing a double into the floor function
Nov 11, 2011 at 11:29pm
And are you including cmath or math.h ?
Nov 11, 2011 at 11:32pm
yes I have #include <cmath>
Nov 11, 2011 at 11:33pm
Let's see the line that complains.
Last edited on Nov 11, 2011 at 11:34pm
Nov 11, 2011 at 11:35pm
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
if (percentage >= 95)
	{
		cout << "You got an A" << endl;
	}
	else if (percentage >= 90 && percentage < 95)
	{
		cout << "You got a A-" << endl;
	}
	else if (percentage > (85) && percentage < 90)
	{
		cout << "You got a B+" << endl;
	}
	else if (percentage = floor (85))
	{
		cout << "You got a B" << endl;
	}
	else if (percentage >= 80 && percentage < 85)
	{
		cout << "You got a B-" << endl;
	}
	else if (percentage > 75 && percentage < 80)
	{
		cout << "You got a C+" << endl;
	}
	else if (percentage == 75)
	{
		cout << "You got a C" << endl;
	}
	else if (percentage >= 70 && percentage < 75)
	{
		cout << "You got a C-" << endl;
	}
	else if (percentage > 65 && percentage < 70)
	{
		cout << "You got a D+" << endl;
	}
	else if (percentage == 65)
	{
		cout << "You got a D" << endl;
	}
	else if (percentage >= 60 && percentage < 65)
	{
		cout << "You got a D-" << endl;
	}
	else
	{
		cout << "You got a F" << endl;
	}
Nov 11, 2011 at 11:37pm
else if (percentage = floor (85))

So you're passing the integer 85 into the function floor, and then making percentage equal to 85.

I shall say no more and you can fix it all.
Last edited on Nov 11, 2011 at 11:37pm
Nov 11, 2011 at 11:42pm
Hmmm I'm not understanding what your saying
Nov 11, 2011 at 11:44pm
You want the floor function to take your percentage value and round it down, yes?

So, you have to pass in your percentage value to the function. Like this: floor(percentage)

If you've not had to use functions before, you should definitely stop and read up on them:

http://www.cplusplus.com/doc/tutorial/functions/
Last edited on Nov 11, 2011 at 11:45pm
Nov 12, 2011 at 12:03am
So its still not working....
1
2
3
4
5
else if (percentage == 85)
	{
		percentage = floor (percentage);
		cout << "You got a B" << endl;
	}
Pages: 12