-2

I know, the question is really complicated. Believe me, I am too. But I think a better programmer than me should know the solution. I have the following:

int cash[5] = {90000, 50000, 50000, 20000, 0};
int bankaccounts[3];
int dicenumberforeachplayer[3] = {6, 3, 9}; //Every indice is a player,
so dicenumberforeachplayer[0] is player one and so on

I have to add the highest cashnumber to the player, with the highest dicenumber, then the second player the second highest cash and so on. but I really don't know how. I am really confused, bc I am working on a project all day and I've come really far (i've written maybe 3000 lines of code) but I really hate to stop and confuse me with this problem for hours. Pls help me :((

I think, that I have to sort the player or sth like this and then I could use a for-loop for inserting the cash in every acc but I am really tired and I can't think right now XD

Turksat46
  • 9
  • 4
  • [std::max_element](https://en.cppreference.com/w/cpp/algorithm/max_element) can help you out. – user4581301 May 26 '21 at 19:18
  • Hmm, first of all, thx for your reply, second, how do I then cycle through? Because I have to then give the second player the second highest cash and so on. I think I should edit that in my question :| – Turksat46 May 26 '21 at 19:20
  • Find the index of the player that has the highest dice roll. This will ostensibly be `0`, `1` or `2` (unless there are ties, in which case you'll have to tell us what you want to do). – Wyck May 26 '21 at 19:21
  • If it is a tie, I want to add to the first player of the tie the highest number and so on – Turksat46 May 26 '21 at 19:23
  • Sorry guys, I'm a german and my english is pretty bad andd it is my first time asking a question here – Turksat46 May 26 '21 at 19:24
  • Perhaps you should focus on making the problem smaller. How would you find the highest roller? Write a small program that just does that. When you have that working correctly, write another small program that builds on the first and writes out the locations of the rolls in descending order. Once it works write another program that enhances the second program to print out the locations and the money they won. Once that works you should be able to see the next step for yourself. When you can't figure out how to solve a big problem, turn it into smaller problems. – user4581301 May 26 '21 at 19:41
  • Yeah, i think you are right and I even tried to do that like two hours ago and I can sort the players by how much dices they have, but I only dont know how to add it to the accounts to the appropiate player. Btw, i am 17 and not a pro, so thats a bad thing also. xD – Turksat46 May 26 '21 at 19:51

1 Answers1

1

Modern C++ approach. There are no Magic numbers. Every thing is governed by the size of its respective array.

#include <algorithm>
#include <iostream>

int main()
{
    int cash[5] = {90000, 50000, 50000, 20000, 0};
    int bankaccounts[3] = {}; // added initializer to zero accounts
    int dicenumberforeachplayer[3] = {6, 3, 9};

    // iterate until we run out of prizes or players, whichever comes first
    for (size_t cashcounter = 0;
         cashcounter < std::min(std::size(dicenumberforeachplayer),
                                std::size(cash));
         cashcounter++)
    {
        // find location in array of highest-rolling player
        auto location = std::max_element(std::begin(dicenumberforeachplayer),
                                         std::end(dicenumberforeachplayer));

        // transform location into array index
        auto index = std::distance(std::begin(dicenumberforeachplayer),
                                   location);

        //increase money in bank account for this player
        bankaccounts[index] += cash[cashcounter];

        // give winner impossibly low dice roll so they don't win again next time.
        // Assumption: rolls cannot be negative. If they can, -1 might not be small enough
        dicenumberforeachplayer[index] = -1;
    }

    // for display and test, print all accounts
    for (const auto & val : bankaccounts)
    {
        std::cout << val << std::endl;
    }
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Hey , thx for that solution, and it works! That is all I searched for and it's even a simple solution! I must have think of that in the first place XD – Turksat46 May 27 '21 at 11:36