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.
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.
Write a function that will generate an array of random numbers. It needs to:
DO IN C++Write a function that will generate an array of random numbers. It needs to:Take 3 integers as parameters-The first is the minimum value-the second is the maximum value-the third is the size of the new array-create a dynamically allocated array of the correct size-generate a random number (between min and max) for each element in the array-return a pointer to the arrayCreate a main() function that tests the above function and displays the values in the random array.
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
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