In: Computer Science
Write a C++ program that creates a file called
Readings.txt. Inside the file, your program must
create
a list. The list is composed of integer double pairs. There is one
pair per line. The integers are in
sequence (0, 1, 2, 3, ...) beginning with zero and ending with some
random value between 512 and
1024. The doubles should be random values between 50.000 and
90.000. The doubles only have 3
decimal places. The file should look like this (of course your
doubles will be random, and there will
be more than 5 readings):
0, 56.347
1, 78.231
2, 89.999
3, 68.002
4, 55.128
#include <iostream>
using std::cerr;
using std::endl;
#include <fstream>
using std::ofstream;
#include <cstdlib>
#include<bits/stdc++.h>
int main()
{
ofstream op;
int i;
op.open("Readings.txt");
if( !op ) {
cerr << "file not found" << endl;
exit(1);
}
int nums=512+ ( std::rand() % ( 1025 - 512 + 1
));
for (i=0; i<nums; ++i){
float sub=90.0 + (rand() /( RAND_MAX / (50.0-90.0) )
);
op
<<i<<"\t"<<floor(sub*1000)/1000<<endl;
}
op.close();
return 0;
}
#output: