Make use of a for loop and Make use of a while loop and Make use of a do-while l
ID: 3793542 • Letter: M
Question
Make use of a for loop and Make use of a while loop and Make use of a do-while loop Instructions: Add a for loop to run the Rock, Paper Scissors n times and print how many times you won. how many times the computer won and how many times ended in a tie. Input n from the user. Add a while loop to the Rock. Paper. Scissors lab to keep running until you have won n times. Input n from the user. Print how many times you won, how many times the computer won and how many times ended in a tie. Add a do while loop to the Rock, Paper. Scissors lab to keep running the program until the user says to stop. At the end of the program print how many times you won, how many times the computer won and how many times ended in a tie. Run at least 5 times. Run: Run the first version, second version and third version. Make sure you have not created an endless loop when you run the program.Explanation / Answer
//Using For Loop
#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
int main()
{
int v1, v2; // Values to represent one of the events Rock, Paper, Scissor
int n, cwon, uwon, tie;
cwon = 0;
uwon = 0;
tie = 0;
cout << " Enter the number of times you want to run the loop ";
cin >> n;
for (int i = 0; i < n; i++)
{
v1 = rand() % 3; // Generates random values == 0,1,2
/* 0 = Rock
1 = Paper
2 = Scissor
*/
cout << "Enter!!! 0 = Rock, 1 = Paper, 2 = Scissor ";
cin >> v2;
if ((v1 == 0 && v2 == 0) || (v1 == 1 && v2 == 1) || (v1 == 2 && v2 == 2))
tie++;
if (v1 == 0 && v2 == 1)
uwon++;
if (v1 == 0 && v2 == 2)
cwon++;
if (v1 == 1 && v2 == 0)
cwon++;
if (v1 == 1 && v2 == 2)
uwon++;
if (v1 == 2 && v2 == 0)
uwon++;
if (v1 == 2 && v2 == 1)
cwon++;
//cout << " V1 = " << v1 << " V2 = " << v2 <<" "; // if you want to see values of v1 and v2, remove this comment
}
cout << " Number of times user won = " << uwon;
cout << " Number of times computer won = " << cwon;
cout << " Number of times game tied = " << tie;
getch();
return 0;
}
// Using While Loop
#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
int main()
{
int v1, v2; // Values to represent one of the events Rock, Paper, Scissor
int n, cwon, uwon, tie;
cwon = 0;
uwon = 0;
tie = 0;
cout << " Enter the number of times you want to run the loop ";
cin >> n;
while(n > 0)
{
v1 = rand() % 3; // Generates random values == 0,1,2
/* 0 = Rock
1 = Paper
2 = Scissor
*/
cout << "Enter!!! 0 = Rock, 1 = Paper, 2 = Scissor ";
cin >> v2;
if ((v1 == 0 && v2 == 0) || (v1 == 1 && v2 == 1) || (v1 == 2 && v2 == 2))
tie++;
if (v1 == 0 && v2 == 1)
uwon++;
if (v1 == 0 && v2 == 2)
cwon++;
if (v1 == 1 && v2 == 0)
cwon++;
if (v1 == 1 && v2 == 2)
uwon++;
if (v1 == 2 && v2 == 0)
uwon++;
if (v1 == 2 && v2 == 1)
cwon++;
//cout << " V1 = " << v1 << " V2 = " << v2 <<" "; // if you want to see values of v1 and v2, remove this comment
n--;
}
cout << " Number of times user won = " << uwon;
cout << " Number of times computer won = " << cwon;
cout << " Number of times game tied = " << tie;
getch();
return 0;
}
//Using Do While Loop
#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
int main()
{
int v1, v2; // Values to represent one of the events Rock, Paper, Scissor
int n, cwon, uwon, tie;
n = 1;
cwon = 0;
uwon = 0;
tie = 0;
do
{
v1 = rand() % 3; // Generates random values == 0,1,2
/* 0 = Rock
1 = Paper
2 = Scissor
*/
cout << "Enter!!! 0 = Rock, 1 = Paper, 2 = Scissor ";
cin >> v2;
if ((v1 == 0 && v2 == 0) || (v1 == 1 && v2 == 1) || (v1 == 2 && v2 == 2))
tie++;
if (v1 == 0 && v2 == 1)
uwon++;
if (v1 == 0 && v2 == 2)
cwon++;
if (v1 == 1 && v2 == 0)
cwon++;
if (v1 == 1 && v2 == 2)
uwon++;
if (v1 == 2 && v2 == 0)
uwon++;
if (v1 == 2 && v2 == 1)
cwon++;
//cout << " V1 = " << v1 << " V2 = " << v2 <<" "; // if you want to see values of v1 and v2, remove this comment
cout << "Do you want continue (Press 1 for yes / Press 0 for No) ";
cin >> n;
} while (n != 0);
cout << " Number of times user won = " << uwon;
cout << " Number of times computer won = " << cwon;
cout << " Number of times game tied = " << tie;
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.