Suppose if there are 10 thieves and 100 gold coins to be distributed and the distribution pattern goes like this:
Thief 1 gets 1 coin.
Thief 2 gets 2 coins.
Thief 3 gets 3 coins.... and so on upto 10.
When all the thief have received the coins then the sum of total coins will be(1+2+3+... = 55 coins)
. Now the coins left are 45. Now, how can I start redistribution from thief one but with the last incremented value instead of starting from 1 coin again? Like 1st thief in 2nd round should get 11 coins instead of 1 and this should continue until all the coins are distributed and coin left is 0. If the last thief has to get 7 coins but the coin available is 3 then he should get 3 coins and the distribution should be over.
I tried this...
$thieves = 10;
$goldCoins = 100;
$thiefArr = range(1, $thieves);
$assgnCoins = 0;
foreach($thiefArr as $key => $value){
for($i=1; $i<=$goldCoins; $i++){
$assgnCoins = $value; // This assigns first round of coins but how to redistribute it again I have no idea.
}
echo "Thief ".$value." will have ".$assgnCoins." gold coins. <br><br>";
}
I'm not really sure if I understood your problem correctly but here's my take, hope this helps:
<?php
$numt = 10; //Total number of thieves
$totalCoins = 100; //Total coins to redistribute
$data = []; //Data array (will contains the index, which is the "thief" number, and the value of a specific index is the total amount of coins for that specific thief
//Loop init: $coins is the amount of coins given to a thief in a specific distribution round
//Loop condition: We loop until there are still coins to redistribute
//Loop statement: We add 1 coin to each redistribution round
// (I start from 0 and use $coins + 1 because I do modulus on $numt ($coins % $numt) which will give me a number from 0 to $numt - 1 (Hint/fact: array indexes start from 0 and not 1)
for ($coins = 0; $totalCoins > 0; $coins++)
{
//This will always give me a number between 0 and $numt - 1 (so that we know which thief's turn is)
//(If this is not clear, you can print out $coins and $thiefIndex then you'll see/understand why I do this)
$thiefIndex = ($coins % $numt);
//Because we did not initialize $data values, we check if this specific thief's coins amount has been initialized
if (!isset($data[$thiefIndex]))
{
$data[$thiefIndex] = 0; //Every thief starts with 0 coins
}
//Because we started from 0, we need to add 1 (this is the amount of coins that this thief ($thiefIndex) will get in this redistribution round
$coinsToGive = ($coins + 1);
//If there's not enough coins left, we just give the total coins remaining
if ($totalCoins < $coinsToGive)
{
$coinsToGive = $totalCoins;
}
//Here we sum up all coins that a thief receives in a specific redistribution round
$data[$thiefIndex] += $coinsToGive;
//We need to subtract the given coins to the total conins that we redistribute (this makes the loop break when it reaches 0 (see the loop condition part)
$totalCoins -= $coinsToGive;
}
//Use data/print in your case
foreach ($data as $idx => $tot)
{
echo "Thief ".($idx + 1)." will have ".$tot." gold coins. <br />";
}
EDIT: I think you edited the question while I was writing my answer. Anyways, if you want to see how much coins every thief is getting on every redistribution round, you can add an echo in the first loop (at the end).
If you're new to programming, you may not know what += or -= means:
$foo += $bar;
Is the same as:
$foo = $foo + $bar;
This can be applied to every arithmetical operator like +, -, *, / (and even other signs like &= and |=, etc... but those will be bit-wise operations)
Information about operators can be found online on the PHP website.
<?php
$thieves = range(1, 10);
$availableCoins = 100;
$assignCoins = 0;
while($availableCoins > 0) {
foreach($thieves as $thief) {
// increase the coins to assign
$assignCoins++;
// check if assign coins are greater than the availableCoins
if ($assignCoins > $availableCoins) {
$assignCoins = $availableCoins;
}
echo "Thief ".$thief." will have ".$assignCoins." gold coins. <br><br>";
// substract assignedCoins from availableCoins
$availableCoins -= $assignCoins;
// break the loop if no coins left
if ($availableCoins <= 0) {
break;
}
}
}
I have now edited the answer of @julian S to give the result that the homework should give:
I also have added some more HTMl input to demonstrate the structure of the array the running operations and the resulting array in readable form.
In short:
The range was wrong here.
Because it will fill an array with 1,2,3,4,5,6,7,8,9, that already is a numerical array so member 1 has a value 1 ?? Useless. NO!
So we crate an array of the 10thives weach with teh initial value = - because teh thieves have = money initially.
We print this array and add pre so that it looks a little better :-)
Then we have two loops - the outer while loop that has the function to look if there is still money left. if there is still moey left then make another round. That was the logic missing to the original poster as he did understand that he needed a loop for the 10 thieves.
Then there is the INNER LOOP
just count from 1 to the number of thieves. So each thief will get its share. $a += $b is the short form of $a=$a+$b
Add the currentcoins to the current arrray member
Add a break to leave the for loop if the monex is gone before you are at the end of the 10 thieves.
At the end just print the array with the wealth of the thieves.
0-9 as arrays start with 0.
<?php
$thieves = 10;
$availableCoins = 100;
$assignCoins = 0;
$array_thieves= array_fill(0,10,0);
echo "<h2>Initial array with wealth of each thief</h2> ";
echo "<pre>";
print_r($array_thieves)."<br>";
echo"</pre>";
while($availableCoins > 0) { //inerate as long as there are coins - outer loop
for($i=0; $i<$thieves; $i++) { //execute loop once per thieve so 10 x per round -inner loop
// increase the coins to assign
$assignCoins++; //we have started with 0 and each time we add 1
// check if assign coins are greater than the availableCoins
//if the number of available coins is smaller then just assign all available coins
if ($assignCoins > $availableCoins) {
$assignCoins = $availableCoins;
}
$array_thieves[$i] += $assignCoins; //add the coins to the thied (= array member) with the current number $i [0-9]
echo "Thief ". $i ." gets now additional ".$assignCoins." gold coins. He has now a total of: ". $array_thieves[$i] ." <br>";
// substract assignedCoins from availableCoins
$availableCoins -= $assignCoins;
// break the FOR loop if no coins left - otherwise the inner loop will continue
if ($availableCoins <= 0) {
break;
}
}
}
echo"<h2>Resulting array with all thieves and their wealth</h2>";
echo"<pre>";
print_r($array_thieves);
echo"</pre>";
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.