Use the input data provided! Use c++ as program language! Use c++ compilier! Two
ID: 3903502 • Letter: U
Question
Use the input data provided! Use c++ as program language! Use c++ compilier! Two cats named A and B and two mice are standing at some points on a line. Cat A is standing at point r and cat B is standing at point y. The two mice are hiding at a point z. Both cats run at the same speed, and each wants to catch one of the two mice. If one of the cats arrives first at point z, it catches one of the mice and the other one escapes; the other cat catches none. If both cats arrive at point z at the same time, each cat catches one mouse You are given a number of test cases in the form of x, y, and z. Find the total number of mice each cat catches over all the test cases. Input Format The first line contains a single integer T denoting the number of test cases. . Each of the subsequent T lines contains three space-separated integers describing x (cat A's location), y (cat B's location), and z (location of the mice) on the line. Sample Input 3 1 23 132 213 Sample Output Cat A: 2 Cat B: 2 Instructions includes: () Statement of the problem, (2) solution method, (3) the C++ program developed, (4) input data used, and (5) output. Input data 6 22 75 70 33 86 59 47 29 89 18 19 82 84 1718 84 68 76Explanation / Answer
I have answered a previous version of this question. So I’m making a few changes in it to satisfy the new requirements. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//CODE
#include <iostream>
#include <math.h> //for abs() method
using namespace std;
int main()
{
int numTestCases;
//getting number of test cases (input line 1)
cin>>numTestCases;
//declaring x, y, z arrays for storing all test cases
int x[numTestCases],y[numTestCases],z[numTestCases];
//reading all test cases
for(int i=0;i<numTestCases;i++){
cin>>x[i];
cin>>y[i];
cin>>z[i];
}
int catA=0,catB=0; //the number of mice eaten by each cats
//looping through each test case
for(int i=0;i<numTestCases;i++){
//finding the difference between Cat A and mice
int catAdistance=abs(x[i]-z[i]);
//finding the difference between Cat B and mice
int catBdistance=abs(y[i]-z[i]);
if(catAdistance < catBdistance){
//cat A is more closer to the mice, so catA gets one
catA++;
}else if(catAdistance > catBdistance){
//cat B is more closer to the mice,so catB gets one
catB++;
}else{
//equal distance, so each one gets one mice
catA++;
catB++;
}
}
//printing stats
cout<<"Cat A: "<<catA<<endl;
cout<<"Cat B: "<<catB<<endl;
}
//Test cases 1
3
1 2 3
1 3 2
2 1 3
//OUTPUT 1
Cat A: 2
Cat B: 2
//Test cases 2
6
25 75 70
33 86 59
47 29 89
18 19 82
84 17 18
84 68 76
//OUTPUT 2
Cat A: 3
Cat B: 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.