954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

qsort Confusion

So after putting together this program, I am trying to sort multiple words alphabetically using [TEX]qsort ()[/TEX]. I have been using examples of [TEX]qsort ()[/TEX] found on the web and trying to adapt them to my cause, but all I've done so far is confuse myself. There are no errors, but it crashes when I run it.

Can anyone help?

#include <iostream>
#include <iomanip>
#include <search.h>
#include <string.h>
#include <cctype>
#include <stdio.h>
#include <stdlib.h>
using namespace std;


const int MAX = 100;

int compare(const void* pnum1, const void *pnum2);



void main()
{

	int i=0;
	int length;
	char *delim = " ";
	char *token;
	char string[MAX];
	char* array[255] = {0}; // assume max of 255 pointers
	cout << "Please enter a sequence of words. Press enter when completed" << endl;
	cin.getline(string, MAX);
	length = strlen (string);
	while (i < length)
	{
		string[i] = tolower(string[i]);
		i++;
	}
	cout << "Lower case the words are:\n"
		 << string << endl;


	token = strtok(string, delim);
	int c = 0;
	while (token != NULL)
	{
		
		cout << "token " << c << " is " << endl << token << endl;
		array[c] = token;
		token = strtok(NULL,delim);
		c++;
	}
	qsort(array[c], length, sizeof(int), compare);
	cout << "\n\nThe alphabetized words are:\n";
	for (i = 0; i < length; i++)
	{
		cout << array [i] << endl;
	}
}



	int compare(const void* pnum1, const void *pnum2)
	// compares two numbers, returns -1 if first is smaller
	// returns 0 is they are equal, returns 1 if first is larger
	{
	int num1, num2;

	num1 = *(int *)pnum1; // cast from pointer to void
	num2 = *(int *)pnum2; 

	if(num1 < num2)
	return -1;
	else
	if (num1 == num2)
	return 0;
	else
	return 1;
	}


Thanks, I'm fried :(

picklesandmayo
Newbie Poster
21 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

>>qsort(array[c], length, sizeof(int), compare);
that is incorrect. You are sorting an array of char pointers not an array of integers qsort(array, c, sizeof(char*), compare);

Your compare function is also wrong. Why are you converting the char pointers to integers? I thought the strings are words? All you need is this?

int compare(const void* pnum1, const void *pnum2)
{
    return strcmp((char*)pnum1, (char*)pnum2);
}
for (i = 0; i < length; i++) { cout << array [i] << endl; }

wrong there too.length is the number of characters in the original string. What you want here is variable c, which is the number of valid pointers in array

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thank you. I am still having a problem though... I think I'm retarded :$

For some reason, the values in the array don't rearrange. Here's what I've got:

#include <iostream>
#include <iomanip>
#include <search.h>
#include <string.h>
#include <cctype>
#include <stdio.h>
#include <stdlib.h>
using namespace std;


const int MAX = 100;

int compare(const void *a, const void *b);



void main()
{

	int i=0;
	int length;
	char *delim = " ";
	char *token;
	char string[MAX];
	char* array[255] = {0}; // assume max of 255 pointers
	cout << "Please enter a sequence of words. Press enter when completed" << endl;
	cin.getline(string, MAX);
	length = strlen (string);
	while (i < length)
	{
		string[i] = tolower(string[i]);
		i++;
	}
	cout << "Lower case the words are:\n"
		 << string << endl;


	token = strtok(string, delim);
	int c = 0;
	while (token != NULL)
	{
		
		cout << "token " << c << " is " << endl << token << endl;
		array[c] = token;
		token = strtok(NULL,delim);
		c++;
	}
	qsort(array, c, sizeof(char*), compare);
	cout << "\n\nThe alphabetized words are:\n";
	for (i = 0; i < c; i++)
	{
		cout << array [i] << endl;
	}
}


int compare(const void *a, const void *b)
{
    return strcmp((char*)a, (char*)b);
}
picklesandmayo
Newbie Poster
21 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

anyone?

picklesandmayo
Newbie Poster
21 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Yes, see this thread . Just change your compare function as shown in that thread.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanks! It works now!

picklesandmayo
Newbie Poster
21 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You