Question: /* Program Name: Program 2 File Name: Program 2 Programmer: Emmett Manzell Date Written: 07/15/2018 Date Compiled:07/15/2018 Purpose: //if not required, remove next 3 lines
/* Program Name: Program 2 File Name: Program 2 Programmer: Emmett Manzell Date Written: 07/15/2018 Date Compiled:07/15/2018
Purpose:
//if not required, remove next 3 lines Date Updated: File Name: Purpose:
SDM:
1. Problem: Program Objective: Using program 1 as a template, repeatedly assign the line segments and circle random values (same ranges as program 1) 1000 times.
Count how many of the 1000 randomly assigned lines are completely in the corresponding circle. Calculate the percentage of lines that were in circles.
Have the program recreate and retest the 1000 lines and circles until the percentage from subsequent sets is greater than the percentage from the first set.
Use the same functions as created in Program 1.
The program should display only the percentage message shown below in the Output Layout section.
2. Analysis:
Variables: Identifier Type Default Min Max Description -------------- -------- -------- -------- -------- --------------- NA
Objects: Identifier Class Description --------------- --------------- ------------------------------------- NA
Structures: Identifier Description --------------- ------------------------------------------------------ NA
Classes: Identifier Description --------------- ------------------------------------------------------ NA
Data Structures: Identifier Description --------------- ------------------------------------------------------ NA
Equations: NA
User Defined Function: //if not required, remove next 3 lines and replace with NA precondition: postcondition: type funcIdentifier(paramType ParamIdentifier)
Output Layout: Provide a sample of output here.
3. Design (the algorithm): 1. 2. 3. ... n. END PROGRAM
4. Code Implementation: (See code below this block comment.)
5. Testing (done before coding!):
Bench Test Data: Output Expected: Output Recieved:
*/
//Uncomment line below is using a math defined constant like M_PI //#define _USE_MATH_DEFINES
#include "iostream"
#include "cmath"
#include "ctime"
#include "cstdlib"
using namespace std;
int randomInteger(const int from, const int to);
int lineSegLength(const int p0x, const int p0y, const int p1x, const int p1y);
bool lineInCircle (const int p0x, const int p0y, const int p1x, const int p1y,
const int mpx, const int mpy, const int radius);
int runSingle()
{
const int from = -99;
const int to = 99;
const int p0x = randomInteger(from, to);
const int p0y = randomInteger(from, to);
const int p1x = randomInteger(from, to );
const int p1y = randomInteger(from, to);
const int mpx = randomInteger(from, to);
const int mpy = randomInteger(from, to);
const int radius = randomInteger(1, 200);
/* cout << "Coordinates of a Random Line Segment "
<< " 1st Point's x coordinate: " << p0x << " "
<< " 1st Point's y coordinate: " << p0y << " "
<< " 2nd Point's x coordinate: " << p1x << " "
<< " 2nd Point's y coordinate: " << p1y << " "
<< " "
<< "Coordinates of a Random Circle "
<< " x coordinate: " << mpx << " "
<< " y coordinate: " << mpy << " "
<< "Radius: " << radius << " "
<< " "; */
return lineInCircle(p0x, p0y, p1x, p1y, mpx, mpy, radius);
}
int randomInteger(const int from, const int to)
{
return from + rand() % (to-from+1);
}
int lineSegLength(const int p0x, const int p0y, const int p1x, const int p1y)
{
const int delx = p0x - p1x;
const int dely = p0y - p1y;
return sqrt(pow(delx,2) + pow(dely,2));
}
//The line segment is in the circle if and only if
//both endpoints of the line segment are in the circle.
bool lineInCircle (const int p0x, const int p0y, const int p1x, const int p1y, const int mpx, const int mpy, const int radius)
{
return lineSegLength(p0x, p0y, mpx, mpy) < radius && lineSegLength(p1x, p1y, mpx, mpy) < radius;
}
int run1000() // runs random line-circle intersection 1000 times and returns the percentage of the times they intersect { int count=0; for(int i=0;i<1000;i++) { if(runSingle()) count++; } return count/10; } int pass_number = 1; const int first_percent = run1000(); cout << "Pass " << pass_number << ": " << first_percent << "% of the lines were contained in the circles." << endl;
do { int pass_number = 1; const int first_percent = run1000(); cout << "Pass " << pass_number << ": " << first_percent << "% of the lines were contained in the circles." << endl;
do { const int subsequent_percent = run1000(); cout << "Pass " << pass_number++ << ": " << subsequent_percent << "% of the lines were contained in the circles." << endl; } while (subsequent_percent <= first_percent); const int subsequent_percent = run1000(); cout << "Pass " << pass_number++ << ": " << subsequent_percent << "% of the lines were contained in the circles." << endl; } while (subsequent_percent <= first_percent);
int main() { srand(time(NULL)); int pass_number = 1; const int first_percent = run1000(); cout << "Pass " << pass_number << ": " << first_percent << "% of the lines were contained in the circles." << endl;
do { const int subsequent_percent = run1000(); cout << "Pass " << pass_number++ << ": " << subsequent_percent << "% of the lines were contained in the circles." << endl; } while (subsequent_percent <= first_percent); } return 0; }
______________________________________________________________
I cant seam to get this to work C++
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
