Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please fill out programming project analysis design sheet tto get full points. (

ID: 3545421 • Letter: P

Question

                   please fill out programming project analysis design sheet tto get full points.                                    

                                (LANGUAGE C++)


Programming Project Analysis and Design Sheet


(Repeat this section for each of your functions). Provide the function prototype here.



Experiments that are either too expensive or too dangerous to perform are often simulated on a computer when the computer is able to provide a good representation of the experiment. Find out how to call the random-number generator (usually a function returning a floating point value in the range 0 to 1) for your C++ system. (Look up the functions rand and srand in the library cstdlib on the website cplusplus.com). Write a program that uses the random-number generator to simulate the dropping of glass rods that break into three pieces. The purpose of the experiment is to estimate the probability that the lengths of the three pieces are such that they might form the sides of a triangle.

For the purposes of this experiment, you may assume that the glass rod always breaks into three pieces. If you use the line segment 0 to 1 (on the real number line) as a mathematical model of the glass rod, a random-number generator (function) can be used to generate two numbers between 0 and 1 representing the coordinates of the breaks. The triangle inequality (the sum of the lengths of two sides of a triangle are always greater than the length of the third side) may be used to test the length of each piece against the lengths of the other two pieces.

To estimate the probability that the pieces of the rod form a triangle, you

Explanation / Answer

#include <iostream>

#include <cmath>

#include <cstdlib>

#include <cfloat>

#include <iomanip>

#include <stdlib.h>

#include <time.h>

using namespace std;

float doBreak ();

float doProbability (float, float);

const int SENTINEL = 21; //sentinal value

int main()

{

float count;

float tests;

float probability;

float triangle;

count = 1;

srand (time (NULL));

cout << "Enter number of glassrods to demolish (Enter " << SENTINEL << " to end program): ";

cin >> tests;

if

(tests != SENTINEL)

{

do

{

triangle = doBreak();

count++;

}while (count <= tests);

probability = doProbability(triangle, tests);

cout << "The probability that the broken glass rods will form a triangle is: " << probability << "%" << endl;

}

else

{

cout << "Aww, I was hoping to break stuff..." << endl;

}

return 0;

}

float doBreak()

{

float break1;

float break2;   

float side1;

float side2;

float side3;

float static triangle = 0;

break1 = (float)rand()/RAND_MAX;

break2 = (float)rand()/RAND_MAX;

if (break1 < break2)

{

side1 = break1;

side2 = (break2 - break1);

side3 = 1 - break2;

}

else

{   

side1 = break2;

side2 = (break1 - break2);

side3 = 1 - break1;

}

if

((side1 + side2) > side3 &&

(side1 + side3) > side2 &&

(side2 + side3) > side1)

{

triangle += 1;

}

return triangle;

}

float doProbability

(float triangle,

float tests)

{

float probability;

probability = (triangle / tests) * 100;

return probability;

}