In: Computer Science
write a c++ code to execute the given task.
/******************************************************************************
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 ******************************************************************************************/