Click here to Skip to main content
15,867,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The project directions are as follows:

USE INPUT.TXT FILE (#fstream)

Implement the Merge Sort algorithm in C++.

Requirement

Input of the program: Unsorted list of integers separated by spaces; your program must read input file containing the numbers to be sorted.

Output of the program: Sorted list of inputted integers separated by spaces in ascending order.

Pivot: you need to infuse randomness for choosing pivot; you come up with an idea and explain it as comments in your program.

I am kinda confused about why the program below is not running properly. I am getting several errors on my end. Any advice or help will be greatly appreciated. Thank you in advance!

I am getting these errors:

Expression must have a constant value Line 54

Expression did not evaluate to a constant Line 54

See usage of 'size' Line 54

Failure was caused by a read of variable outside its lifetime Line 54

>> result of expression not used Line 60

<< result of expression not used Line 69

What I have tried:

C++
  1  #include <iostream>
  2  #include<fstream>
  3  using namespace std;
  4  
  5  int partition(int arr[], int start, int end)
  6  {
  7  
  8  	int pivot = arr[start];
  9  
 10  	int count = 0;
 11  	for (int i = start + 1; i <= end; i++) {
 12  		if (arr[i] <= pivot)
 13  			count++;
 14  	}
 15  
 16  	// Giving pivot element its correct position
 17  	int pivotIndex = start + count;
 18  	swap(arr[pivotIndex], arr[start]);
 19  
 20  	// Sorting left and right parts of the pivot element
 21  	int i = start, j = end;
 22  
 23  	while (i < pivotIndex && j > pivotIndex) {
 24  
 25  		while (arr[i] <= pivot) {
 26  			i++;
 27  		}
 28  
 29  		while (arr[j] > pivot) {
 30  			j--;
 31  		}
 32  
 33  		if (i < pivotIndex && j > pivotIndex) {
 34  			swap(arr[i++], arr[j--]);
 35  		}
 36  	}
 37  
 38  	return pivotIndex;
 39  }
 40  
 41  void quickSort(int arr[], int start, int end)
 42  {
 43  
 44  	// base case
 45  	if (start >= end)
 46  		return;
 47  
 48  	// partitioning the array
 49  	int p = partition(arr, start, end);
 50  
 51  	// Sorting the left part
 52  	quickSort(arr, start, p - 1);
 53  
 54  	// Sorting the right part
 55  	quickSort(arr, p + 1, end);
 56  }
 57  
 58  int main()
 59  {
 60      ifstream fin;
 61  
 62  	fin.open("input.txt");
 63  
 64  	int size;
 65  	int s = 0;
 66     
 67      fin>>size;
 68      //First row having size of arrat
 69  	int arr[size];
 70  
 71  	for(int i=0;i<size;i++)
 72   {
 73  ="" fin="">>arr[i];
 74      }
 75  
 76      fin.close();
 77  
 78  	quickSort(arr, s, size - 1);
 79  
 80  	for (int i = 0; i < size; i++) {
 81  
 82  		cout << arr[i] << " ";
 83  		
 84  	}
 85  	
 86  
 87  	return 0;
 88  }
Posted
Updated 3-Oct-22 12:39pm
v4
Comments
Richard MacCutchan 3-Oct-22 8:32am    
"I am getting several errors on my end
Then please use the Improve question link above, and add complete details and where they occur.
Venki Vky 3-Oct-22 8:41am    
Please do not write in italic, it looks like you are shouting, which is considered rude. Everyone here is quite capable of reading normal text
0x01AA 3-Oct-22 8:47am    
For me italic means more quoted text ;)
Venki Vky 3-Oct-22 8:52am    
yaa its not rude but Everyone here is quite capable of reading normal text
Richard MacCutchan 3-Oct-22 8:55am    
The italics are to indicate a quote from the question. The plain text that follows is my response asking for further details.

If you do not understand how this site works then maybe you should hold back from pointless comments.

Quote:
fin>>size;
int arr[size];
You cannot do that in C++ (while, you can do that in C...).
So you either dynamically allocate the array, e.g.
C++
int * arr = new int[size];
(then you MUST:
  • check for memory allocation failure
  • release the allocated memory when you don't need it anymore
)
or use a std::vector instead of a C-like array.
 
Share this answer
 
v2
Comments
0x01AA 3-Oct-22 11:24am    
As far as I know if new fails it ends in an exception. Therefore it is allready a kind of checked ;)
CPallini 3-Oct-22 15:21pm    
You know well, my bad.
merano99 3-Oct-22 18:42pm    
My 5. If you had taken the solution with the vector as you suggested yourself, neither check nor release would have been necessary.
CPallini 4-Oct-22 1:56am    
Thank you.
Here are some problems I can see

int arr[size];


'size' isn't initilized and also must be a const.

This line seems to be corrupted

="" fin="">>arr[i];
 
Share this answer
 
Comments
XPixxeliaGamingX 3-Oct-22 9:58am    
Hi, that corrupted line is only like that because of how it was pasted. I am gonna have to paste the code again. But how do I make 'size' initialized and a constant?
0x01AA 3-Oct-22 10:27am    
You need to dynamically allocate the array, something like that (press <crlf> button to make it readable...):


int size;
int* arr;

// Set the size, e.g. by user input. Be carefull with user inputs, maybe a check for max., negative or '0' size makes sense
fin >> size;

// Create the array
arr= new int[size];

// Do you work here with the 'arr' .....

// Finally delete the array
delete[] arr;
Variable dynamic arrays are not supported in C++. It would be more convenient and flexible to use a std::vector instead of the araay anyway, as CPallini had already pointed out.
C++
//First row having size of array
//int arr[size];
std::vector <int>arr(size);

Except for the function declaration of the subroutines, the rest of the code can remain unchanged.
C++
void quickSort(std::vector<int>& arr, int start, int end);
int partition(std::vector<int>& arr, int start, int end);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900