Question: OK IM SENDING MY SOURCE CODE, I BELIEVE THERE IS SOME ERROR EVEN THOUGH IT STILL COMPILES. INT MAIN () looks to open the ThreeData
OK IM SENDING MY SOURCE CODE, I BELIEVE THERE IS SOME ERROR EVEN THOUGH IT STILL COMPILES. INT MAIN () looks to open the "ThreeData" binary file, store the values in an array and then write those files in "sortedBinaryfile". Please look over my main (), everything else should be in order. I will attach my header file, function declarations and most importantly my main (). The issue I'm having is not opening the 32 byte integrar binary file but storing those in the array and passing them through the mergesort function. Please let me know what I can change or do.
// Main fucntion followed by header then function protypes....
#include
using namespace std; int main() {
ifstream file; fstream outfile;
int n=0; 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
MergeSort(array,0,n-1);
// print the array elements to file
for(int i=0;i
return 0;}
#ifndef MERGESORT_H_INCLUDED #define MERGESORT_H_INCLUDED
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);
#endif // MERGESORT_H_INCLUDED
#include "Mergesort.h"
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
