GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Dec-2005, 20:11
sasuke101 sasuke101 is offline
New Member
 
Join Date: Sep 2005
Posts: 24
sasuke101 is on a distinguished road
Question

help with a quicksort program


I got some errors with my quicksort program. It is supposed to sort a vector and also count the number of swaps done. any help would be appreciate.
thank you

my header file
CPP / C++ / C Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <cstdlib>

using namespace std;

class quicksort
{
public:
	quicksort( unsigned int vectorSize );	//constructor with arguments
	~quicksort();				//destructor
	void qsort(T data[], int first, int last);		//function to bubble sort the values.
	void put();				//function to print the values.
	

	
private:
	std::vector<int> data;		//vector testData
	
   
};

my bub1 cpp file
CPP / C++ / C Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "bub1.h"  // Header file
#include <vector>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
using namespace std;

quicksort::quicksort()			 //constructor     
{
int size;
	 

	 cout<<"Enter an array of 10, 100, 1000 to be randomly sorted"<< endl;
         cin >> size;		//get the size required by the user.
     
	 (srand( (unsigned)time( NULL ) )); //seed the rand function
      
	 data.resize( size );  //resize the sort

         for (int i=0;  i < size; i++)
         {
            data[i] = rand();     // pointers of the function srand
         }
}

quicksort::~quicksort()					 // Destructor
{
    data.clear();                //Clear the vector contents

}
template <class T>
void quicksort::qsort(T data[], int first, int last)
{
	int lower = first+1, upper = last;					
int count = 0;
   swap(data[first], data[(first+last)/2]);
   T bound = data[first];
   while (lower<=upper)
   {
	   while (data[lower] < bound)
		   lower++;
	   while (bound < data[upper])
		   upper--;
	   if (lower < upper)
		   swap(data[lower++], data[upper--]);
	   else lower++;
   }
   swap(data[upper], data[first]);
   if (first < upper-1)
	   quicksort (data,first,upper-1);
   if (upper+1 < last)
	   quicksort (data,upper+1,last);
}
quicksort(T data[], int n);
{
	if (n<2)
		return;
	for (int i = 1, max = 0; i < n; i++)
		if (data[max] < data[i])
			max = i;
		swap(data[n-1], data[max]);
		quicksort(data, 0, n-2);

            if ( data[j] > data[j+1] ) 
			{
				temp = data[j];			//sort the data in Data vector itself. 
				data[j] = data[j+1];
				data[j+1] = temp;
				count++;
			 }	
			
			cout<< "\n" << "here are the number of swaps done" << endl;
	cout << count << endl;
}




void quicksort::put()                            //prints the contents of arr
{
	
	cout<< "\n" << "Here are your numbers"<< endl;  
    
	for (int i =0; i < data.size(); i++)
        cout<< "\n" << data[i] << endl;        //print the output.
	
	
}

and here is my bub file
CPP / C++ / C Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "bub1.h"  // Header file

int main()
{

    sort a;                //create object a. this automatically calls the default constructor
    
    cout<<"Before sorting"<<endl;

    a.put();                //prints the value in arr

    a.qsort();        //sort arr

    cout<<"After sorting"<<endl;

    a.put();                //prints the value of arr

	cin.get();              //pause the program for a while    

   

    return 0;

}

here are the errors i get

