cs325/hw1/mergesort.cpp
//This function reads the data.txt file
This function reads the data.txt file
This function reads the data . txt file
void readFile(int *arr, int size) {
	int numLines = 0;
	string line;
	ifstream file("data.txt");
	while(getline(file, line)) {
		++numLines;
	}
	file.close();
	file.open("data.txt");
	int count = 0, x;
	while(count < 1000 && file >> x) {
		arr[count] = x;
		count++;
	}
	int arrSize = arr[0];
	int mainArrayIndex = 1;
	int *tempArray;
	for(int i = 0; i < numLines; i++) {
		tempArray = new int[arrSize];
		for(int j = 0; j < arrSize; j++) {
			tempArray[j] = arr[mainArrayIndex];
			mainArrayIndex++;
		}
		mergeSort(tempArray, 0, arrSize - 1);
		print_array(tempArray, arrSize);
		arrSize = arr[mainArrayIndex];
		mainArrayIndex++;
		delete[] tempArray;
	}
	file.close();
}
void readFile ( int * arr , int size ) { int numLines = 0 ; string line ; ifstream file ( " " ) ; while ( getline ( file , line ) ) { ++ numLines ; } file . close ( ) ; file . open ( " " ) ; int count = 0 , x ; while ( count < 1000 && file > > x ) { arr [ count ] = x ; count ++ ; } int arrSize = arr [ 0 ] ; int mainArrayIndex = 1 ; int * tempArray ; for ( int i = 0 ; i < numLines ; i ++ ) { tempArray = new int [ arrSize ] ; for ( int j = 0 ; j < arrSize ; j ++ ) { tempArray [ j ] = arr [ mainArrayIndex ] ; mainArrayIndex ++ ; } mergeSort ( tempArray , 0 , arrSize - 1 ) ; print_array ( tempArray , arrSize ) ; arrSize = arr [ mainArrayIndex ] ; mainArrayIndex ++ ; delete [ ] tempArray ; } file . close ( ) ; }
This function reads the data.txt file
This function reads the data . txt file
//Open data.txt //Get the number of lines //Close and open again so that it's pointing to the beginning of the file //Collect all of the ints into one big array //For loop runs numLines times //tempArray is initalized with the correct number of values //This for loop collects all of the ints that pertain to each line //Each array gets sorted and then printed //arrSize is changed to the next line of ints //To free memory //File closed