Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++) There are 8 slots in a plinko game. If I drop a chip into slot 1, output t

ID: 3856129 • 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

In your above code you are generating random number two times, hence the output is different. Use the single random number through the program, then it will produce the correct output.

I have modified the code.

#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++)
    {
      int val=rand()%2;
       if (val == 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;
}