c:\\bub1.h(16) : error C2061: syntax error : identifier 'T'
c:\\bub1.cpp(12) : error C2511: 'quicksort::quicksort' : overloaded member function 'void (void)' not found in 'quicksort'
c:\\bub1.h(12) : see declaration of 'quicksort'
c:\\bub1.cpp(56) : error C2244: 'quicksort::qsort' : unable to resolve function overload
c:\\bub1.cpp(57) : error C2199: syntax error : found 'quicksort (' at global scope (was a declaration intended?)
c:\\bub1.cpp(5 : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.

bub1.obj - 5 error(s), 0 warning(s)
  #2  
Old 03-Dec-2005, 08:07
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: help with a quicksort program


Quote:
Originally Posted by sasuke101
here are the errors i get

c:\\bub1.h(16) : error C2061: syntax error : identifier 'T'
c:\\bub1.cpp(12) : error C2511: 'quicksort::quicksort' : overloaded member function 'void (void)' not found in 'quicksort'
c:\\bub1.h(12) : see declaration of 'quicksort'
c:\\bub1.cpp(56) : error C2244: 'quicksort::qsort' : unable to resolve function overload
c:\\bub1.cpp(57) : error C2199: syntax error : found 'quicksort (' at global scope (was a declaration intended?)
c:\\bub1.cpp(5 : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.

bub1.obj - 5 error(s), 0 warning(s)

You have defined your Constructor to take an unsigned int parameter in your class definition, but then only implemented the default constructor in the .cpp. You must implement the other one too, so the linker will be able to resolve the links
Define the default constructor as well in the header while you are at it. Otherwise, you might leave the impression you are trying to hide the default constructor for the class from the users (although it is public by default, it might not be so obvious).

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 04-Dec-2005, 09:31
sasuke101 sasuke101 is offline
New Member
 
Join Date: Sep 2005
Posts: 24
sasuke101 is on a distinguished road

Re: help with a quicksort program


im not getting what u mean?
  #4  
Old 04-Dec-2005, 19:29
sasuke101 sasuke101 is offline
New Member
 
Join Date: Sep 2005
Posts: 24
sasuke101 is on a distinguished road

Re: help with a quicksort program


i am completely lost in trying to fix this anymore suggestions?
  #5  
Old 04-Dec-2005, 19:54
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: help with a quicksort program


Declare the default constructor in the header file....(this is what Lucian meant)
CPP / C++ / C Code:
public:
    quicksort( );                                 // default constructor
    quicksort( unsigned int vectorSize );    //constructor with arguments
    ~quicksort();    


Then in your header file, you didnt define T.
So, declare like this:
CPP / C++ / C Code:
        template <class T>
	void qsort(T data[], int first, int last);		//function to bubble sort the values.

in your header file.

And in you main function, you didnt pass any arguments to the qsort function.
So, change that like this:
CPP / C++ / C Code:
//define data, first and last here...
    a.qsort(data, first, last);        //sort data

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #6  
Old 04-Dec-2005, 22:00
sasuke101 sasuke101 is offline
New Member
 
Join Date: Sep 2005
Posts: 24
sasuke101 is on a distinguished road

Re: help with a quicksort program


a.qsort(data, first, last); //sort data

how do you go about declaring these identifiers?
i get a error asking to declare them?
  #7  
Old 05-Dec-2005, 08:55
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: help with a quicksort program


I went through your code "in detail" many things were wrong.
First, where is the swap function?

And, try to implement the quicksort function using int only, and then we'll switch to templates.

According to your code, we cant pass the values in main.
So, lets create an overloaded quick_sort function which takes no arguments, in the class:
CPP / C++ / C Code:
void quick_sort()
{
    quick_sort(data, 0, data.size());
}
Can you say why we did like this?

And remove this unnecessary code in your bub.cpp(shouldn't it be qui.cpp??)
CPP / C++ / C Code:
quicksort(T data[], int n);
{
    if (n<2)
        return;
    for (int i = 1, max = 0; i < n; i++)
        if (data[max] < data[i])
            max = i;
        swap(data[n-1], data[max]);
        quicksort(data, 0, n-2);

            if ( data[j] > data[j+1] ) 
            {
                temp = data[j];            //sort the data in Data vector itself. 
                data[j] = data[j+1];
                data[j+1] = temp;
                count++;
             }    
            
            cout<< "\n" << "here are the number of swaps done" << endl;
    cout << count << endl;
}


Another thing. You have used many headers in your files, which are unnecessary.
And dont include headers in the cpp files again. It is already included in your header file.

If you get the program correct, then as you posted earlier, use templates.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #8  
Old 05-Dec-2005, 18:46
sasuke101 sasuke101 is offline
New Member
 
Join Date: Sep 2005
Posts: 24
sasuke101 is on a distinguished road

Re: help with a quicksort program


oye i am so lost on this one
  #9  
Old 08-Dec-2005, 14:38
sasuke101 sasuke101 is offline
New Member
 
Join Date: Sep 2005
Posts: 24
sasuke101 is on a distinguished road

Re: help with a quicksort program


really need some help i cant get it to work at all it keeps telling me to declare data and it is due tonight any help would be appriciated
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a file in [c] i hate c C Programming Language 15 21-Nov-2005 12:52
Type casts ? kai85 C++ Forum 12 23-Jun-2005 12:04
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 20:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:00.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.