Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers.
I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as your input file. Create a function that does all of this:
void Random_Input (ofstream& outfile);
II) Create an output file to store the data from the file above in a neat fashion. Call the output file “Results.txt”. The neatness instructions are as follows: i. Numbers are written one per line in a field width of 10 ii. Each number is written with 6 digits after the decimal point iii. Each number is written with a plus sign iv. Numbers are right-justified. Define a function neat.
III) Your program should define a function to compute the average of the numbers in the file. The functions should display the average value and also store it at the end of the “Results.txt” file. The function is called with the two input-file output-file stream as its arguments. The prototype should look like the following where file_size represents the number of digits in the file, the function returns the values of the average as a double.
double Averages(ifstream& infile, ofstream& outfile, int file_size);
IV) Your program should define a function to compute the average of the numbers in the file. The functionsshould display the average value and also store it at the end of the “Results.txt” file. The function is called with the two input-file output-file stream as its arguments. The prototype should look like the following where file_size represents the number of digits in the file, the function returns the values of the average as a double.
double Averages(ifstream& infile, ofstream& outfile, int file_size);
Note: use C++ for this program. do the main function first and then do the user defined functions. use cmath library.
1). ANSWER :
GIVENTHAT :
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers.
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<time.h>
#include <iomanip>
using namespace std;
int n; //No of random numbers to be generated
void Random_Input(ofstream& outfile)
{
int i;
double num;
cout<<"Enter the value of N : ";
cin>>n;
for(i=0;i<n;i++)
{
num = double(rand()) * 50.0 / RAND_MAX ; //Generating double numbers
outfile << num << endl; //Writing into the input file
}
}
void neat(ofstream& outfile,int file_size)
{
ifstream infile;
double num;
int i;
infile.open ("inputfile.txt");
for(i=0;i<file_size;i++)
{
infile >> num; //Reading from the input file
outfile.setf(ios_base::right); //To right-justify the contents pf the file
outfile<<setw(10)<<setprecision(6)<<"+"<<num<<endl; //Writing into the Results file
}
}
double Averages(ifstream& infile, ofstream& outfile, int file_size)
{
int i;
double sum=0.0,avg,num;
for(i=0;i<file_size;i++)
{
infile >> num;
sum=sum+num;
}
avg=sum/file_size; //Calculating average
outfile.setf(ios_base::right);
outfile<<setw(10)<<setprecision(6)<<"+"<<avg<<endl; //Writing avg into the Results file
return avg;
}
int main()
{
srand(time(0));
ofstream outfile;
ifstream infile;
double num;
int file_size=0;
outfile.open ("inputfile.txt");
Random_Input(outfile); //Calling Random_Input function()
outfile.close();
file_size=n;
outfile.open ("Results.txt");
neat(outfile,file_size); //Calling neat function()
infile.open("inputfile.txt");
num=Averages(infile,outfile,file_size); //Calling Averages function()
cout<<"The average is : "<<num<<endl;
infile.close();
outfile.close();
return 0;
}
The ouput of the code is :
Results.txt :
Get Answers For Free
Most questions answered within 1 hours.