Name: Section:CC-4_ Two cats named A and B and two mice are standing at some poi
ID: 3871035 • Letter: N
Question
Name: Section:CC-4_ Two cats named A and B and two mice are standing at some points on a line. Cat A is standing at point x and cat B is standing at point y. The two mice are hiding at a point z. Both cats rurn at the same speed, and they want 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 number of mice each cat catches 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. Output Format Sample Input Sample Output Cat A: 2 Cat B: 2 123 132 213 Instructions Write and run a C++ program to solve the problem using the input data below. Provide on the back of this sheet: (1) the C++ program, and (2) the output. Demonstration of the program developed to your instructor before leaving is required. Input datoa 22 75 70 33 86 59 47 29 89 18 19 82 84 1718 84 68 76Explanation / Answer
#include<iostream>
using namespace std;
// this function is to get the absolute difference between 2 points
int absdiff(int x, int y)
{
if(x>y)
return x-y;
return y-x;
}
main()
{
// declaring variables
int n,i,x,y,z,xz,yz, a=0, b=0;
// taking user input of n
cin >> n;
// running loop for n times
for(i=0;i<n;i++)
{
// taking user input of x, y and z
cin >> x;
cin >> y;
cin >> z;
// finding difference between x and z
xz = absdiff(x,z);
// finding difference between y and z
yz = absdiff(y,z);
// both go at a time to mice, then incrementing a and b as well
if(xz==yz)
{
a++;
b++;
}
// if cat A goes faster, incrementing a by 1
else if(xz<yz)
{
a++;
}
// incrementing b by 1
else
{
b++;
}
}
// printing output
cout << endl << "Cat A: " << a << endl;
cout << "Cat B: " << b << endl;
}
Sample Output 1
Sample Output 2
#include<iostream>
using namespace std;
// this function is to get the absolute difference between 2 points
int absdiff(int x, int y)
{
if(x>y)
return x-y;
return y-x;
}
main()
{
// declaring variables
int n,i,x,y,z,xz,yz, a=0, b=0;
// taking user input of n
cin >> n;
// running loop for n times
for(i=0;i<n;i++)
{
// taking user input of x, y and z
cin >> x;
cin >> y;
cin >> z;
// finding difference between x and z
xz = absdiff(x,z);
// finding difference between y and z
yz = absdiff(y,z);
// both go at a time to mice, then incrementing a and b as well
if(xz==yz)
{
a++;
b++;
}
// if cat A goes faster, incrementing a by 1
else if(xz<yz)
{
a++;
}
// incrementing b by 1
else
{
b++;
}
}
// printing output
cout << endl << "Cat A: " << a << endl;
cout << "Cat B: " << b << endl;
}
Sample Output 1
3 1 2 3 1 3 2 2 1 3 Cat A: 2 Cat B: 2
Sample Output 2
6 22 75 70 33 86 59 47 29 89 18 19 82 84 17 18 84 68 76 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.