Question: Hello all! I'm currently trying to debug a application I have to write for my CSC 102 C++ course. I've got a good majority of
Hello all! I'm currently trying to debug a application I have to write for my CSC 102 C++ course. I've got a good majority of the code working, but when my code tries to print the final components, I get a memory error. Any help? I've attached the source code and the prompt.
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
- To generate 100 random numbers between 1-100 in a randomData.txt file
- To read the 100 random numbers from randomData.txt and store them in an array
- Print the data in the array
- Find the largest, second and third largest of the random numbers and their array position
- Delete all the elements of the array having values between 20-40 and print the residual array
- Sort the residual data in the array in an ascending order and print
- Insert elements 50 and 80 in the sorted array at their respective positions and print the final array
Here is my C++ code.
#include
using namespace std;
void writeToFile() { int num, i, min = 1, max = 100; ofstream outfile; outfile.open("randomData.txt"); srand(time(NULL));
for (i = 0; i
} outfile.close(); }
void readFromFile(int* arr) { int num, i = 0; ifstream infile; infile.open("randomData.txt"); while (infile) { infile >> num; // number by number reading. arr[i++] = num; } infile.close(); }
void printArray(int* arr, int length) { for (int i = 0; i
void find_largest(int* arr, int length) { int first, second, third; first = second = third = INT_MIN; for (int i = 0; i first) { third = second; second = first; first = arr[i]; } else if (arr[i] > second) { third = second; second = arr[i]; } else if (arr[i] > third) { third = arr[i]; } } cout
int* delete2040(int* arr, int& length) { // temp_array is used because after deletion there will be size reduction. int i, j, * temp_arr; i = -1;
// i is used as separator between all valid i.e 40 elements which is in left side // it is a simple quuicksort step. for (j = 0; j 40) { arr[++i] = arr[j]; } } //checks if there is no element 40. if (i != -1) { length = i + 1; temp_arr = new int[length]; for (int i = 0; i
// Insertion sort algorithm used. void sort_array(int* arr, int length) { int element, i, j; for (i = 1; i = 0 && element
int* sorted_insert(int* arr, int& length, int elem) { int j; length++; //length increased by 1 int* temp_arr = new int[length]; for (int i = 0; i = 0 && elem
int main() { int length = 100; int* arr = new int[length]; writeToFile(); readFromFile(arr); cout
system("pause"); return 0;
}
When I run my program, this is the error I receive, and the result after I abort the error.


As you can see, the program just halts at inserting 50 into the array. Any help?
Pile LUIL VIEW Piujell Dulu Deuuu TESL Alldlyze IUUIS EXLENISTUMISV IHUUW nei sedili VISUdl SLUUIU LIU P PHI GD: OneDrive\Documents\University of Southern Mississippi Computer Science Il Programming Assignments\PA1\Debug\PA1.exe - O X D.Initial Array Read from file: "85 43 60 20 62 74 73 76 40 3 30 11 58 54 8 37 80 81 67 59 36 80 49 35 95 57 39 99 33 72 34 98 48 86 91 64 46 53 23 50 11 52 92 23 8 14 56 97 34 4 95 70 47 50 2 84 7 67 74 67 50 82 82 50 88 95 93 49 49 10 64 97 86 12 23 56 79 67 49 39 15 93 57 74 25 4 30 3 72 20 74 45 25 20 63 29 83 66 74 78 P - sorted_insert(int * arr, int Finding 1st, 2nd and 3rd largest: First Largest: 99 Second Largest: 98 Third Largest: 97 After deleting between 20 to 40: 85 43 60 62 74 73 76 3 11 58 54 8 80 81 67 59 80 49 95 57 99 72 98 48 86 91 64 46 53 50 11 52 92 8 14 56 97 4 95 70 47 5 0 2 84 7 67 74 67 50 82 82 50 88 95 93 49 49 10 64 97 86 12 56 79 67 49 15 93 57 74 4 3 72 74 45 63 83 66 74 78 Sorted Array: 2 3 3 4 4 7 8 8 10 11 11 12 14 15 43 45 46 47 48 49 49 49 49 50 50 50 50 52 53 54 56 56 57 57 58 59 60 62 63 64 64 66 67 67 67 67 70 72 72 73 74 74 74 74 74 76 78 79 80 80 81 82 82 83 84 85 86 86 88 91 92 93 93 95 95 95 97 97 98 99 After inserting 50, array becomes: Microsoft Visual C++ Runtime Library Debug Error! Program: ...Computer Science II\Programming Assignments\PA1\Debug\PA 1.exe 147 148 149 150 HEAP CORRUPTION DETECTED: after Normal block (#834) at OxO0B8A570. CRT detected that the application wrote to memory after end of heap buffer. (Press Retry to debug the application) find_largest(arr, length); cout
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
