Hi all i need some help in my merge sorting

class Database
{
protected:
		struct data
		{
			string pName;
			string pCat;
			int pBarcode;
			double pPrice;
			string pMan;
			int pStock;
			int pSold;
			data *next;
		};//struture of product
}
void Database::topX(int number,data output[]){

	data *cur=_head;
	data* *sorted=new data*[_n];// new datatype:data array of _n size, pointer pointing to pointer sorted// 
	for(int i=0;i<_n;i++){
		sorted[i]=cur;
		cur= cur->next;
	}//link list
	mergeSort(*sorted,0,_n);
}
void Database::mergeSort(data sorted[],int low,int high){//divide step
	//base case- when low < high: do nothing
	if(low>high){
		int mid=(high+low)/2;//divide array to 2
		mergeSort(sorted,low,mid);
		mergeSort(sorted,mid+1,high);

		merge(sorted,low,mid,high);
	}
	
}
void Database::merge(data sorted[],int low,int mid,int high){//conquer step
	int n = high-low+1;
	data* *b = new data*[_n];
	int left=low, right=mid+1, bIdx=0;
	
	while (left<=mid && right<=high) {
		if (sorted[left].pSold <= sorted[right].pSold)
			b[bIdx++] = sorted[left++];
		else
			b[bIdx++] = sorted[right++];
	}
	
}

i guess i have mixed up my pointer.. please someone advise what is wrong with my code


error C2440: '=' : cannot convert from 'Database::data' to 'Database::data *'
error C2440: '=' : cannot convert from 'Database::data' to 'Database::data *'

> data* *b = new data*[_n];
Why two *'s here?

because *sorted is a array of pointers. den to access must be * *..

i duno if i am correct..

Your compiler seems to disagree - so what's your choice?

i edited my code

void Database::topX(int number,data output[]){

	data *cur=_head;
	data* *sorted=new data*[_n];// new datatype:data array of _n size, pointer pointing to pointer sorted// 
	for(int i=0;i<_n;i++){
		sorted[i]=cur;
		cur= cur->next;
	}//link list

	mergeSort(sorted,0,_n);
	
	/*for(int i=0;i<_n;i++){
		for(int j=1;j<_n-1;j++){
			if(sorted[j-1]->pSold<sorted[j]->pSold){//compare item
				data* hold=sorted[j-1];//hold the min
				sorted[j-1]=sorted[j];//position of high
				sorted[j]=hold;//the other item
			}
		}
	}*///bubble sort//

	for(int i=0;i<number;i++)
	{
		output[i].pBarcode=sorted[i]->pBarcode;
		output[i].pCat=sorted[i]->pCat;
		output[i].pPrice=sorted[i]->pPrice;
		output[i].pName=sorted[i]->pName;
		output[i].pMan=sorted[i]->pMan;
		output[i].pStock=sorted[i]->pStock;
		output[i].pSold=sorted[i]->pSold;
		output[i].next=NULL;
	}
	for(int i=0;i<_n;i++)
	{
		delete sorted[i];
	}
	delete []sorted;
	sorted=NULL;
}

void Database::mergeSort(data *sorted[],int low,int high){//divide step
	//base case- when low < high: do nothing
	if(low<high){
		int mid=(high+low)/2;//divide array to 2
		mergeSort(sorted,low,mid);
		mergeSort(sorted,mid+1,high);

		merge(sorted,low,mid,high);
	}
	
}
void Database::merge(data *sorted[],int low,int mid,int high){//conquer step
	int n = high-low+1;
	data* *b = new data*[n];

	int left=low, right=mid+1, bIdx=0;
	
	while (left<=mid && right<=high) {
		if (sorted[left]->pSold <= sorted[right]->pSold){
			b[bIdx++]->pSold = sorted[left++]->pSold;
			b[bIdx++]->pBarcode = sorted[left++]->pBarcode;
			b[bIdx++]->pName = sorted[left++]->pName;
		}
		else{
			b[bIdx++]->pSold = sorted[right++]->pSold;
			b[bIdx++]->pBarcode = sorted[right++]->pBarcode;
			b[bIdx++]->pName = sorted[right++]->pName;
		}
		while (left<=mid)
			b[bIdx++] = sorted[left++];
		while (right<=high)
			b[bIdx++] = sorted[right++]; 
		for (int k=0;k<n; ++k)
			sorted[low+k] = b[k];
		delete [] b;
	}
	
}

no compiler error.

but when i run the function topX

Unhandled exception at 0x00daa8cb in Lab4.exe: 0xC0000005: Access violation writing location 0xcdcdce41.

the program stopped on this line

b[bIdx++]->pSold = sorted[left++]->pSold;

please advise what i should i change.. please advise

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.