Write a program in c++ that generates a 100 random numbers between 1 and 1000 and writes them to a data file called "randNum.dat". Then re-open the file, read the 100 numbers, print them to the screen, and find and print the minimum and maximum values.
Specifications:
If your output or input file fails to open correctly, print an error message that either states:
Output file failed to open Input file failed to open
depending on whether the output or input file failed to open.
Prompt the user for a seed value so that your program can interface with the grader program.
When printing the numbers to the data file, print a return character after each number in the file.
When reading in the numbers from the file and printing them to the screen, use the following output:
Num XXX: YYYY
where XXX indicates which number is being printed out, and YYYY represents the actual random value. For instance, if you input a seed value of 42, then for the 91st number the following should be printed to the screen:
Num 91: 923
Use the setw() function with a width of 3 to indicate which number is being printed out, and use a width of 4 to print the actual value so that everything is right justified. Use a width of 4 as well when printing the minimum and maximum values.
As an example, if you execute the program with the following underlined inputs, the output will be:
Enter Seed: 42 Num 1: 167 Num 2: 741 Num 3: 882 ... Num 67: 451 Num 68: 361 ... Num 99: 652 Num 100: 37 Min: 13 Max: 996 ~>
// C++ program to perform operations on file
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
// filename
const string filename = "randNum.dat";
int seed, num, max, min, count;
// create and open the file
ofstream fout(filename);
// file open failed, display error and exit the program
if(fout.fail()){
cout<<"Output file failed to open"<<endl;
return 0;
}
// input the seed
cout<<"Enter Seed: ";
cin>>seed;
// set the seed value for random number generator
srand(seed);
// loop to generate 100 random integers between [1,1000] and
output to file
for(count=1;count<=100;count++)
{
num = rand()%1000 + 1;
fout<<num;
if(count < 100) // not the last integer, display a new
line
fout<<endl;
}
fout.close(); // close the output file
// re-open the file in read mode
ifstream fin(filename);
// file open failed, display error and exit the program
if(fin.fail())
{
cout<<"Input file failed to open"<<endl;
return 0;
}
count = 0; // reset count to 0
// loop till the end of file
while(!fin.eof())
{
// read an integer
count++;
fin >> num;
// display the integer read
cout<<"Num"<<right<<setw(3)<<count<<":"<<right<<setw(4)<<num<<endl;
// this is the first integer read, set max and min to num
if(count == 1)
{
max = num;
min = num;
}
else // not the first integer read
{
// num > max, update max to num
if(num > max)
max = num;
else if(num < min) // num < min, update min to num
min = num;
}
}
fin.close(); // close the input file
// display the minimum and maximum number
cout<<endl<<"Min:
"<<right<<setw(4)<<min<<endl;
cout<<"Max:
"<<right<<setw(4)<<max<<endl;
return 0;
}
//end of program
Output:
Output file : randNum.dat
Write a program in c++ that generates a 100 random numbers between 1 and 1000 and...
C++ language Write a program that 1. generates a random integer between 100 and 1000, call the number N 2. reads a file name from the keyboard 3. opens a file for output using the file name from step 2 4. generates N random numbers between 1 and 100 and writes them to the file from step 3 5. closes the file 6. opens the file from steps 3, and 4 for input 7. reads the numbers from the file...
Write a program that inputs up to 100 real numbers from the keyboard (one at a time). When a number greater than 1.0e9 is input the keyboard input will stop. Then the following statistic for the input data will be computed and printed out to the screen. a) average b) standard deviation c) minimum value d) maximum value e) range = (maximum-minimum)/2 f) the number of times zero is input **Note: this is a program in C++** 3) Write a...
Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...
DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them in three arrays, then sorts the arrays with Insertion Sort, Merge Sort, and Quick Sort, respectively. Augment the three sorting algorithms with counters and report the number of characteristic operations each performs in sorting the (same) random values. INPUT The program reads from the terminal the number of random integers to generate, a seed value for the pseudo-random number generator, and a character that...
Write a program in C to generate random numbers. The program recieves 4 items on activation through argv: 1. Name of the data file 2. Number of items for program to generate 3. Range of numbers 4. Seed of the random number generator Example run: ./rand datafile.txt 20 1000 3127 Program must convert all numbers to ints using atoi(), must save all parameters into variables, opens the data file to write the random numbers into it, program loops to generate...
programming language Write a program that will generate 5 random numbers between 20 and 80 and assign each to a variable of datatype double. You should use a random number generator seeded with time Using a series of if statements, determine which of the 5 variables contains the smallest value generated Print all five random numbers to the screen and print out the value that you determined was the smallest. All values should be printed with zero decimal places. 2.
Create a JAVA application which 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.
C++ Functions & Streams Write a program that will demonstrate some of the C++ Library Functions, Stream Manipulators, and Selection Control Structures. The user will be given a menu of four choices. They can input a 1 for finding Cosines, 2 for finding Logarithms, 3 for converting between Decimal and Hexadecimal, or 4 to change the format of a cstring date. You must use the proper functions and/or stream manipulators to find the answers. If the user picks the cosine,...
C++ programming please Write a program that will display random numbers in order from low to high. 1. Declare an integer array of size 100. Ask the user how many numbers to work with (n). It can be less than 100. 2. Generate random numbers from 10 to 50. 3. Write the random numbers to an output file named random.dat. Close the file. 4. Open random.dat for input and read the data values into your array. Pass your array to...
For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....