C programing For this problem we have two pieces P1 and P2
[ C programing ]
For this problem we have two pieces, P1 and P2, randomly placed on a R X C board. R is the number
of rows and C is the number of columns. Each round P1 and P2 attempt to move closer together by
moving a random amount towards the other piece\'s current position. Your job is to figure out the
average number of rounds it takes for P1 and P2 to arrive at the same spot.
Input Constraints
• Seed: an integer
• Number of rows: an integer greater than 0
• Number of columns: an integer greater than 0
• Number of rounds: an integer greater than 0
Randomness
• In order to match the tester output you must make calls to rand in the exact order that I do
• For each simulation
? Generate the starting row number for P1
? Generate the starting column number for P1
? Generate the starting row number for P2
? Generate the starting column number for P2
• For each round in a simulation
? If P1 and P2 are on different rows generate a random new row position for P1
? This value should be between [P1\' s row ,P2 \' s row]
? If P1 and P2 are on different columns generate a random new column position for P1
? This value should be between [P1\' s column ,P2 \' s column]
? If P1 and P2 are on different rows generate a random new row position for P2
? This value should be between [P2 \' s row , P1\' s row]
? If P1 and P2 are on different columns generate a random new column position for P2
? This value should be between [P2 \' s column , P1\' s column]
Additional Requirements
• In order to help with debugging if the number of simulations to run is less than or equal to 5
your program should print out the following information
? The starting positions of P1 and P2
? Where P1 and P2 move to each round.
Solution
Solution:
// include required heade files
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include <stdlib.h>
// method ot generate random value
int randval(int v1,int v2)
{
int n = rand() % v2 + v1;
return n;
}
// main method to process
int main()
{
// declare the required variables
int p1,p2,r=0,c=0,seed=0,round=0,p1r,p1c,p2r,p2c,i,lp,mv=0;
//int **board;
// get input for seed and validate it
do
{
printf(\"Enter the seed for the random number generatior:\");
scanf(\"%d\",&seed);
}while(seed<0);
// get input for row and validate it
do
{
printf(\"Enter the number of rows on the board:\");
scanf(\"%d\",&r);
}while(r<0);
// get input for column and validate it
do
{
printf(\"Enter the number of columns on the board:\");
scanf(\"%d\",&c);
}while(c<0);
// get input for round and validate it
do
{
printf(\"Enter the number of simulations to run:\");
scanf(\"%d\",&round);
}while(round<0);
/**board= (int *) malloc(r* sizeof(int));
for(i = 0; i < r; i++)
{
board[i]=(int *) malloc(c* sizeof(int));
}
for(i = 0; i < r; i++)
for(int j=0;j<c;j++)
board[i][j]=0;*/
// generate p1,p2 values
p1=randval(0,seed);
p2=randval(0,seed);
// generate row column valueof p1
p1r=randval(0,r);
p1c=randval(0,c);
// generate row column valueof p2
p2r=randval(0,r);
p2c=randval(0,c);
//board[p1r][p1c]=p1;
//board[p2r][p2c]=p2;
// Code for simulation
for(lp=0;lp<round;lp++)
{
printf(\"Simulation %d\",lp);
printf(\"First piece moves from %d,%d to \",p1r,p1c);
// check p1 row and p2 row same or not
if(p1r!=p2r)
{
//board[p1r][p1c]=0;
p1r=randval(p1r,p2r);
//board[p1r][p1c]=p1;
mv++;
}
// check p1 column and p2 column same or not
if(p1c!=p2c)
{
//board[p1r][p1c]=0;
p1c=randval(p1c,p2c);
// board[p1r][p1c]=p1;
mv++;
}
printf(\"%d,%d to \",p1r,p1c);
printf(\"second piece moves from %d,%d to \",p2r,p2c);
// check p2 row and p1 row same or not
if(p1r!=p2r)
{
//board[p2r][p2c]=0;
p2r=randval(p2r,p1r);
// board[p2r][p2c]=p2;
mv++;
}
// check p2 column and p1 column same or not
if(p1c!=p2c)
{
//board[p2r][p2c]=0;
p2c=randval(p2c,p1c);
//board[p2r][p2c]=p1;
mv++;
}
printf(\"%d,%d to \",p2r,p2c);
}
printf(\"On average it takes %f rounds on a board %d x %d for the pieces to meet.\", round/mv,r,c);
//system(\"pause\");
return 0;
}
![[ C programing ] For this problem we have two pieces, P1 and P2, randomly placed on a R X C board. R is the number of rows and C is the number of columns. Each [ C programing ] For this problem we have two pieces, P1 and P2, randomly placed on a R X C board. R is the number of rows and C is the number of columns. Each](/WebImages/20/c-programing-for-this-problem-we-have-two-pieces-p1-and-p2-1043381-1761542376-0.webp)
![[ C programing ] For this problem we have two pieces, P1 and P2, randomly placed on a R X C board. R is the number of rows and C is the number of columns. Each [ C programing ] For this problem we have two pieces, P1 and P2, randomly placed on a R X C board. R is the number of rows and C is the number of columns. Each](/WebImages/20/c-programing-for-this-problem-we-have-two-pieces-p1-and-p2-1043381-1761542376-1.webp)
![[ C programing ] For this problem we have two pieces, P1 and P2, randomly placed on a R X C board. R is the number of rows and C is the number of columns. Each [ C programing ] For this problem we have two pieces, P1 and P2, randomly placed on a R X C board. R is the number of rows and C is the number of columns. Each](/WebImages/20/c-programing-for-this-problem-we-have-two-pieces-p1-and-p2-1043381-1761542376-2.webp)