INTRODUCTION Several seasons of hot summers and cold winters
INTRODUCTION: Several seasons of hot summers and cold winters have taken their toll on Farmer John\'s fence, and he decides it is time to repaint it, along with the help of his favorite cow, Bessie. Unfortunately, while Bessie is remarkably proficient at painting, she is not as good at understanding Farmer John\'s instructions.
If we regard the fence as a one-dimensional number line, Farmer John paints the interval between x=a and x=b. For example, if a=3 and b=5, then Farmer John paints an interval of length 2. Bessie, misunderstanding Farmer John\'s instructions, paints the interval from x=c to x=d, which may possibly overlap with none, part, or all of Farmer John\'s interval. Please determine the total length of fence that is now covered with paint.
INPUT FORMAT:
Your program must open and read an input file containing the following two lines of data:
The first line of the input file contains the integers a and b, separated by a space (a
The second line of the input file contains integers c and d, separated by a space (c
The values of a, b, c, and d all lie in the range 0…100, inclusive.
OUTPUT FORMAT:
Please output a single line containing the total length of the fence covered with paint.
SAMPLE INPUT:
7 10
4 8
SAMPLE OUTPUT:
6
Here, 6 total units of fence are covered with paint, from x=4 all the way through x=10.
INCLUDE IN YOUR ASSIGNMENT
At the top of each of your C++ programs, you should have at least four lines of documentation:
// Program name: tictactoe.cpp
// Author: Twilight Sparkle
// Date last updated: 5/26/2016
// Purpose: Play the game of Tic-Tac-Toe
1.The Source code for the lab assignment (*.cpp)
2.And any Header files for the lab assignment (*.h)
Solution
//C++ code
#include <iostream>
#include <random>
#include <string>
#include <iomanip>
#include <ctype.h>
using namespace std;
int main()
{
int a, b, c, d;
cout << \"Enter a, b: \";
cin >> a >> b;
cout << \"Enter c, d: \";
cin >> c >> d;
int totalUnits = 0;
// both segments are apart
if(d < a)
totalUnits = (b-a) + (d-c);
// segment cd is partially inside ab
else if(c < a && d > a && d < b)
totalUnits = (b-a) + (d-c) - (d - a);
// segment cd is inside ab
else if(c > a && d < b)
totalUnits = (b-a);
// segment cd is partially outside ab
else if(c > a && c < b && d > b)
totalUnits = (b-a) + (d-c) - (b-c);
// both segments are apart
else if(c > b)
totalUnits = (b-a) + (d-c);
// ab is inside cd
else if(c < a && d > b )
totalUnits = (d-c);
cout << \"Total Units: \" << totalUnits << endl;
return 0;
}
/*
output:
Enter a, b: 7 10
Enter c, d: 4 8
Total Units: 6
Enter a, b: 5 10
Enter c, d: 4 11
Total Units: 7
*/

