Question

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. do the main function first and then do the user defined functions. use cmath library.

Homework Answers

Answer #1

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 :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a program that filters the data in a CSV file of product data based on...
Create a program that filters the data in a CSV file of product data based on some search word and prints the resulting output to a new file. Additionally, the program will print the number of items filtered to stdout. • Your program should take three arguments: an input file to process, an output file to save the results, and a search word. • If the output file already exists or cannot be opened, warn the user by printing "CANNOT...
C++ create a program that: in main: -opens the file provided for input (this file is...
C++ create a program that: in main: -opens the file provided for input (this file is titled 'Lab_HW10_Input.txt' and simply has 1-10, with each number on a new line for 10 lines total) -calls a function to determine how many lines are in the file -creates an array of the proper size -calls a function to read the file and populate the array -calls a function to write out the contents of the array in reverse order *output file should...
Create a program that copies the data from one file to another while converting all lowercase...
Create a program that copies the data from one file to another while converting all lowercase vowels to uppercase vowels. Your program should accept two arguments: an input file to read from and an output file to copy to. • Your program should check to make sure the output file does not already exist. If it does, print "DESTINATION FILE EXISTS." to stdout. • Print the number of characters changed to stdout using the format string "%d characters changed." •...
Using C++, create a program to input a list of positive numbers, find the mean (average)...
Using C++, create a program to input a list of positive numbers, find the mean (average) of the numbers, and output the result. Use a subprogram to input the numbers, a function to find the mean, and a subprogram to output the result.
// This program prints a table to convert numbers from one unit to another. // The...
// This program prints a table to convert numbers from one unit to another. // The program illustrates some implementation techniques. //Include the header file ostream for including all stream. ---------------------------------------------------------*/ #include <iostream> //Provides cout <v1.0> //Include iomanip that is used for set precision. #include <iomanip> //Provides setw function for setting output width <v1.1> //Header file for EXIT_SUCCESS. #include <stdlib.h>// Provides EXIT_SUCCESS <v1.2> //Header file for assert. #include <assert.h>// Provides assert function <1.3> using namespace std; // Allows all standard...
we will be taking data in as a file. you cannot use the data values in...
we will be taking data in as a file. you cannot use the data values in your source file but you must read in everything into variables and use it from there. First we need to include <fstream> at the top of our file. We will need to create an input file stream to work and ifstream. ifstream is a datatype. Create a variable with the datatype being ifstream. Variable is drfine by using the member accessor operator to call...
In C++ Write a simple program to generate random integers between X and Y and store...
In C++ Write a simple program to generate random integers between X and Y and store them in a file, one number per line. The user should input the number of elements to generate and the boundary numbers X and Y (e.g. the inputs 100, 0, 999 mean your program should generate a list of 100 integers with values between 0 and 999, inclusive).
Create a C++ program that generates a random number from 1 to 100 using the rand()...
Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
C++ Programming   You are to develop a program to read Baseball player statistics from an input...
C++ Programming   You are to develop a program to read Baseball player statistics from an input file. Each player should bestored in a Player object. Therefore, you need to define the Player class. Each player will have a firstname, last name, a position (strings) and a batting average (floating point number). Your class needs to provide all the methods needed to read, write, initialize the object. Your data needs to be stored in an array of player objects. The maximum...