Hey, so I'm completely lost on this question, or even what is being asked of me. The book gives little or posibly no ensite as to how to begin this assignment. Can anyone help? Thank you
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(usally 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 te library cstdlib.) write a program that uses the random-number generaor to simulate the dropping of glass tods that break into three pieces. The purpose of the experiment is to estimate the probability that the lenths of the three pieces are such that they might form the sides of a triangle.
For the purpose of this experiment, you may assume that the glass rod always breaks into 3 pieces. if you use the line segment 0 to 1(on the real numberl ine) as a mathmatical 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 lentths of two sides of the traingle are always greater than the lenght 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 from a triangle, you'll need to repeat the experiment many times and count the number of times a triangle can be formed from the pieces. the probability estimate is the number ofsuccesses divided by the total number of rods dropped. Your program should prompt the user for the number of rods to drop and allow the experiment to be repeated. Use a sentinel value of 21 to halr execution of the program.
Alright, so you need to use the commands rand() and srand() to generate two random numbers, which represent where the glass rod broke. You can then use these two points to generate 3 different lengths representing the 3 pieces of glass from when the rod broke.
Once you have the 3 lengths, you need to test if they are a triangle. Say you have 3 lengths: A, B, and C. A triangle can be formed if all the following conditions are met:
A+B > C
A+C > B
B+C > A
Now to implement this into C++. It is easiest to start simple, just write code that tests 1 breaking of the glass rod and sees if those 3 pieces can be made into a triangle. Here is how I would go about it.
#include <iostream>
#include <math.h>
usingnamespace std;
int main(){
//Define the variables. You need 2 floats to represent the breaking points
//3 floats to represent the lengths of the pieces of the rod, etc.
//Here you need to generate two random numbers between 0 and 1. This can be done using the rand() and srand() commands.
//Here you need to calculate the lengths of the pieces of the rod from the random numbers.
//Say the breaking points are A and B, and the rod lengths are X, Y, and Z.
//if A < B: X = A, Y = B - A, Z = 1 - B
//Here, you need to test if X, Y, and Z make a triangle.
//Do this by testing X + Y > Z, X + Z > Y, and Y + Z > X
//Finally, print out if it makes a triangle or doesn't.
}
Once you get your code working for a single experiment, just throw it into a for loop and run it like 10,000 times and calculate the probability.
if ((side1 ++ side2 > side3) && (side1 ++ side3 > side2) && (side2 ++ side3 > side1))
{
cout << "The pieces are a triangle." << endl;
}
else
{
cout << "Doesn't form a triangle." << endl;
}
}
return 0;
}
I'm getting
./glassrod.cpp: In function `int main()':
./glassrod.cpp:47: error: expected `)' before "side2"
./glassrod.cpp:51: error: expected `)' before "else"
./glassrod.cpp:57: error: expected primary-expression before '}' token
./glassrod.cpp:57: error: expected `;' before '}' token
./glassrod.cpp:65:2: warning: no newline at end of file
for compiling errors. I'm very confused on this project. We do have a book but it doesn't even have srand or rand in the book. Anymore help would be much appreciated!!