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 23-Oct-2005, 22:45
sasuke101 sasuke101 is offline
New Member
 
Join Date: Sep 2005
Posts: 24
sasuke101 is on a distinguished road

help with Bubble sort program that uses vectors instead of arrays


Header file
CPP / C++ / C Code:
class Sort  
{
public:
void bubble_Sort();
Sort(unsigned int vectorSize );
~Sort();
void put();
void Swap(int *arrj, int *arrk);
Sort( );

private:
vector<int>	testData_;
   
};

my cpp file for the functions
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>

Sort::Sort( ) 
{
	testData_.resize( 0 );
}

Sort::Sort(unsigned int vectorSize )             
{
	

	 cout<<"Enter an array of 10, 100, 1000 to be randomly sorted"<< endl;
     cin>> size;
     
     size = unsigned int (vectorSize);               //dynamic memory allocation
	       
      (srand( (unsigned)time( NULL ) )); 
      
       testData_.resize( vectorSize );  
      for (int i=0;  i < vectorSize; i++)
      {
        testData_[i] = rand(); // pointers of the function srand
      }

}

Sort::~Sort()  // Destructor
{
	delete[] unsigned int (vectorSize);	                        //delete the allocated memory
}

//bubble sort function
void Sort::bubble_Sort()
{
	vector<int> output( testData_.size() );
	output = testData_;

                               
    for (int i=0; i< output.size(); i++)
        for(int j=0; j< output.size()-i-1; j++)
            if ( output[j] > output[j+1] ) 
			{
				Swap(&output[j] , &output[j+1] ) ;
			}
}
				
				
void Sort::Swap(int *outputj, int *outputk) // pointers
			{
                int tmp = *outputk;    //use this tmp for swapping       
                *outputk = *outputj;
                *outputj = tmp;
            }
{

testData_ = output;


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

this is my main cpp file
CPP / C++ / C Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "bub1.h"  // Header file

void main()
{

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

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

    a.bubble_Sort();        //sort arr

    cout<<"After sorting"<<endl;

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

}

my program needs to sort vectors instead of arrays but it seems to get errors need some help?
am i on the right track or far off?
  #2  
Old 23-Oct-2005, 23:34
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road

Re: help with Bubble sort program that uses vectors instead of arrays


Quote:
Originally Posted by sasuke101
My program needs to sort vectors instead of arrays but it seems to get errors need some help?
am i on the right track or far off?
Please be more explicit. There are thousands of errors available, you need to let us know which ones you've chosen with your code. Reread Guideline #2 please.
__________________

Please read http://www.gidforums.com/t-5566.html. They were written to help you create a request that is readable and has enough information we can actually tell what you need help with.
  #3  
Old 23-Oct-2005, 23:40
sasuke101 sasuke101 is offline
New Member
 
Join Date: Sep 2005
Posts: 24
sasuke101 is on a distinguished road

Re: help with Bubble sort program that uses vectors instead of arrays


bub1.cpp
c:\program files\microsoft visual studio\myprojects\new\bub1.h(17) : error C2143: syntax error : missing ';' before '<'
c:\program files\microsoft visual studio\myprojects\new\bub1.h(17) : error C2501: 'vector' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\new\bub1.h(17) : error C2059: syntax error : '<'
c:\program files\microsoft visual studio\myprojects\new\bub1.h(17) : error C2238: unexpected token(s) preceding ';'
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(12) : error C2065: 'testData_' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(12) : error C2228: left of '.resize' must have class/struct/union type
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(20) : error C2065: 'size' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(26) : error C2228: left of '.resize' must have class/struct/union type
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(29) : error C2109: subscript requires array or pointer type
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(29) : error C2106: '=' : left operand must be l-value
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(36) : error C2065: 'vectorSize' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(36) : error C2541: delete : cannot delete objects that are not pointers
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(42) : error C2065: 'vector' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(42) : error C2062: type 'int' unexpected
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(43) : error C2065: 'output' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(46) : error C2228: left of '.size' must have class/struct/union type
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(47) : error C2228: left of '.size' must have class/struct/union type
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(4 : error C2109: subscript requires array or pointer type
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(4 : error C2109: subscript requires array or pointer type
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(50) : error C2109: subscript requires array or pointer type
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(50) : error C2102: '&' requires l-value
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(50) : error C2109: subscript requires array or pointer type
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(50) : error C2102: '&' requires l-value
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(61) : error C2447: missing function header (old-style formal list?)
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(70) : error C2228: left of '.size' must have class/struct/union type
c:\program files\microsoft visual studio\myprojects\new\bub1.cpp(71) : error C2109: subscript requires array or pointer type
Error executing cl.exe.

bub1.obj - 26 error(s), 0 warning(s)
  #4  
Old 24-Oct-2005, 07:51
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 Bubble sort program that uses vectors instead of arrays


Read this tutorial in using vectors.
This will help you fix many of your errors:

Using Vectors

In your header file,
Instead of
CPP / C++ / C Code:
vector<int>testData_;

Use
CPP / C++ / C Code:
std::vector<int>testData_;   //why???

Next:
Remember the scope of variables.
If you use output in one function, it is not available in the other.

And,
Use clear() in vector to free up memory.
so instead of
CPP / C++ / C Code:
delete[] unsigned int (vectorSize);    //error... you cant delete unsigned int.

You can use this:
CPP / C++ / C Code:
testdata_.clear();

Use int main() instead of void main().
You can find why here....

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.
  #5  
Old 24-Oct-2005, 22:14
sasuke101 sasuke101 is offline
New Member
 
Join Date: Sep 2005
Posts: 24
sasuke101 is on a distinguished road

Re: help with Bubble sort program that uses vectors instead of arrays


bub1.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\new\bub1.cpp(37) : error C2065: 'testdata_' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\new\bub1.cpp(37) : error C2228: left of '.clear' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\new\bub1.cpp(69) : error C2228: left of '.size' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\new\bub1.cpp(70) : error C2109: subscript requires array or pointer type
Error executing cl.exe.

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

still having trouble with theses four errors
  #6  
Old 25-Oct-2005, 08:42
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 Bubble sort program that uses vectors instead of arrays


Hi sasuke,

Quote:
error C2065: 'testdata_' : undeclared identifier
It should be testData_ and not testdata_.

Please post the lines where you get the error message. We cant figure it out by just looking at the errors.

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.
  #7  
Old 25-Oct-2005, 11:12
sasuke101 sasuke101 is offline
New Member
 
Join Date: Sep 2005
Posts: 24
sasuke101 is on a distinguished road

Re: help with Bubble sort program that uses vectors instead of arrays


for (int i =0; i < output.size(); i++)
C:\Program Files\Microsoft Visual Studio\MyProjects\new\bub1.cpp(69) : error C2228: left of '.size' must have class/struct/union type


cout<<"\n"<<output[i]<< endl;
C:\Program Files\Microsoft Visual Studio\MyProjects\new\bub1.cpp(70) : error C2109: subscript requires array or pointer type



those are the two errors i have left
  #8  
Old 25-Oct-2005, 11:51
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: help with Bubble sort program that uses vectors instead of arrays


Quote:
Originally Posted by sasuke101
for (int i =0; i < output.size(); i++)
C:\Program Files\Microsoft Visual Studio\MyProjects\new\bub1.cpp(69) : error C2228: left of '.size' must have class/struct/union type
is output a class or a struct or a union? If not you can't use .size()


Quote:
Originally Posted by sasuke101
cout<<"\n"<<output[i]<< endl;
C:\Program Files\Microsoft Visual Studio\MyProjects\new\bub1.cpp(70) : error C2109: subscript requires array or pointer type
is output an array or a pointer? If not you can't use a subscript.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #9  
Old 25-Oct-2005, 11:53
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 Bubble sort program that uses vectors instead of arrays


I think the problem is here:
CPP / C++ / C Code:
void Sort::put()                            //prints the contents of arr
{
cout<<"\n"<< "Here are your  numbers"<< endl;  
     for (int i =0; i < output.size(); i++)
         cout<<"\n"<<output[i]<< endl;        
}

The answer is this::
Quote:
Originally Posted by Paramesh
Remember the scope of variables.
If you use output in one function, it is not available in the other.

and why do you use the output variable? Use the testData_ variable instead.

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.
  #10  
Old 25-Oct-2005, 12:26
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
Smile

Re: help with Bubble sort program that uses vectors instead of arrays


Hi Sasuke,

Even if you modify those things, your code is wrong in bubble sort functions and is confusing.
The code you have written for arrays will not be the same in the case of vectors.

Since you are unable to find an answer using vectors and your code is quite confusing, i'll modify the entire thing so that it remains simple.

Here we go:

First the header file:
Remember your previous post issues about the header file?

Here is the modified header file:
CPP / C++ / C Code:
#include <iostream>			//include the required header files only.
#include <ctime>			//include all the header files in bub1.h
#include <vector>			


#ifndef _BUBBLE_			//if _BUBBLE_ is not defined,
#define _BUBBLE_			//define it and compile everything else up to #endif

class Sort					
{
public:
	Sort( );				//constructor
	Sort( unsigned int vectorSize );	//constructor with arguments
	~Sort();				//destructor
	void bubble_Sort();		//function to bubble sort the values.
	void put();				//function to print the values.
	
private:
	std::vector<int> testData;		//vector testData
   
};

#endif

Just include the header files you require. Include all the precompiled header files in your program header file..
You already know what is #ifndef..etc in your previous thread.

Here are the modifications i did in the program:
Renamed the vector as testData only instead of testData_ to make coding easier.
Added #ifndef..etc to the code.
Removed your swap function. Added the modified swap code in the bubble_Sort function itself.

Now to the cpp file:
Removed the other cpp file used to define the functions.
I used the main cpp file instead as in the previous post to maintain simplicity.

Here is the constructor definition:
CPP / C++ / C Code:
Sort::Sort()			 //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
      
	 testData.resize( size );  //resize the testData

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

}
This is what this constructor do:
Get the input size.
Resize the vector.
Generate random numbers and store them in the vector.

You can add code for you constructor with argument if you want to.(i didnt do it);

Now to the destructor:
CPP / C++ / C Code:
Sort::~Sort()					 // Destructor
{
    testData.clear();                //Clear the vector contents
Just clear the vector contents.

Your bubble_Sort() function:
CPP / C++ / C Code:
//bubble sort function
void Sort::bubble_Sort()
{
	int temp;					//a temporary variable

    for (int i = 0; i < testData.size(); i++)

        for(int j = 0; j < testData.size()-i-1; j++)

            if ( testData[j] > testData[j+1] ) 
			{
				temp = testData[j];			//sort the data in the testData vector itself. this will simple.
				testData[j] = testData[j+1];
				testData[j+1] = temp;
			 }								
}

Didnt use a swap function. I entered the code here itself. Note how the vector is used here.

Your put() function:
CPP / C++ / C Code:
void Sort::put()                            //prints the contents of arr
{
	cout<< endl << "Here are your numbers"<< endl;  
    
	for (int i =0; i < testData.size(); i++)
        cout<< endl << testData[i] << endl;        //print the output.
}
Just note how to print the values. The same way as you use arrays.

Your main function:
CPP / C++ / C Code:
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.bubble_Sort();        //sort arr

    cout<<"After sorting"<<endl;

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

	return 0;

}
Use int main instead of void main.

Please read the comments for more information about the program.

So, summarising, we have the main.cpp file as:
CPP / C++ / C Code:
#include "bub1.h"		//Include Header file

using namespace std;  

Sort::Sort()			 //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
      
	 testData.resize( size );  //resize the testData

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

}


Sort::~Sort()					 // Destructor
{
testData.clear();                //delete the allocated memory
}

//bubble sort function
void Sort::bubble_Sort()
{
	int temp;					//a temporary variable

    for (int i = 0; i < testData.size(); i++)

        for(int j = 0; j < testData.size()-i-1; j++)

            if ( testData[j] > testData[j+1] ) 
			{
				temp = testData[j];			//sort the data in the testData vector itself. this will simple.
				testData[j] = testData[j+1];
				testData[j+1] = temp;
			 }								
}


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

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.bubble_Sort();        //sort arr

    cout<<"After sorting"<<endl;

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

	return 0;

}

Just include the bub1.h file.
Define the class functions.

Please dont take it for granted.
I have done this for you only to make you understand.
If you have any doubts, please clarify.

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.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

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
need help with bubble sort for c++ sasuke101 C++ Forum 2 21-Sep-2005 21:12
Type casts ? kai85 C++ Forum 12 23-Jun-2005 13:04
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 19:35
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 09:48
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10

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

All times are GMT -6. The time now is 04:59.


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