(C++) There are 8 slots in a plinko game. If I drop a chip into slot 1, output t
ID: 3856130 • Letter: #
Question
(C++) There are 8 slots in a plinko game. If I drop a chip into slot 1, output the path that the chip takes until it hits the bottom.
The correct path should be [1.0 0.5 0.0 0.5 1.0 1.5 1.0 0.5 1.0 0.5 1.0 1.5 1.0] when using srand(42).
Fix the following code to output the CORRECT path instead of the current path its outputting:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
srand(42);
int slot = 1;
double slotDouble = slot;
cout << "Random numbers [";
for (int j = 0; j < 12; ++j)
{
cout << rand() % 2 << " ";
}
cout << "]" << endl << endl;
cout << fixed << setprecision(1) << "OUR Path: [" << slotDouble;
double chipLocation = slotDouble;
for (int i = 0; i < 12; i++)
{
if ((rand() % 2) == 0)
{
chipLocation -= 0.5;
if (chipLocation < 0)
{
chipLocation = 0.5;
cout << " " << chipLocation;
}
else if (chipLocation > 8)
{
chipLocation = 7.5;
cout << " " << chipLocation;
}
else
{
cout << " " << chipLocation;
}
}
else
{
chipLocation +=0.5;
if (chipLocation < 0.0)
{
chipLocation = 0.5;
cout << " " << chipLocation;
}
else if (chipLocation > 8.0)
{
chipLocation = 7.5;
cout << " " << chipLocation;
}
else
{
cout << " " << chipLocation;
}
}
}
cout << "]" << endl;
cout << "CORRECT Path: [1.0 0.5 0.0 0.5 1.0 1.5 1.0 0.5 1.0 0.5 1.0 1.5 1.0]" << endl;
return 0;
}
Explanation / Answer
#include int simulatedrop(int slot, int printslots) { float location = slot; for (int i = 0; i < 12; i++) { int random = rand() % 10; if (random < 5){ location -= 0.5; } else { location += 0.5; } if (location < 0) { location = 0.5; } else if (location > 8) { location = 7.5; } if (printslots == 1) { std::coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.