Question: I'm having issues with inserting 50 and 80 into my code. Problem 1: Write C++ program in .cpp file and algorithm in .doc file to

I'm having issues with inserting 50 and 80 into my code.

Problem 1: Write C++ program in .cpp file and algorithm in .doc file to create seven subroutines (functions) described in the following and call them in the main function

  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 largest, second and third largest of the random numbers and their array position
  5. Delete all the elements of the array having values between 20-40 and print the residual array
  6. Sort the residual data in the array in an ascending order and print
  7. Insert elements 50 and 80 in the sorted array at their respective positions and print the final array

My code:

#include #include #include #include #include #include

using namespace std; ofstream randomData; ifstream inputrandomData; void randomgenerator(); void read(int* numarray); void printArray(int* numarray); void searchArray(int* numarray); void Delete(int* numarray); void sortArray(int* numarray); void insertArray(int* numarray);

void randomgenerator() { srand(time(0)); randomData.open("randomData.txt"); for (int counter = 0; counter < 100; counter++) { randomData << rand() % 100 + 1 << endl; } randomData.close();

}

void read(int* numarray) { inputrandomData.open("randomData.txt");

for (int i = 0; i < 100; i++) { inputrandomData >> numarray[i];

} inputrandomData.close();

}

void printArray(int* numarray) {

for (int index = 0; index < 100; index++) { cout << numarray[index] << endl; }

}

void searchArray(int* numarray) { int searchedArray[6] = {}; for (int index = 0; index < 100; index++) { if (numarray[index] > searchedArray[0]) { searchedArray[0] = numarray[index]; searchedArray[1] = index;

} } for (int index = 0; index < 100; index++) { if (numarray[index] > searchedArray[2] && numarray[index] < searchedArray[0]) { searchedArray[2] = numarray[index]; searchedArray[3] = index; } }

for (int index = 0; index < 100; index++) { if (numarray[index] > searchedArray[4] && numarray[index] < searchedArray[2]) { searchedArray[4] = numarray[index]; searchedArray[5] = index; } } cout << "Largest Number: " << searchedArray[0] << " " << "Index: " << searchedArray[1] << endl; cout << "Second Largest Number: " << searchedArray[2] << " " << "Index: " << searchedArray[3] << endl; cout << "Third Largest Number: " << searchedArray[4] << " " << "Index: " << searchedArray[5] << endl;

}

void Delete(int* numarray) {

int arrayindex = 0; for (int index = 0; index < 100; index++) { if (numarray[index] > 20 && numarray[index] < 40) { numarray[index] = -1;

}

if (numarray[index] > 0) { cout << numarray[index] << endl; } }

}

void sortArray(int* numarray) { int temp; int size = 100; int min = numarray[0];

for (int i = 0; i < size - 1; i++) {

min = numarray[i]; for (int j = i + 1; j < size; j++) { if (min >= numarray[j]) { min = numarray[j]; temp = numarray[i]; numarray[i] = numarray[j]; numarray[j] = temp; } }

} for (int i = 0; i < size; i++) { if (numarray[i] >= 0) { cout << numarray[i] << endl; } } }

void insertArray(int* numarray) { int num1 = 50; int num2 = 80; int counter = 0; int newarray[103] = {}; for (int newindex = 0; newindex < 103; newindex++) { if (numarray[newindex] <= num1 && numarray[newindex + 1] > num1) { newarray[newindex] = num1; newarray[newindex + 1] = numarray[newindex];

} else if (numarray[newindex] <= num2 && numarray[newindex + 1] > num2) { newarray[newindex] = num2; newarray[newindex + 1] = numarray[newindex];

} else { newarray[newindex] = numarray[newindex]; } cout << newarray[newindex] << endl; }

}

int main() { int numarray[100] = {}; randomgenerator(); read(numarray); printArray(numarray); searchArray(numarray); Delete(numarray); sortArray(numarray);

insertArray(numarray); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!