In: Computer Science
Write an algorithm to create seven subroutines (functions) described in the following and call them in the main function
------------------------------------------------------------------------------------------------
// C++ Code
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<time.h>
using namespace std;
void generateRandomNumbers(ofstream *fout){
// generating 100 random numbers between 1 to 100 and storing them
in randomData.txt file
int i,r;
for(i=0;i<100;i++){
r = rand()%100 + 1;
*fout << r << endl;
}
}
void readRandomNumbers(ifstream *fin,int *randArray){
for(int i=0;i<100;i++){
*fin >> randArray[i];
}
}
void printRandomArray(int *randArray){
cout << "The elements of Random array are : { ";
for(int i=0;i<99;i++){
cout << randArray[i] << ",";
}
cout << randArray[99] << " }" << endl <<
endl;
}
void FindMaxMinRandArray(int *randArray){
int maxNum,minNum,maxIndex,minIndex;
maxNum = randArray[0];
minNum = randArray[0];
maxIndex = 0;
minIndex = 0;
for(int i=1;i<100;i++){
if(maxNum < randArray[i]){
maxNum = randArray[i];
maxIndex = i;
}
if(minNum > randArray[i]){
minNum = randArray[i];
minIndex = i;
}
}
cout << "The maximum Number in the random Array is " <<
maxNum << " at index " << maxIndex << endl;
cout << "The minimum Number in the random Array is " <<
minNum << " at index " << minIndex << endl;
}
void insertElementInRandArray(int *randArray){
// Element has to inserted at 51-th position in Array
// Since, the array is 0-indexed, 51-th position corresponds to
50-th index
randArray[50] = 100;
}
int deleteElements(int *randArray){
// Delete elements between 50-80(both inclusive) in the
array
// Let k be the number of deleted elements in the array
// the function returns 100 - k
int temp,i,j;
i = -1;
for(j=0;j<100;j++){
// if the element is < 50 and > 80, swap with i-th
element
if(randArray[j] < 50 || randArray[j] > 80){
i = i+1;
temp = randArray[j];
randArray[j] = randArray[i];
randArray[i] = temp;
}
}
//delete the numbers from (i+1) to n
//here i set them to be simply 0
for(j=i+1;j<100;j++){
randArray[j] = 0;
}
return (i+1);
}
void sortResidualArray(int *randArray,int n){
int i,j,minIndex,minNumber,temp;
for(i=0;i<n;i++){
minIndex = i;
minNumber = randArray[i];
for(j=i+1;j<n;j++){
if(minNumber > randArray[j]){
minNumber = randArray[j];
minIndex = j;
}
}
// swap the elements at minIndex and i
temp = randArray[minIndex];
randArray[minIndex] = randArray[i];
randArray[i] = temp;
}
//print the sorted Array
cout << endl << "The sorted elements of Random array
are : { ";
for(i=0;i<n-1;i++){
cout << randArray[i] << ",";
}
cout << randArray[i] << " }" << endl <<
endl;
}
int main(){
int i,j,n;
srand(time(0));
ofstream fout;
fout.open("randomData.txt");
generateRandomNumbers(&fout);
fout.close();
ifstream fin;
fin.open("randomData.txt");
int *randArray = (int*)malloc(sizeof(int)*100);
readRandomNumbers(&fin,randArray);
printRandomArray(randArray);
FindMaxMinRandArray(randArray);
n = deleteElements(randArray);
cout << endl << "The Remaining Number of elements in
the residual array are " << n << endl;
sortResidualArray(randArray,n);
return(0);
}
------------------------------------------------------------------------------------------------
// Screenshot of output
------------------------------------------------------------------------------------------------