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
For the MIPS assembly instructions below, what is the corresponding C statement? Assume that the variables f, g, h, i, and j are
Sauron [17]

Answer:

lw $t0, 16($s7) // $t0 = B[4]

lw $t1, 12($s7) // $t1 = B[3]

sub $t0, $t0, $t1 // $t0 = B[4] – B[3]

sll $t0, $t0, 2 // $t0 = $t0 * 4

add $t0, $t0, $s6 // $t0 = &A[B[4] – B[3]]

lw $t1, 0($t0) // $t1 = A[B[4] – B[3]]

add $s0, $s1, $t1 // f = g + A[B[4] – B[3]

Explanation:

lw $t0, 16($s7) // $t0 = B[4]

lw $t1, 12($s7) // $t1 = B[3]

sub $t0, $t0, $t1 // $t0 = B[4] – B[3]

sll $t0, $t0, 2 // $t0 = $t0 * 4

add $t0, $t0, $s6 // $t0 = &A[B[4] – B[3]]

lw $t1, 0($t0) // $t1 = A[B[4] – B[3]]

add $s0, $s1, $t1 // f = g + A[B[4] – B[3]

6 0
3 years ago
Read 2 more answers
An informative presentation can be used for conducting a classroom discussion.
Dmitrij [34]

Answer:

I think you have to ask you're teacher, not brainly because people on here can't answer this for you

3 0
2 years ago
Devices used for viewing images from one computer to another over the internet
garri49 [273]
Its a monitor otherwise the screen on a computer
3 0
3 years ago
Which item is used for formatting in responsive web design? Adding horizontal scroll Adding interesting text Improving image qua
vivado [14]

Answer:

3) Improving image quality

8 0
3 years ago
Now it says this and the airplane mode option is dull
AnnyKZ [126]

Explanation:

Click the airplane mode again and see if your internet turns back on. if not then click the internet symbol and check to see why its down.

7 0
3 years ago
Read 2 more answers
Other questions:
  • What are some good editing software apps for pc?
    11·1 answer
  • My computer have black spots and line
    7·2 answers
  • In a computerized accounting system, each transaction that is analyzed must be entered by hand into the appropriate journal and
    12·2 answers
  • Which expense is a bank least likely to extend a line of credit for?
    13·2 answers
  • Sharon is a skilled professional who operates logging equipment.
    12·2 answers
  • What does this image represent?
    9·2 answers
  • To prepare a data character for transmission, a ____ bit is added to the beginning of the character and informs the receiver tha
    11·1 answer
  • What is the d/c b/n information rich and information poor society​
    13·1 answer
  • Write a program to input a number and check whether it is even or odd number
    7·1 answer
  • Después de un incidente de seguridad del cliente, el equipo realiza un análisis en profundidad de cada paso dado por los atacant
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!