Question

How can I properly define a function in C++ in one file and call it from another without using header files? My I would use a header file but my professor told us not to. I keep having many compiling issues with my files, dealing with my function not existing. Any help is appreciated. My program is supposed to sort an array in ascending order using selection sort and descending order using bubble sort.

Here's what I have so far. Here's my driver.

Driver.cpp

#include "selection.cpp"
#include "bubble.cpp"

#define ArraySize 10 //size of the array
#define  Seed  1 //seed used to generate random number
int values[ArraySize];

int main(int argc, char** argv) {

    int i;



    //seed random number generator
    srand(Seed);

    //Fill array with random integers
    for(i=0;i<ArraySize;i++)
        values[i] = rand();

    cout << "\n Numbers in array." << endl;

    for(i=0;i<ArraySize; i++)
        cout << &values[i]<< "\n";

    int* array_p[] = values[]; 

    //Function call for BubbleSort
    bubblesort(array_p[], ArraySize);  

    for (i=0;i<ArraySize; i++)
        cout << &values[i] << "\n";


 //SelectionSort
    selectionsort(array_p, ArraySize);

    cout << "Numbers in ascending order." << endl;

    for (i=0;i<ArraySize; i++)
        cout << &values[i] << "\n";
    return 0;
}

bubble.cpp

#include <iostream>


int* bubblesort(int values[], int size) {

    int i, j;

    for(i=0;i<size-1;i++){
        for(j=0; j<size-1; j++){
             if(values[j+1] > values[j]){
                int temp = values[j];
                values[j] = values[j+1];
                values[j+1] = temp;

                 return values;

             }
        }
    }
};

selection.cpp

#include <iostream>



int *selectionsort(int values[], int size){


    for(int i=0; i<size-1; i++){
        for(int j=0; j<size; j++){
            if(values[i] < values[j]){
                int temp = values[i];
                values[i] = values[j];
                values[j] = temp;


                return values;
            }
        }

    }
};
Was it helpful?

Solution

Just write

int* bubblesort(int values[], int size);

in the other source files that you want to call that function (before you call it).

Note that if you are not using header files, then you have to manually take care that if you change the return type or parameter list in one file, you make the same change in all files.

A few other things:

You might want to consider moving your return statement to the end of the function,instead of returning the instant you make the first swap. Or even better, have the functions return void - the caller already knows values because he just called the function, so it achieves nothing to return it again.

cout << &values[i]<< "\n"; outputs the address of each value, I guess you wanted to output the values instead.

int* array_p[] = values[]; and the line following it are syntax errors, I think you meant: int *array_p = values; bubblesort(array_p, ArraySize);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top