Mwahahahaha

(This problem was partly made by someone else.) In C++, what will the program print? Sum all the printed fractions, then add the numerator and denominator of the resulting (simplified) fraction, then square the sum.

 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
61
#include <iostream>
#include <string>

using namespace std;

struct character
{
    string name;
    int hitPoints;
    int maxHitPoints;
};

void harmCharacter(character characterToHarm, int health)
{
    characterToHarm.hitPoints -= health;

    if(characterToHarm.hitPoints < 0)
    {
        characterToHarm.hitPoints = 0;
    }
}    

void healCharacter(character characterToHeal, int health)
{
    characterToHeal.hitPoints += health;

    if(characterToHeal.hitPoints > characterToHeal.maxHitPoints)
    {
        characterToHeal.hitPoints = characterToHeal.maxHitPoints;
    }
}

int main()
{
    character player = {"Col. Mustard", 10, 15};

    cout << player.hitPoints << "/" << player.maxHitPoints << endl;

    harmCharacter(player, 7);

    cout << player.hitPoints << "/" << player.maxHitPoints << endl;

    healCharacter(player, 16);

    cout << player.hitPoints << "/" << player.maxHitPoints << endl;

    harmCharacter(player, 4);

    cout << player.hitPoints << "/" << player.maxHitPoints << endl;

    healCharacter(player, 1);

    cout << player.hitPoints << "/" << player.maxHitPoints << endl;

    cout << "Col. Mustard SUCKS!!! Professor Plum and Miss Scarlet are SO MUCH BETTER!!!!!" << endl;

    cout << endl;
    cout << endl;

    return 0;
} 


The answer is 169.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Ivan Koswara
Jul 20, 2015

Note that in the definition of harmCharacter , the first argument has type character instead of character* (a pointer to a character ). Thus the whole struct is copied, and only the copy is modified; the original is left untouched. Likewise with healCharacter . Thus in all cases, player is never modified, and thus there will be five lines of 10/15 . The sum of them is 10 3 \frac{10}{3} .

This is most certainly a bug, as harmCharacter and healCharacter would do nothing this way. The fix is to change the arguments into of type character* instead, so that we have a pointer to the original player and thus we can modify it in the functions.

Moderator note:

Good eye, Ivan!

Nice solution! However, an easier way is just to put &characterToHarm instead of characterToHarm, and do the same for characterToHeal.

Kevin Li - 5 years, 10 months ago

Log in to reply

One of the reasons (why I didn't do that) is that I'm not that proficient in C++, so I forgot a few such tricks.

Ivan Koswara - 5 years, 10 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...