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
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
Write a C++ Program Consider the following incomplete C++ program: #include <iostream> using namespace std; int...
Write a C++ Program Consider the following incomplete C++ program: #include <iostream> using namespace std; int main() {    …. } Rewrite the program include the following statements. Include the header file fstream, string, and iomanip in this program. Declare inFile to be an ifstream variable and outFile to be an ofstream variable. The program will read data from the file inData.txt and write output to the file outData.txt. Include statements to open both of these files, associate inFile with...
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...
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...
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...
Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to...
Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to get 4 integers from the user and store them in variables. The program should calculate the average of the 6 numbers as a floating point. Output all of the original input and the calculated average in the Command window. The code is expected to be commented and user-friendly.
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...
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." •...
Create a C program that evaluates the function 3x + 7. Your input will consist of...
Create a C program that evaluates the function 3x + 7. Your input will consist of a number of lines, each containing a single integer number. For each number x that is provided in the input, you should output 3x + 7 as a single integer number. Each number your program prints has to occupy a separate line. No other character should be sent to the output other than the digits of the number (and possible sign) and the newline...