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

C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The...
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each user input, the program tells the user whether the input is too low or too high, so the user can choose the next input intelligently. Here is a sample run:
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
Let a , b , c be three integer numbers. Write a C++ program with a...
Let a , b , c be three integer numbers. Write a C++ program with a functions void rotate1(int* a,int* b,int* c) void rotate2(int& a,int& b,int& c) such that a -> b , b -> c and c -> a. Thus we use two different approaches (pointers in rotate1 and references in rotate2).
Write a program in java which randomly generates two integer number n1 and n2 (suppose the...
Write a program in java which randomly generates two integer number n1 and n2 (suppose the range for each integer is [1, 100]), then asks the user what is the value of n1*n2, if the user’s answer is correct, call method printGoodComment to print out something nice, otherwise, call printBadComment to print out something “mean”. The method signatures are:                   public static void printGoodComment() and                   public static void printBadComment() in your printGoodComment method, it will randomly print one sentence from the...
Write a program in java which randomly generates two integer number n1 and n2 (suppose the...
Write a program in java which randomly generates two integer number n1 and n2 (suppose the range for each integer is [1, 100]), then asks the user what is the value of n1*n2, if the user’s answer is correct, call method printGoodComment to print out something nice, otherwise, call printBadComment to print out something “mean”. The method signatures are:                   public static void printGoodComment() and                   public static void printBadComment() in your printGoodComment method, it will randomly print one sentence from the...
1- Write a Java program called ArabicMonth that randomly generates an integer between 1 and 12...
1- Write a Java program called ArabicMonth that randomly generates an integer between 1 and 12 and displays the month name Jan, Feb, …, December for the number 1, 2, …, 12, accordingly.. Sample Run: 9 Oct 2- Write a program that prompts the user to enter the exchange rate from currency in U.S. dollars to Saudi Riyals. Prompt the user to enter 0 to convert from U.S. dollars to Saudi Riyals and 1 to convert from Saudi Riyal and...
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 program that reads n integer values. If a negative value is entered, we want...
Write a program that reads n integer values. If a negative value is entered, we want to terminate the input, i.e., exit from the loop. If a zero value is entered, we want to ignore it and read the next value. Any strictly positive values (greater or equal zero) are to be totaled. Print the number of values read, the number of values totaled and the total. If a negative value is entered, print an error message before terminating the...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT