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]
3 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]3 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
Explain the paging concept and main disadvantages of pipelined
Furkat [3]

Answer:

PAGINACIÓN En la gestión de memoria con intercambio, cuando ... Debido a que es posible separar los módulos, se hace más fácil la modificación de los mismos. ... Ventajas y Desventajas de la segmentación paginada

Explanation:

8 0
3 years ago
Code embedded into an HTML page and downloaded by a user; resides on the client and helps process Web form input. Common clients
Stels [109]

Answer:

TRUE

Explanation:

8 0
3 years ago
Wireless attacks avoid the access points to limit detection. <br> a. True <br> b. False
-BARSIC- [3]

Answer:

Option B i.e., False.

Explanation:

The weakness, KRACK tricks a wifi access points to reuse an in-use encryption, enabling the intruder to decode and interpret data intended to remain encrypted. Wireless communication encrypts, that decrypt unencrypted wireless network activity and expose sensitive data.

So, the following scenario is false about the wireless attack.

6 0
3 years ago
Write a program that prompts the user for their quarterly water bill for the last four quarters. The program should find and out
Fed [463]

Answer:

total = 0

for i in range(4):

   bill = float(input("Enter bill for quarter " + str(i+1) + ": "))

   total += bill

average = total / (4 * 3)

if average > 75:

   print("Average monthly bill is $" + str(average) + ". Too much water is being used")

if 25 <= average < 75:

   print("Average monthly bill is $" + str(average) + ". A typical amount of water is being used")

if average < 25:

   print("Average monthly bill is $" + str(average) + ". Thank you for conserving water")

Explanation:

*The code is in Python.

Create a for loop that asks the user to enter the bill for 4 quarters and calculate the total of the bills

Calculate the average monthly bill, divide the total by 12

Check the average for the given conditions. Depending on its value print the required message along with the average monthly bill

4 0
4 years ago
Electricity fact topic
gregori [183]
''for kids'' i think thats what that mean
5 0
3 years ago
Read 2 more answers
Other questions:
  • What kind of firewall can block designated types of traffic based on application data contained within packets?
    7·1 answer
  • When selecting current page from the print range, on the print dialong box it will print
    5·1 answer
  • How do you change the slide layout?
    6·2 answers
  • Your program will demonstrate use of arrays, searching an array, using methods, passing parameters by reference, returning value
    13·1 answer
  • technology might not possess emotional intelligence but it can certainly influence ours. how have technological changes affected
    7·1 answer
  • Gigantic Life Insurance is organized as a single domain. The network manager is concerned that dividing into multiple domains to
    15·1 answer
  • Fill in the blank with the correct response.
    5·2 answers
  • You can find synonyms and disciplinary jargon in the ______, _______, and ______ in your search results. You can then use these
    13·1 answer
  • What does the following loop do? val = 0 total = 0 while (val &lt; 10): val = val + 1 total = total + val print(total)
    15·1 answer
  • Pls paanswer asap......​
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!