create a file in c++. Download the attached CreateRandomNumbersFile.cpp file, open it in Dev C++, and then compile and run it. The program should generate a file called "numbers_lastname.txt" in the same folder as the program (replace lastname with your last name). Write a program that asks for the name of an input file. Then, read all the numbers in the file, and display the following information to the screen: name of the input file count of numbers in the file sum of all numbers in the file average of all numbers in the file (to 2 decimal places)
create a file in c++.
Download the attached CreateRandomNumbersFile.cpp file, open it in Dev C++, and then compile and run it. The program should generate a file called "numbers_lastname.txt" in the same folder as the program (replace lastname with your last name).
Write a program that asks for the name of an input file. Then, read all the numbers in the file, and display the following information to the screen:
- name of the input file
- count of numbers in the file
- sum of all numbers in the file
- average of all numbers in the file (to 2 decimal places)
- count of numbers in each range (100-199, 200-299, 300-399, etc.)
The program should:
- display a hello message
- ask the user for an input file
- display the name of the input file
- display statistical information as shown above
- display a goodbye message
create random numbers:
/* This program will ask the user for their last name, which will be used for
* naming an output file. The output file will consist of 500-999 random
* integers, all between 100-999.
*
* COSC-1436 Fundamentals of
* Author: Richard Herschede
* Date: 7/23/2019
*/
//LIBRARIES
#include <iostream> //for input/output
#include <string> //for string functions
#include <fstream> //for files
#include <cstdlib> //for rand() and srand()
#include <ctime> //for system time
using namespace std;
//GLOBAL CONSTANTS
const int MAX_COUNT = 999; //maximum count of numbers to generate
const int MIN_COUNT = 500; //minimum count of numbers to generate
const int MAX_NUM = 999; //maximum value of random number
const int MIN_NUM = 100; //minimum value of random number
//MAIN FUNCTION
int main()
{
//hello
cout << "This program will generate 500-999 random numbers, and write" << endl;
cout << "them to a file. The user's last name will determine the name" << endl;
cout << "of the output file. Each random number will be between 100-999." << endl << endl;
//define local variables
ofstream outfile; //output file stream
string username, //user's last name
filename = "numbers_"; //name of output file
int num, //variable to hold random number
count; //variable to hold count of numbers
//get system time and seed random number generator
unsigned seed = time(0);
srand(seed);
//set file name for output file
cout << "Please enter your last name: ";
getline(cin,username);
filename += username + ".txt";
//open the output file (will create new or overwrite existing)
outfile.open(filename);
//get count of random numbers to generate
count = (rand() % (MAX_COUNT - MIN_COUNT + 1)) + MIN_COUNT;
cout << "Generating " << count << " random numbers to file " << filename << endl;
//generate random numbers and write them to output file
for (int i = 0; i < count; i++)
{
num = (rand() % (MAX_NUM - MIN_NUM + 1)) + MIN_NUM;
outfile << num << " ";
} //end for
//close the output file
outfile.close();
//goodbye
cout << "\nProgram complete. The output file, " << filename << ", is located" << endl;
cout << "in the same directory as this program." << endl;
return 0;
} //end main()
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images