1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
arsen [322]
2 years ago
10

Write a program that writes 10 random numbers into a file named numbers using loops.

Computers and Technology
1 answer:
MariettaO [177]2 years ago
3 0

Answer:

The program in cpp for the given scenario is shown below.

#include <stdio.h>

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

   //object created of file stream

   ofstream out;

   //file opened for writing

   out.open("numbers.txt");

   std::cout << "File opened." << std::endl;

   //inside for loop, random numbers written to numbers.txt using rand()

   for(int n=0; n<10; n++)

   {

       out<<rand()<<endl;

   }

   //file closed

   out.close();

   std::cout << "File closed." << std::endl;

   return 0;

}

Explanation:

1. An object of the ofstream is created. The ofstream refers to output file stream. This is used to create file for write and append operations.

ofstream out;

2. The file named, numbers.txt, is opened using the open() method with the object of ofstream.

out.open("numbers.txt");

3. The message is displayed to the user that the file is opened.

4. Inside a for loop, which executes 10 times, a random number is generated. This random number is written to the file and a new line inserted.

5. The random number is generated using rand() method. Every number is written on  new line.

out<<rand()<<endl;

6. Every execution of the for loop repeats steps 4 and 5.

7. When the loop ends, the file is closed using close() method with the object of ofstream.

out.close();

8. A message is displayed to the user that the file is closed.

9. The header file, fstream, is included to support file operations.

10. The extension of the file can be changed from .txt to any other type of file as per requirement.

11. The program can be tested for writing more numbers to the file.

12. The program can be tested for writing any type of numeric data to the file.

13. The program is written in cpp due to simplicity.

14. In other languages such as java or csharp, the code is written inside classes.

15. In cpp, all the code is written inside main() method.

16.    The screenshot of the output messages displayed is attached.

You might be interested in
What is the difference, if any, between a project manager and a producer on a digital media production team? A project manager o
Flauer [41]

Answer:

A project manager just oversees the creative element, while a producer oversees the entire project.

It is B

7 0
2 years ago
The _____ option will allow a user to remove text from one location and keep it for use later on the clipboard.
ValentinkaMS [17]

I believe the answer is the "cut" option?

8 0
3 years ago
Jerome falls sick after eating a burger at a restaurant. After a visit to the doctor, he finds out he has a foodborne illness. O
babunello [35]

Answer: food poisning

Explanation:

either the food wasnt

cooked properly or the food was out of date and went bad

3 0
3 years ago
Which package is the Graphics class part of? _________________ .
Ksenya-84 [330]

Answer: C) java.awt

Explanation:

Java.awt is commonly used in AWT (abstract window toolkit) which basically contain core graphics in AWT class. The java.awt is the important package in the AWT. It basically contain the main capabilities of the graphics and define the GUI ( graphical user interface) frame in the java platform.

It also support the event handling and also the event class like window and key event in the java.awt.

Therefore, option (C) is correct.

8 0
3 years ago
Which of the following is the best reason for including a photograph in a formal business document
Bond [772]

Answer:

The answer is "The reader wants to see how your product looks".

Explanation:

Some information is missing in the question. so, the correct choice can be described as follows:

  • The corporation uses records and reports to transfer facts, statistics, and figures, including explanations for enhancing activities, administration, and sales.  
  • It generally refers to the various documentation, all with various sections and contents.  
  • In the organization's usage of records for correspondence, transaction, and product research, that's why the "reader wants to see how your product looks" is the correct choice.
8 0
3 years ago
Other questions:
  • A ____ operating system should be capable of supporting the applications and tools necessary to support Internet operations.
    10·1 answer
  • When would you use an omnidirectional microphone?
    9·1 answer
  • A computer retail store has 15 personal computers in stock. A buyer wants to purchase 3 of them. Unknown to either the retail st
    14·1 answer
  • Write a MARIE program to calculate some basic statistics on a list of positive numbers. The program will ask users to input the
    6·1 answer
  • What is the primary responsibility of the federal reserve bank??
    11·1 answer
  • A line beginning with a # will be transmitted to the programmer’s social media feed.
    11·2 answers
  • You work in an office that uses Linux and Windows servers. The network uses the IP protocol. You are sitting at a Windows workst
    13·1 answer
  • 2.4 Code Practice: Question 2
    5·1 answer
  • Which statement about intellectual property is true? (CSI-7.6) Group of answer choices It is okay to use code you find on the in
    14·1 answer
  • What type of e-mail typically lures users to sites or asks for sensitive information?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!