Given main:

#include <iostream>
using namespace std;
#include <stdlib.h>

#include "minivector.h"
#include "bubbleSort.h"
#include "Fraction.h"

int main()
{
    int arr[]={99, 12, 50, 80, 1, 79, 20};

    cout << endl << "bubbleSort ints" << endl;
    bubbleSort(arr, 7);
    for (int i=0; i < 7; i++)
        cout << arr[i]<< endl;

    double arr2[]={99.2, 12.9, 50.5, 80.7, 1.8, 79.0, 20.3};

    cout << endl<< "bubbleSort doubles" << endl;
    bubbleSort(arr2, 7);
    for (int i=0; i < 7; i++)
        cout << arr2[i]<< endl;

    
    Fraction arr3[]={Fraction(1,2), Fraction(1,3), Fraction(1,4),
                     Fraction(5,6), Fraction(3,4), Fraction(7,3),
                     Fraction(9,4)};

    cout << endl<< "bubbleSort Fractions" << endl;
    bubbleSort(arr3, 7);
    for (int i=0; i < 7; i++)
        cout << arr3[i]<< endl;

    cout << endl<< endl<< "miniVector sutff"<< endl;
    miniVector<Fraction> varr;

    varr.push_back(Fraction(12,14));
    varr.push_back(Fraction(5,7));
    varr.push_back(Fraction(2,5));    
    varr.push_back(Fraction(1,6));   
    varr.push_back(Fraction(3,7));

    bubbleSort(varr);

    for (int i=0; i < varr.size(); i++)
        cout << varr[i] << endl;
    return 0;
}

Given miniVector.h:

#ifndef MINI_VECTOR
#define MINI_VECTOR

template <typename T>
class miniVector
{
    public:
        miniVector(int size = 0);
            // constructor.
            // Postconditions: allocates array with size number of elements
            // and capacity. elements are initialized to T(), the default
            // value for type T

        miniVector(const miniVector<T>& obj);
            // copy constructor
            // Postcondition: creates current vector as a copy of obj

        ~miniVector();
            // destructor
            // Postcondition: the dynamic array is destroyed

        miniVector& operator= (const miniVector<T>& rhs);
            // assignment operator.
            // Postcondition: current vector holds the same data
            // as rhs

        T& back();
            // return the element at the rear of the vector.
            // Precondition: the vector is not empty. if vector
            // is empty, throws the underflowError exception

        
        T& operator[] (int i);
            // provides general access to elements using an index.
            // Precondition: 0 <= i < vSize. if the index is out
            // of range, throws the indexRangeError exception

        void push_back(const T& item);
            // insert item at the rear of the vector.
            // Postcondition: the vector size is increased by 1

        int size() const;
            // return current list size

        bool empty() const;
            // return true if vector is empty and false otherwise

        int capacity() const;
            // return the current capacity of the vector

   private:
        int vCapacity;        // amount of available space
        int vSize;            // number of elements in the list
        T *vArr;                // the dynamic array

        void reserve(int n);
            // called by public functions only if n > vCapacity. expands
            // the vector capacity to n elements, copies the existing
            // elements to the new space
};


#endif   // MINI_VECTOR

I need to create a bubbleSort.h and Fraction.h

So far, everything I've tried is garbage! lol

My attempt at somewhat of a Fraction.h:

class Fraction
{
		int num;
		int denom;
	public:
		Fraction(): num(1), denom(1)
		{}
	  
		Fraction(int n, int d): num(n), denom(d)
		{}

		Fraction add(const Fraction & op) const;  
		Fraction subtract(const Fraction & op) const;  
		Fraction multiply(const Fraction & op) const;  
		Fraction divide(const Fraction & op) const;
		bool operator < (Fraction &f);
	  
		void reduce();
		void display(const char * label) const;
		void readin(const char * label);

		friend ostream& operator << (ostream &s, Fraction &f);
	 
};

And my attempt at bubbleSort.h:

//bubbleSort header for HW

template <typename T>
class bubbleSort
{
    public:
		//bubbleSort(int &arr, int num); //DO I NEED THIS ALSO?!?
		bubbleSort(miniVector<T> &arr, int []);
};




