Question

In: Computer Science

Create a program that generates a file of random numbers, and then prints them in neat...

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

Solutions

Expert Solution

#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 :

PLEASE LIKE THE ANSWER IF YOU FIND IT HELPFUL OR YOU CAN COMMENT IF YOU NEED CLARITY / EXPLANATION ON ANY POINT.


Related Solutions

Write a program that generates and prints out in a neat format all possible truth tables...
Write a program that generates and prints out in a neat format all possible truth tables with three propositional variables p, q, and r. Your program should also number generated truth tables and output the numbers so it is clear how many total tables were generated. Your program output may look like the following (Note: only the beginning of the output is shown): Truth table 1: p q r proposition ----------------- F F F F F F T F F...
Create a program that generates random number between 0 and 50 and puts them into a...
Create a program that generates random number between 0 and 50 and puts them into a linked list. When it generates 49, instead of adding it to the list, it prints the list, freeing each node and then exits. Submit your .c file
Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the...
Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the following three methods in the class RandomFile: ​public static void randomBinaryFile(String fileName, int length) ​public static void randomNumberFile(String fileName, intlength) ​public static void randomCharFile(String fileName, int length) The parameter “fileName” specifies the file name, and “length” specifies the length of the sequence in the file. ● randomBinaryFile will generate a file containing a sequence of 0 or 1 of the specified length; ● randomNumberFile...
****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them...
****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them to a text file on separate lines. Then the program should read the numbers from the file, calculate the average of the numbers, and display this to the screen.
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
/ File: temperature.cxx // This program prints a table to convert numbers from one unit to...
/ File: temperature.cxx // This program prints a table to convert numbers from one unit to another. // The program illustrases some implementation techniques. #include // Provides cout #include // Provides setw function for setting output width #include // Provides EXIT_SUCCESS #include // Provides assert function using namespace std; // Allows all standard library items to be used double celsius_to_fahrenheit(double c) // Precondition: c is a Celsius temperature no less than absolute // zero (-273.16). // Postcondition: The return value...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
java program Create a program that creates and prints a random phone number using the following...
java program Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes.  Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint:   Think though the easiest way to construct the phone number. Each digit does do not have to be determined...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers...
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT