Question: The following C++ program has error. Please i need help. //Error Message: (./vpl_execution: line 2: 26145 Segmentation fault (core dumped) ./BoatPlane) #include #include #include using
The following C++ program has error. Please i need help.
//Error Message: (./vpl_execution: line 2: 26145 Segmentation fault (core dumped) ./BoatPlane)
#include
#include
#include
using namespace std;
void Merge(int *a, int low, int high, int mid);
void MergeSort(int *a, int low, int high);
void SelectionSort(int *a, int low,int high);
void swap(int *x, int *y);
int main (){
ifstream file;
fstream outfile;
int n=5001025;
int array[n];
uint32_t a;
file.open("threesData.bin",ios::binary | ios::in);
if (!file)
{
cout << "Error opening file ";
}
int count = 0;
while (!file.eof()){
file.read(reinterpret_cast
count++;}
file.close();
MergeSort(array,0,count-1);
// print the array elements to file
for(int i=0;i outfile.open("sortedThreesData.bin", ios::out | ios::binary); for(int i=0;i outfile.write(reinterpret_cast outfile.close();} return 0;} void Merge(int *a, int low, int high, int mid) { // We have low to mid and mid+1 to high already sorted. int i, j, k, temp[high-low+1]; i = low; k = 0; j = mid + 1; // Merge the two parts into temp[]. while (i <= mid && j <= high) { if (a[i] < a[j]) { temp[k] = a[i]; k++; i++; } else { temp[k] = a[j]; k++; j++; } } // Insert all the remaining values from i to mid into temp[]. while (i <= mid) { temp[k] = a[i]; k++; i++; } // Insert all the remaining values from j to high into temp[]. while (j <= high) { temp[k] = a[j]; k++; j++; } // Assign sorted data stored in temp[] to a[]. for (i = low; i <= high; i++) { a[i] = temp[i-low]; } } // A function to split array into two parts. void MergeSort(int *a, int low, int high) { int mid; if (low < high) { mid=(low+high)/2; // Split the data into two half. SelectionSort(a, low, mid); SelectionSort(a, mid+1, high); // Merge them to get sorted output. Merge(a, low, high, mid); } } void SelectionSort(int *a, int low,int high) { int i, j, min_idx; // One by one move boundary of unsorted subarray for (i = low; i <=high; i++) { // Find the minimum element in unsorted array min_idx = i; for (j = i+1; j <= high; j++) if (a[j] < a[min_idx]) min_idx = j; // Swap the found minimum element with the first element swap(&a[min_idx], &a[i]); } } void swap(int *x, int *y) { int tmp = *x; *x = *y; *y = tmp; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