template <typename T>
void bubbleSort(miniVector<T> &arr, int [])
{
	int smallIndex; // index of smallest element in the sublist
	int pass, j;
	T temp;

	int n = arr.size();

	// pass has the range 0 to n-2
	for (pass = 0; pass < n-1; pass++)
	{
        // scan the sublist starting at index pass
        smallIndex = pass;

        // j traverses the sublist arr[pass+1] to arr[n-1]
        for (j = pass+1; j < n; j++)
         // update if smaller element found
            if (arr[j] < arr[smallIndex])
                smallIndex = j;

        // if smallIndex and pass are not the same location,
        // exchange the smallest item in the sublist with arr[pass]
        if (smallIndex != pass)
        {
            temp = arr[pass];
            arr[pass] = arr[smallIndex];
            arr[smallIndex] = temp;
        }
    }
}

HELP PLEASE!!?

Recommended Answers

All 12 Replies

Could someone at least comment on my bubbleSort.h?? Is it even close? Do I need something completely different? Any ideas?!?

Even experienced people are a little intimidated when they see a lot of class code or the like. It isn't a simple question. You are asking for someone to take a little while to completely analyze your code, rather than deal with a specific question.

Your bubble sort looks fine from just glancing over it, but what is that int[] argument? It has no name and is not used.

Did you try compiling this? Did you try using it? Do those things and you'll get some instant feedback on exactly what works and what doesn't.

Good luck.

[EDIT] Oh yeah, that bubbleSort( int&, int ) makes no sense. A single integer is always sorted. If it were bubbleSort( int[], int ) then I would venture that your teacher wants you to write it without the template, then convert it to a template: bubbleSort( miniVector<T> & ).

Ok, well thanks for the response. I'll try and make my question more direct, now that my code is out there to look at....

The bubbleSort(int&, int) was me trying to get it to work before template, but it was makin me more confused. So ignore that.
But now if I have "bubbleSort(miniVector<T> &arr)" and then when it's called in my main with bubbleSort(arr, 7) i'm using two args there, but my func is only set to take one? I'm not sure what I need to do..unless I do (miniVector<T> &arr, int num) or somethin like that?

I also get this error, but it points to the end of my bubbleSort code when I click it to see where the problem is at.

error C2904: 'bubbleSort' : name already used for a template in the current scope

What does that mean??

Just for the record, the sort you're trying to implement is a Selection sort, not Bubble.

Hah, ok good to know! :) Any ideas on why the error tho?

Ok, this bubblesort is ACTUALLY a bubblesort...correct?

Well I'm still getting error:

error C2780: 'void bubbleSort(miniVector<T> &)' : expects 1 arguments - 2 provided

because when it gets called from the main "bubbleSort(arr, 7)" there's obviously 2 args there. How do i change my bubbleSort func to accept these 2 args correctly?!?

I tried adding (..., int n) and (..., int []) and even (..., int<T> n), but then it kept telling me that it could not deduce it. What am I doing wrong!??

bubblesort:

template <typename T>
void bubbleSort(miniVector<T> &arr)
{
    int i, j;
    T temp;
	int arrSize = arr.size();
     
	for(i=1; (i <= arrSize); i++)
	{
          for (j=0; j < (arrSize -1); j++)
         {
               if (arr[j+1] > arr[j])   
              { 
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
               }
          }
     }
}

On top of fixing that error..

Do I need to make bubbleSort a class??

What is the "7" for when you call the function in main()? Just call it without the "7". (I don't know why it complained about (..., int n), as that should have made the compiler change its complaint to "argument 'n' not used in function bubbleSort()".)

You might want to read up a little on templates. Here's the reading at the C++ FAQ Lite. There is a difference between a template class and a template function.

However, since your sort only works on miniVectors, it might be a good idea to make the sort function a member of the miniVector template class template <typename T> void miniVector<T>::bubbleSort() It really is a selection sort. A selection sort looks ahead for the smallest next element. A bubble sort only concerns itself with adjacent elements. The Wikipedia generally has good articles on such things.

Hope this helps.

The best tip I can think of with regard to writing templated code, is to write a non-template version first. Then you can be sure that your program is correct for one type (such as int), and that you're happy with the design of your class(es). Afterwards, create a generic version of the code to work with any type, where, hopefully, the only problems you'll run into are simple syntax errors from the compiler.

Ok thanks for the input guys. Back to the drawing board I suppose...

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.