Integer divsion

Hi, I'm kind of new here.

The thing is I want it when I (for example) enter 5 as x to get stars to equal 2, but the problem is I get 1. (I don't want it to be 1.66. I want it to be 2). However, I want it when I enter 4 as x (4/3=1.3) to get 1 (which is already happening)

I know this is not a good example, but I couldn't find a better way to explain it. Sorry
1
2
3
4
int stars, x;
cin >>x;
stars=(abs(x)/3);
cout << stars;

Thanks!
Sounds like x minus 3 would give you the result you want, but I'm not sure if that fits with all possible values of x you might have. What is the overall goal?
try modulus division
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
	cout << "5 % 3 = " << 5 % 3 << endl;
	cout << "4 % 3 = " <<  4 % 3 << endl;
	cin.ignore();
	return 0;
}
try modulus division
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
cout << "5 % 3 = " << 5 % 3 << endl;
cout << "4 % 3 = " << 4 % 3 << endl;
cin.ignore();
return 0;
}


The reminder thing doesn't work.

Here is what I want to do exactly.. I want to make a program that prints a star for every 3 points. For example let's say that a guy got 90 points (90/3=30) the program will print 30 stars next to each other.. so what I'm having a problem with is when the number isn't divisible by 3. For example, 92 (92/3=30.66) which is closer to 31. In this case I want the program to print out 31 stars, but what it ends up doing is printing only 30. I hope it is clearer now.
So you want to round up to the closest integer value? If you define the values as floating point (either float or double), so you aren't subject to integer division, you could use the ceiling function to round up. ceil() is in the <cmath> header.

ceil(30.66) will give 31.


Edit:
or -

you can use the floor function floor() - since you want to round down if the decimal part is below .5? You can add .5 to whatever result you get, then use the floor function. That will round to the closest integer.

example
4.0/3 = 1.33 -> 1.33 + .5 = 1.83 -> floor(1.83) = 1
92.0/3 = 30.66 -> 30.66 + .5 = 31.16 -> floor(31.16) = 31
Last edited on
If you're only worried about getting it to work when you divide by three, the following logic will help:
1
2
3
4
5
6
int stars, x;
cin >>x;
stars=(abs(x)/3);
int remainder = (abs(x) % 3);
if (remainder == 2) stars++;
cout << stars;

Basically, you're grabbing the remainder and seeing whether it is big enough to warrant incrementing the value of your quotient.

If you're only worried about getting it to work when you divide by three, the following logic will help:

1
2
3
4
5
6
int stars, x;
cin >>x;
stars=(abs(x)/3);
int remainder = (abs(x) % 3);
if (remainder == 2) stars++;
cout << stars;

Basically, you're grabbing the remainder and seeing whether it is big enough to warrant incrementing the value of your quotient.


This is great, thanks for help. Only problem is I decided to make x a float, and apparently I can't do remainder operation of floats.

Edit:
or -

you can use the floor function floor() - since you want to round down if the decimal part is below .5? You can add .5 to whatever result you get, then use the floor function. That will round to the closest integer.

example
4.0/3 = 1.33 -> 1.33 + .5 = 1.83 -> floor(1.83) = 1
92.0/3 = 30.66 -> 30.66 + .5 = 31.16 -> floor(31.16) = 31


This works for me! Thanks!
Topic archived. No new replies allowed.