Related questions
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 nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images
- Can this be done in Java and not C++arrow_forwardCreate a program that reads in a word from the user and counts the number of occurrences of that word in a file called words.txt. -- words.txt This is a file that contains many words. Yes it does have so many words. Many, many words. Well, maybe it is not that many after all. So, just how many is MANY? in c++ pleasearrow_forwardJAVA PPROGRAM Write a program that prompts the user to enter a file name, then opens the file in text mode and reads names. The file contains one name on each line. The program then compares each name with the name that is at the end of the file in a symmetrical position. For example if the file contains 10 names, the name #1 is compared with name #10, name #2 is compared with name #9, and so on. If you find matches you should print the name and the line numbers where the match was found. While entering the file name, the program should allow the user to type quit to exit the program. If the file with a given name does not exist, then display a message and allow the user to re-enter the file name. The file may contain up to 100 names. You can use an array or ArrayList object of your choosing, however you can only have one array or ArrayList. Input validation: a) If the file does not exist, then you should display a message "File 'somefile.txt' is not found." and allow the...arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education