Hi,

I am working on a C++ select algorithm using the qsort function.
I am suppose to first sort the array and then return the i-th smallest value, which is given by the stat value.

However, I have no idea how to return the value from a void array.
I would appreciate if someone could help me.

This is my code:

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

using namespace std;

int qsortselect(void *keys, int n, int stat, int size, int (*compare)(const void *,const void *))
{
	qsort(keys,n,sizeof(char)*size,compare);

	return &keys[stat*size];

}

int compare(const void  *x,const void  *y)
{
	return(* ((int*)x)-*((int *)y));
}

void main()
{
	int A[5]={3,5,67, 7, 2};

	for(int i=0; i<5;i++)
	{
		cout << A[i] << " ";
	}

	int stat = qsortselect(A,5,2,sizeof(int), compare);

	cout << endl << stat << endl;

	getch();
}

You have made that problem a lot more difficult than it needed to be.

int compare(const void  *x,const void  *y)
{
	return(* ((int*)x)-*((int *)y));
}

int qsortselect(int keys[], int n, int size)
{
	qsort(keys,n,sizeof(int),compare);

	int x = keys[size];
    return x;

}


int main()
{
	int A[5]={3,5,67, 7, 2};

	for(int i=0; i<5;i++)
	{
		cout << A[i] << " ";
	}

	int stat = qsortselect(A,5,2);

	cout << endl << stat << endl;

	getch();
}

I should have explained a bit more. Tthe qsortselect() function is suppose to be a generic function that takes any values such as int, float, double, etc. So the type is not known.

The main method is not important, I just posted it to give an example. Is there a way to make the function return an int value?
I am quite new to void pointers so I am not familiar with other methods to reference an address value.

I should have explained a bit more. Tthe qsortselect() function is suppose to be a generic function that takes any values such as int, float, double, etc. So the type is not known.

The main method is not important, I just posted it to give an example. Is there a way to make the function return an int value?
I am quite new to void pointers so I am not familiar with other methods to reference an address value.

I don't fully understand what you are trying to say, but I think this will be helpful:

http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr240.htm

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.