Question

C++ language Write a program that 1. generates a random integer between 100 and 1000, call...

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 and finds the largest number in the file.

8. opens the file from step 3 again and counts the number of times the largest number appears in the file

9. displays the largest number and the number of times it appears with appropriate labels.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <fstream>

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main() {

int seedVal=0;

//t is a 'time_t' type variable

time_t t;

seedVal = (unsigned) time (&t);

srand(seedVal);

  

//defines an input stream for the data file

ifstream dataIn;

  

//Defines an output stream for the data file

ofstream dataOut;

string filename;

//Declaring variable

int max=-999;

int random,count=0;

  

//Generate the random number

int N=rand()%(901) + 100;

  

//Getting the filename entered by the user

cout<<"Enter a filename:";

cin>>filename;

  

//Opening the input file

dataOut.open(filename.c_str());

  

//Generate N random number

for(int i=1;i<=N;i++)

{

random=rand()%(100) + 1;

//Writing to the output file

dataOut<<random<<endl;

}

  

//closing the output file

dataOut.close();

  

//creating and Opening the output file

dataIn.open(filename.c_str());

for(int i=1;i<=N;i++)

{

dataIn>>random;

if(max<random)

{

max=random;

}

}

  

//Closing the intput file

dataIn.close();

  

cout<<"The Largest number in the file is :"<<max<<endl;

//creating and Opening the output file

dataIn.open(filename.c_str());

  

for(int i=1;i<=N;i++)

{

dataIn>>random;

if(random==max)

{

count++;

}

}

dataIn.close();

//Displaying the output

cout<<"No of times largest number appears in the file is :"<<count<<endl;

  

return 0;

}

______________________

Output:

_____________Could you rate me well.Plz .Thank You

Add a comment
Know the answer?
Add Answer to:
C++ language Write a program that 1. generates a random integer between 100 and 1000, call...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in c++ that generates a 100 random numbers between 1 and 1000 and...

    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...

  • C language Write a program that generates 20 random integers (ranging in between 0 to 100)...

    C language Write a program that generates 20 random integers (ranging in between 0 to 100) and outputs all the even random numbers.

  • Write a program that reads an unknown number (but no more than 100) of integer values...

    Write a program that reads an unknown number (but no more than 100) of integer values from a file (“ass5_Q4_input.txt”), and displays distinct numbers. (i.e. if a number appears multiple times, it is displayed only once.)

  • please help me with, Write a program in java that generates 10 random doubles, all between...

    please help me with, Write a program in java that generates 10 random doubles, all between 1 and 11, and writes them to a text file, one number per line. Then write another program that reads the text file and displays all the doubles and their sum accurate to two decimal places. SAMPLE OUTPUT 10.6269119604172 2.737790338909455 5.427925738865128 1.3742058065472509 1.1858700262498836 4.180391276485228 4.910969998930675 5.710858234343958 7.790857007373052 3.1806714736219543 The total is 47.13

  • A. Create a java program that generates 1000 random integers. Write the 1000 random integers to...

    A. Create a java program that generates 1000 random integers. Write the 1000 random integers to a file using the Formatter class. B. Create a second java program that opens the file with the 1000 integers using the Scanner class. Read in the integers and find and print out the largest, smallest and the average of all 1000 numbers. C. Repeat A from above but use the BufferedWriter class B. Repeat B from above but use the BufferedReader class.

  • C# prograaming language 1. write a program that generates 10,000 random integers in the range of ...

    c# prograaming language 1. write a program that generates 10,000 random integers in the range of 0-9 and them in binary search tree. 2. use any sort algorithm (insertion, bubble, quick...) to display a list of each of the integers and how many times they appear. 3. at last, add ruction to the BinarySearchTree class that count the number of edges in a tree. Write a program that generates 10,000 random integers in the range of 0-9 and store them...

  • Create a JAVA application which generates 20 random numbers between 1 and 100 and writes them...

    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.

  • 1.Write a python program that writes a series of random numbers to a file named random.txt....

    1.Write a python program that writes a series of random numbers to a file named random.txt. Each random number should be in the range of 1 through 300. The application should let the user specify how many random numbers the file will hold. 2. Write another program that reads the random numbers from the random.txt file, displays the numbers, then displays the following data: I. The total of the numbers II. The number of random numbers read from the file

  • C++ need #7 and #8 6. Write code that does the following: Opens an output file...

    C++ need #7 and #8 6. Write code that does the following: Opens an output file with the filename Numbers.txt, uses a loop to write the numbers 1 through 100 to the file, and then closes the file. ofstream outputFile("Numbers.txt"); for(int number = 1; number <= 100; number++) outputFile<< number << endl; outputFile.close(); 7. Write code that does the following: Opens the Numbers.txt file that was created by the code you wrote in question 6, reads all of the numbers...

  • 4. Write a program that generates one hundred random integers between 0 and 9 and displays...

    4. Write a program that generates one hundred random integers between 0 and 9 and displays the count for each number. (Hint: Use rand()%10 to generate a random integer between 0 and 9. Use an array of ten integers, say counts, to store the counts for the number of 0’s, 1’s,…..,9’s.) this program kinda works but i cant figure out why its making more than 100 random numbers ls) [*] test2.cpp test3.cpp #include<stdio.h> #include<stdlib.h> int main() 4 { int a[14],is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT