- 3
Write a c++ program that simulates probability experiment of rolling a die several times
Hint: use psedo- random number generators to simulate the probabilities of events
8 Antworten
+ 8
https://code.sololearn.com/cxTFH2AU2v16/?ref=app
Hope you can understand the changes I have made and why they have been made. If not please ask about them.
+ 7
So close, but so far.
1. Your input for times is inside your while loop. Therefore times will always be uninitialized.. There is no need for a while loop here.
2. The die roll random number generation is outside the for loop. It should be inside the for loop before the if statements so a new die roll happens each loop iteration
3. The for loops counter should be i++ or ++I ( i is never being incremented, thus an infinite loop is being created)
4. The percentage value containers should be of type double or float. As they are storing numbers with decimal places.
5. The percentage calculations ( Num5 = num5/times * 100) should include casting to convert the integers to floats or doubles.
Num5 = double(num5) / double(times) * 100.f
I think that's about it. It should work after these changes
+ 6
Also, because this looks like a homework, you gotta show your attempt to solve it before asking others to write a code, and do your homework for you : )
+ 5
HI! It seems you are going to give a nice challenge to the people. But sorry, this is not the place for it.
Please use the lesson factory or your activity feed for this kind of purposes. Thank you!
https://www.sololearn.com/Discuss/1316935/?ref=app
+ 3
#include<iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand ((unsigned int)time(NULL));
int times = 0;
int roll;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
int num6 = 0;
int Num1, Num2, Num3, Num4, Num5, Num6;
roll = rand() % 6 + 1;
while(times >= 1)
{
cout << "How many times would you like to roll the dice?";
cin >> times;
}
for (int i = 1; i <= times; roll++)
{
if (roll == 1)
{
num1++;
}
else if (roll == 2)
{
num2++;
}
else if (roll == 3)
{
num3++;
}
else if (roll == 4)
{
num4++;
}
else if (roll == 5)
{
num5++;
}
else if (roll == 6)
{
num6++;
}
}
Num1 = num1/times *100;
Num2 = num2/times *100;
Num3 = num3/times *100;
Num4 = num4/times *100;
Num5 = num5/times *100;
Num6 = num6/times *100;
cout <<"# Rolled \t # Times \t % Times" << endl;
cout <<"----- \t ------- \t -------" << endl;
cout <<" 1 \t " << num1 << "\t " << fixed << setprecision(2) << Num1 << "%\n";
cout <<" 2 \t " << num2 << "\t " << fixed << setprecision(2) << Num2 << "%\n";
cout <<" 3 \t " << num3 << "\t " << fixed << setprecision(2) << Num3 << "%\n";
cout <<" 4 \t " << num4 << "\t " << fixed << setprecision(2) << Num4 << "%\n";
cout <<" 5 \t " << num5 << "\t " << fixed << setprecision(2) << Num5 << "%\n";
cout <<" 6 \t " << num6 << "\t " << fixed << setprecision(2) << Num6 << "%\n";
system("PAUSE");
return 0;
}
i have tried to write the above program, but it gives me wrong output. please i need help
+ 2
#include<iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand ((unsigned int)time(NULL));
int times = 0;
int roll;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
int num6 = 0;
int Num1, Num2, Num3, Num4, Num5, Num6;
roll = rand() % 6 + 1;
while(times >= 1)
{
cout << "How many times would you like to roll the dice?";
cin >> times;
}
for (int i = 1; i <= times; roll++)
{
if (roll == 1)
{
num1++;
}
else if (roll == 2)
{
num2++;
}
else if (roll == 3)
{
num3++;
}
else if (roll == 4)
{
num4++;
}
else if (roll == 5)
{
num5++;
}
else if (roll == 6)
{
num6++;
}
}
Num1 = num1/times *100;
Num2 = num2/times *100;
Num3 = num3/times *100;
Num4 = num4/times *100;
Num5 = num5/times *100;
Num6 = num6/times *100;
cout <<"# Rolled \t # Times \t % Times" << endl;
cout <<"----- \t ------- \t -------" << endl;
cout <<" 1 \t " << num1 << "\t " << fixed << setprecision(2) << Num1 << "%\n";
cout <<" 2 \t " << num2 << "\t " << fixed << setprecision(2) << Num2 << "%\n";
cout <<" 3 \t " << num3 << "\t " << fixed << setprecision(2) << Num3 << "%\n";
cout <<" 4 \t " << num4 << "\t " << fixed << setprecision(2) << Num4 << "%\n";
cout <<" 5 \t " << num5 << "\t " << fixed << setprecision(2) << Num5 << "%\n";
cout <<" 6 \t " << num6 << "\t " << fixed << setprecision(2) << Num6 << "%\n";
system("PAUSE");
return 0;
}
i have tried to write the above program, but it gives me wrong output. please i need help
+ 2
Barick, please, for future reference, attach instead a saved code link in the "Description" section of your original post above, rather than writing/pasting the code in answers section. People can view your code more comfortably that way. And also please need to work on the code indentation part : )
0
I haven't understood you well, plz can u write that program correctly as you recomend