Question

In: Computer Science

Write a C++ program that randomly generates N integer numbers (such that N is entered by...

Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order.

Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).

Solutions

Expert Solution

Code:

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<fstream>
using namespace std;
int main(){
   srand(time(0));
   int n;
   cout<<"Enter N:";
   cin>>n;
   int i,j;
   int a[n];
   for(i=0;i<n;i++){
       a[i]=rand();
   }
   for(i=0;i<n;i++){
       for(j=0;j<n;j++){
           if(a[i]<a[j]){
               int tem;
               tem=a[i];
               a[i]=a[j];
               a[j]=tem;
           }
       }
   }
   ofstream f;
   cout<<"\n successfully stored numbers to a text file in sorted order.";
   f.open("output.txt"); //name of output file
   for(i=0;i<n;i++){
       f<<a[i]<<endl;
   }
}

Output:

text file:


Related Solutions

Write a program in C++ that generates and displays the first N three digit odd numbers....
Write a program in C++ that generates and displays the first N three digit odd numbers. Whereas the number N is provided by the user.
PYTHON Let n denote an integer entered by the user. Write a program to print n...
PYTHON Let n denote an integer entered by the user. Write a program to print n multiples of 5 in the descending order, with the last number being 5. Print the average of those n multiples
Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
Write a computer program for a logic bomb that continually generates 8-digit numbers randomly and increases...
Write a computer program for a logic bomb that continually generates 8-digit numbers randomly and increases a counter by one each time. If the random number meets the current date in a format mmddyyyy, it will display 6 times on screen the following message: Today is [date]! The count is: [nnnn] Hint: Since everyday is a different date, don’t hard code the date in your program. And the [nnnn] should be the number from your counter.
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
write a c++ program an expression that determines if an integer, n is a negative four...
write a c++ program an expression that determines if an integer, n is a negative four digit number. write a c++ program an expression that determines if a string, wd, equals "so" ignoring case.
Write a C++ program to read a collective of integer numbers. I f the number is...
Write a C++ program to read a collective of integer numbers. I f the number is greater than zero and less than 15 then terminate the loop and find factorial of the number
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it...
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it in horizontal order of the screen
its a c++ programme. In your program, you will randomly generate integer numbers between -50 and...
its a c++ programme. In your program, you will randomly generate integer numbers between -50 and +50 (including -50 and +50). You will repeat this process 1000 times. While generating these numbers, you need to count the numbers based on their signs separately, as positive and negative. For instance, if your program generate +25 15 times, and -25 19 times. Then this should be reported as, Num PosFre NegFre 25 15 19 For this problem, you need to use array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT