Write a C++ program that is going to use a random number generator to do some an
ID: 3738228 • Letter: W
Question
Write a C++ program that is going to use a random number generator to do some analysis on a product. In the main function, prompt for a seed value to select a random sequence of values. The main should create a counter control file with 25 records where each record has three values. The first value is the integer sample number which can be labeled from 1 to 25. The second and third values represent floating point values for sugar and oil and both will be randomly generated. Sugar will be betweern 2mm and .75mm. The oil will be between 25 to 100 percent. Use the Rand_Float function to randomly generate the sugar and oil values for the main functionExplanation / Answer
#include<iostream>
#include<ctime>
#include<cstdlib>
#include <stdio.h>
#include <fstream>
#include<sstream>
using namespace std;
int main()
{
unsigned n;
cout<<"Enter seed: ";
cin>>n;
srand(n);
float sugar,oil;
std::ofstream outFile;
outFile.open ("output.txt");
for(int i=1; i<=25; i++)
{
sugar = 0.2 + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(0.75-0.2)));
oil = 25 + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(100-25)));
outFile << i << " " << sugar << "mm " << oil << "% " ;
}
outFile.close();
cout << "25 records written to output.txt ";
return 0;
}
Please Rate it if you find the answer is helpful...Thanks...:)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.