Please help with a task

I started learning c++ not a long time ago and i cannot complete one task, more
specifically I can't find remnant. What am I doing wrong? Btw excuse me for bad English, it's not my first language and I was the one to translate the task.
Thank you.


John has n amount of balloons. He decided to share them with friends equally. k amount of balloons exploded .
The rest John divided with d amount of friends. If there was any ballons left after division, John took them.
How many balloons every single friend got and how many ballons John got?

Check yourself with: If n = 77, d = 7 and k = 3, then every single friend got = 9 balloons, and John got 11 balloons.


#include <iostream>
#include <cmath>
using namespace std;
int main ()
{

int n; //amount of balloons
int m; //amount of balloons after explosions
int k; //amount of explosions
int d; //amount of friends
int h; //amount of people to divide it with
int k7, k6;
int u; //1

cout << "write amount of balloons: "; cin >> n;
cout << "write amount of explosions: "; cin >> k;
cout << "write amount of friends: "; cin >> d;
u = 1;
m = n - k;
h = d + 1;

k7 = m / h;
k6 = k7 + k1; // amount of balloons John got

cout << "ballons :" << endl;
cout << " 1 friend got ------> " << k7 << endl;
cout << " John got ----->" << k6 << endl;

return 0;
}



J123654 (1)
I started learning c++ not a long time ago and i cannot complete one task, more
specifically I can't find remnant. What am I doing wrong? Btw excuse me for bad English, it's not my first language and I was the one to translate the task.
Thank you.


John has n amount of balloons. He decided to share them with friends equally. k amount of balloons exploded .
The rest John divided with d amount of friends. If there was any ballons left after division, John took them.
How many balloons every single friend got and how many ballons John got?

Check yourself with: If n = 77, d = 7 and k = 3, then every single friend got = 9 balloons, and John got 11 balloons.


#include <iostream>
#include <cmath>
using namespace std;
int main ()
{

int n; //amount of balloons
int m; //amount of balloons after explosions
int k; //amount of explosions
int d; //amount of friends
int h; //amount of people to divide it with
int k7, k6;
int u; //1

cout << "write amount of balloons: "; cin >> n;
cout << "write amount of explosions: "; cin >> k;
cout << "write amount of friends: "; cin >> d;
u = 1;
m = n - k;
h = d + 1;

k7 = m / h;
k6 = k7 + k1; // amount of balloons John got

cout << "ballons :" << endl;
cout << " 1 friend got ------> " << k7 << endl;
cout << " John got ----->" << k6 << endl;

return 0;
}



you want % operator.
11/5 is 2
11%5 is 1

77 - 3 is 74
there are 8 people
74/8 is 9 each
74%8 is 2 left over
9+2 = 11 for J

you have way, way too many variables.
you need: number of balloons, a temporary variable, number of friends. Nothing else.
get n
get number exploded as temp
n -= temp;
//you can reuse temp for friends if you want to reduce variables by one more.
get friends or temp..
friends++; //one more for John
cout << n/friends //number each friend gets
cout << n/friends + n%friends //number John got


----------
and off the deep end, if you don't use %:
% is approximate in floating point, but you can round up.
74/8 as floating point is 9.25
.25 * 8 is 2
look familiar? Unfortunately, its not always this clean, you could get something like 4.999999999 which means '5' so you need a round() on it and even more if you allow negative values (not useful for this problem).
Last edited on
I interpreted the problem a bit differently:

77 - 3 = 74 (available) balloons, divided equally (as possible) among 7 friends (not 8 people) = 10 balloons per friend, with 4 left over for John.

In C:

n -= k;
balloonsPerFriend = n / d;
balloonsForJohn = n - (balloonsPerFriend * d);

no need for the % operator nor floats.

Also: the example solution isn't possible, as per the story, John would never have more balloons than each friend would get.
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
/*
 John has n amount of balloons.
 He decided to share them with friends equally. k amount of balloons exploded .
 The rest John divided with d amount of friends. If there was any ballons (sic) left
 after division, John took them.
 How many balloons every single friend got and how many ballons (sic) John got?
 */


#include <iostream>

int main()
{
    int n{77}; // John has n balloons.
    
    int k{3}; // k no. of balloons exploded.
    int d{7}; // d no. of friends
    
    int no_distributed = n - k;
    
    int no_per_friend = no_distributed / d; // The rest divided with d friends.
    int no_john_got = no_distributed % d;
    
    std::cout << no_per_friend << ' ' << no_john_got << '\n';
    
    return 0;
}


10 4
Program ended with exit code: 0
First thing I'd do is change the names of the variables to something meaningful. The logic in the code then becomes easier to follow. See below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    int totalBalloons{77};
    int poppedBalloons{3};
    int numFriends{7};

    int balloonsToDivide = totalBalloons - poppedBalloons;
    int peopleToDivideWith = numFriends + 1;  // +1 to include John himself!
    int ballonsPerFriend = balloonsToDivide / peopleToDivideWith;
    int extraBalloons = balloonsToDivide % peopleToDivideWith;

    std::cout << "Each friend got " << ballonsPerFriend << " balloons.\n";
    std::cout << "John got " << ballonsPerFriend + extraBalloons << " balloons.\n";
}


Each friend got 9 balloons.
John got 11 balloons.
Thanks to everyone, I already solved it!
Topic archived. No new replies allowed.