Link to home
Start Free TrialLog in
Avatar of MexicanHeat
MexicanHeat

asked on

Stupid Question: Easy 20 Points

Okay, im a huge retard and in my incredible tiredness cannot figure out why this wont work....

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

class Sorts
{

        public:

        void bubbleSort(int a[], int length)
        {
                int top, i, temp;

                for (top = 0; top < length - 1; top++)
                {
                        for (i = length - 1; i > top; i--)
                        {
                                if (a[i] < a[i-1])
                                {
                                        temp = a[i];
                                        a[i] = a[i-1];
                                        a[i-1] = temp;
                                }
                        }
                }
        }
};


#include <stdlib.h>
#include <iostream.h>
#include "Sorts.cpp"

main()
{
        int a[] = {1,9,3,5,2,8,6};

        for (int x = 0; x <= 6; x++)
        {
                cout << a[x];
        }

        bubbleSort (a , 7);
}

The problem is when i try to compile my driver. I get an error that says:
"Driver.cpp:14: implicit declaration of function `int bubbleSort(...)'"

Thanks for the help.
Avatar of pb_india
pb_india

Try rearranging:

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


to

#include <iostream.h>
#include <stdlib.h>
ASKER CERTIFIED SOLUTION
Avatar of pb_india
pb_india

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You dont need to put bubbleSort in a class. Compile it this way:

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

void bubbleSort(int a[], int length)
{
       int top, i, temp;

       for (top = 0; top < length - 1; top++)
       {
            for (i = length - 1; i > top; i--)
            {
                   if (a[i] < a[i-1])
                   {
                         temp = a[i];
                         a[i] = a[i-1];
                          a[i-1] = temp;
                    }
             }
       }
}


#include <stdlib.h>
#include <iostream.h>
#include "Sorts.cpp"

main()
{
        int a[] = {1,9,3,5,2,8,6};

        for (int x = 0; x <= 6; x++)
        {
                cout << a[x];
        }

        bubbleSort (a , 7);
}
Avatar of MexicanHeat

ASKER

Like i said....im stupid and im tired. Thanks pb. I appreciate it.
Yep, thanks for that flippy. Whats gonna end up happening is that im going to be testing all types of sorts and the code in my driver is going to get kind of long so i figured it would be easier to seperate the sorts. thanks for the help guys.
I dont really see any point in putting bubble sort in a class (maybe you are used to Java?). Classes are for when you spawn multiple instances of a type of objects. Classes makes handling compex data types easy by automating some things, and encapsulating them nicely. Bubble sort is simply a function you perform on a list.
You hit it right on the head flippy. I've only ever done Java...and all of a sudden my class is requiring us to program in C++. I am completely lost in this language and am having a hell of a time figuring out how to do things. I do appreciate the help flippy.