
I have a question for the following program:
#include <iostream>
#include <stdlib.h>
using namespace std;
int rolldice(){
int r1, r2, s = 0;
r1 = rand() % 6 + 1;
cout << "After rolling: " << r1 << endl;
r2 = rand() % 6 + 1;
cout << "After rolling: " << r2 << endl;
s = r1 + r2;
if(r1 == r2){
r1 = rand() % 6 + 1;
cout << "After rolling: " << r1 << endl;
r2 = rand() % 6 + 1;
cout << "After rolling: " << r2 << endl;
s += r1 + r2;
if(r1 == r2)
s = 1;
}
return s;
}
int findfirst(int pos[]){
int i, j = 0, m = pos[0];
for(i = 1; i < 4; i++){
if(m < pos[i]){
m = pos[i];
j = i;
}
}
return j;
}
int findlast(int pos[]){
int i, j = 0, m = pos[0];
for(i = 1; i < 4; i++){
if(m > pos[i]){
m = pos[i];
j = i;
}
}
return j;
}
void calposition(int pos[], int sum[]){
int i, p, temp, j;
for(i = 0; i < 4; i++){
if(sum[i] == 1 || sum[i] == 12)
pos[i] = 0;
else if(sum[i] == 4 && pos[i] > 1)
pos[i] -= 1;
else if(sum[i] == 7){
p = findfirst(pos);
if(pos[p] != pos[i]){
temp = pos[i];
pos[i] = pos[p];
pos[p] = temp;
}
}
else if(sum[i] == 11){
p = findlast(pos);
if(pos[p] != pos[i]){
temp = pos[i];
pos[i] = pos[p];
pos[p] = temp;
}
}
else{
if((pos[i] + sum[i]) <= 50)
pos[i] += sum[i];
}
for(j = 0; j < 4; j++){
if(i != j){
if(pos[i] == pos[j])
pos[j] = 0;
}
}
}
}
void display(int pos[]){
int i,j, k = 1, m;
for(i = 1; i <= 51; i++)
cout << "_";
cout << endl;
for(i = 1; i <= 5; i++){
for(j = 1; j <= 10; j++){
if(k == pos[0])
cout << "|_P" << 1 << "_";
else if(k == pos[1])
cout << "|_P" << 2 << "_";
else if(k == pos[2])
cout << "|_P" << 3 << "_";
else if(k == pos[3])
cout << "|_P" << 4 << "_";
else
cout << "|____";
k++;
}
cout << "|" << endl;
}
}
int findwinner(int pos[]){
int i;
for(i = 0; i < 4; i++){
if(pos[i] == 50){
break;
}
}
if(i == 4)
return 0;
else
return (i + 1);
}
int main() {
int pos[4];
int sum[4];
int i, temp, winner;
char choice = 'y';
cout << "Lets play, sorry!" << endl;
while(choice == 'y' || choice == 'Y'){
pos[0] = pos[1] = pos[2] = pos[3] = 0;
while(true){
for(i = 0; i < 4; i++){
cout << "Player " << i + 1 << " is rollong." << endl;
sum[i] = rolldice();
}
calposition(pos, sum);
display(pos);
winner = findwinner(pos);
if(winner > 0){
cout << "Player " << winner << " is winner!!" << endl;
break;
}
}
cout << "Wanna play again? (Y/N)" << endl;
cin >> choice;
}
return 0;
}
How did it display the player's position after every roll?

Step by stepSolved in 3 steps

- using c++ complete the code where it says , /* Your code goes here */ compute z= y- √2 #include <iostream>#include <cmath>#include <ios>#include <iomanip>using namespace std; int main() {double x;double y;double z; cin >> x;cin >> y; /* Your code goes here */ cout << fixed << setprecision(2); // This will output only 2 decimal places.cout << z << endl; return 0;}arrow_forwardIdentify the maximum number of errors in the code and rewrite the code. #include using namespace std; void main() { int a, b, c; cout >> "enter three sides of a triangle" >> endl; cin > "Area of Triangle ">> s(s - a)(s - b)(s - c)>endl; system("pause");arrow_forwardWhat is the output of the following code: #include #include using namespace std; int main(){ double x = 142.366; cout <« setw(5) <« setiosflags(ios::fixed) << setprecision(2) << x « endl; cin.ignore(); return 0; }arrow_forward
- Explain step by steparrow_forward2 Mark C++ code block int A -1: int B-2; if(((A--1) 11 (B--2)) 66 (B--0)) cout<<"This exam was difficult "; cout<< "This exam was easy "; int count; for (count = 11; count<-15; count++) cout << "Hello" << endl; for (int count 1; count <- 5 / count4-2 1 ( if (count -- 3) break; cout << count ; j Outputarrow_forward#include <iostream>#include <cmath>#include <iomanip>using namespace std; int main() { double x; double y; double z; cin >> x; cin >> y; /* Your code goes here */ cout << fixed << setprecision(1); // setprecision(1) outputs z with 1 decimal place. cout << z << endl; return 0;} having trouble with this code in C++arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





