Question

In: Computer Science

To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random...

  1. To generate 100 random numbers between 1-100 in a randomData.txt file
  2. To read the 100 random numbers from randomData.txt and store them in an array
  3. Print the data in the array
  4. Find the smallest and the largest of the random numbers and their array position
  5. Insert an element of value100 in the 51th position of the array
  6. Delete all the elements of the array having values between 50-80 and print the residual array
  7. Sort the data in the final array(residual) in an ascending order and print

write a c++ code to execute the given task.

Solutions

Expert Solution

/******************************************************************************

Code Below

*******************************************************************************/

#include <iostream>
#include <fstream>
using namespace std;
#define lower 1
#define higher 100
int main()
{
int num[100];
int max = 0;
int min = 100;
int cnt = 0;
int num_elements = 0;
int index_max, index_min;
ofstream file;
ifstream file1;
file.open ("randomData.txt");
int ran = higher - lower;
for(int i=0; i < 100; i++ ){
int randNum = lower + rand() % ran; //generating the random number
file << randNum << " "; //Writing in file
}
file.close();
file1.open ("randomData.txt");
if (file1.is_open()){
for(int i=0; i< 100; i++){
file1 >> num[i]; //reading from file
}
file1.close();
}
else cout << "Not able to open file";
cout << "Displaying RANDOM ARRAY" << " ";
for(int i= 0; i < 100; i++){
cout << num[i] << " "; //Displaying all numbers in array
if(num[i] > max){ //Finding maximum
index_max = i;
max = num[i];
}
if(num[i] < min){ //Finding minimum
index_min = i;
min = num[i];
}
}
cout << " ";
cout << "maximum element: " << max << " and its index: " << index_max << endl;
cout << "minimum element: " << min << " and its index: " << index_min << endl;

//Inserting 100 at 51th position
for (int i = 100 - 1; i >= 51 - 1; i--)
num[i+1] = num[i];

num[51] = 100;

for(int i=0; i<100-cnt; i++)
   {
       if(num[i] > 50 && num[i] < 80)
       {
           for(int j=i; j<(100-cnt); j++)
           {
               num[j]=num[j+1];
           }
           i=0;
           cnt++;
       }
   }
   cout << "++++++++++++++++ Residual array ++++++++++++++++++"<<endl;
num_elements = 100 -cnt;
   for(int i=0; i<num_elements; i++){
   cout << num[i] << " ";
     
   }
   cout << " ";
  
   // Sorting of array in ascending order

   for(int i=0 ; i<num_elements; i++)
   {      
       for(int j=i+1 ; j<num_elements;j++)
       {
           if(num[i]>num[j])
           {
               int temp = num[i];
               num[i]=num[j];
               num[j]=temp;
           }
       }
   }
   cout << "++++++++++++++++ Residual array in ascending order ++++++++++++++++++" << endl;
   //Printing of array in ascending order
   for(int i=0; i<num_elements; i++){
   cout << num[i] << " ";
   }
   cout << " ";

  
return 0;
}

/********* Screen shot of output ******************************************************************************************/



Related Solutions

1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
Propose two of your own random number generation schemes. Please generate 100 random numbers ?? (?...
Propose two of your own random number generation schemes. Please generate 100 random numbers ?? (? = 1,2, … ,100) for each scheme and show the results on the same plot for comparison (i.e., x-axis of the plot will show the index ? and y-axis will show the generated random numbers ??. You can use different colors and/or symbols to distinguish one sequence from the other). Discuss which scheme will be preferred.
Using Python, generate 100 random numbers ranging from 1 to 5, then plot them using matplotlib...
Using Python, generate 100 random numbers ranging from 1 to 5, then plot them using matplotlib or Seaborn as a histogram
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary search tree using the numbers of the sequence. The tree set should not have any nodes with same values and all repeated numbers of the random sequence must be stored in the node as a counter variable. For example, if there are five 20s’ in the random sequence then the tree node having...
****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them...
****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them to a text file on separate lines. Then the program should read the numbers from the file, calculate the average of the numbers, and display this to the screen.
Generate 1000 random numbers from ??3? starting with standard normal random numbers in R.
Generate 1000 random numbers from ??3? starting with standard normal random numbers in R.
Part 1: Using Excel’s Randbetween(0,9) function, generate 200 samples of five random numbers between 0 and...
Part 1: Using Excel’s Randbetween(0,9) function, generate 200 samples of five random numbers between 0 and 9, calculate the mean of each sample. Show me the list of the 200 means. Typically, they should look like: 4.8, 3.6, 4.4, 6.0, etc. Part 2: Using Excel, calculate the overall mean of the 200 sample means (the average of the averages). This should be around 4.5. Part 3:    Using Excel, calculate the standard error of the mean (SEM)  (i.e. the standard deviation of...
Generate 2500 random numbers that are uniformly distributed between 90 and 160. Prove experimentally that STD...
Generate 2500 random numbers that are uniformly distributed between 90 and 160. Prove experimentally that STD of sample means = STD of Population/sqrt(sample size) for sample sizes of 10 and 100. How close is your calculation of STD of sample means to the theoretical approximation? Keep number of samples in each case equal to sample size. Repeat for normal and weibull (also between 90 and 160). What does it say about STD of sample means as you increase your sample...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT