**IT works correctly as one file in a C++ shell but when I separate it into the findMedian file and Main file I get the compile error. **
Here are the 2 files I'm trying to compile together:
This is the error I'm getting:
main2.cpp:(.text+0x0): multiple definition of `sort(int*, int)'
/tmp/ccwMBuaH.o:findMedian.cpp:(.text+0x0): first defined here
/tmp/ccDTJnz4.o: In function `printMedian(int*, int)':
main2.cpp:(.text+0xd5): multiple definition of `printMedian(int*, int)'
/tmp/ccwMBuaH.o:findMedian.cpp:(.text+0xd5): first defined here
/tmp/ccDTJnz4.o: In function `findMedian(int*, int)':
main2.cpp:(.text+0x12a): multiple definition of `findMedian(int*, int)'
/tmp/ccwMBuaH.o:findMedian.cpp:(.text+0x12a): first defined here
collect2: error: ld returned 1 exit status
FILE 1:
findMedian.CPP
#include <iostream>
#include <algorithm>
using namespace std;
//sort() function to sort the array.
void sort(int* arrayPtr, int size)
{
int i, j;
//Start the nested for loop to sort.
for (i = 0;i<size;i++)
for (j = i + 1;j<size;j++)
//Comparison to sort.
if (*(arrayPtr + j)<*(arrayPtr + i))
{
int temp = *(arrayPtr + j);
*(arrayPtr + j) = *(arrayPtr + i);
*(arrayPtr + i) = temp;
}
}
//printMedian() function to print the sorted array.
void printMedian(int array[], int size)
{
for (int i = 0; i < size; i++)
cout << array[i] << ' ';
}
//findMedian() function to find the median.
int findMedian(int array[], int size)
{
int median, mid;
//Find the mid element.
mid = size / 2;
//COMMENT:print out the value of mid
//Array size is even.
if (size % 2 == 0)
{
//Calculate the median.
median =(array[mid] + array[mid - 1]) / 2;
}
//Array size is odd.
else
{
//Calculate the median.
median = (array[mid]);
}
return median;
}
FILE 2 MAIN:
#include "findMedian.cpp"
//main() function
int main()
{
//Initialize an array
int array[] = { 20, 2, 8, 0, 0, 50, 1, 2, 40, 20, 100, };
//make the arrayPtr to an array.
int* arrayPtr = array;
//Initialize the size of the array
int size = 11;
//Declare the variable to store the median.
int median;
//Call the function sort() to sort the array.
sort(arrayPtr, size);
//Call the function printMedian() to print.
printMedian(array, size);
//Call the function findMedian() to find the median.
median = findMedian(array, size);
//Print the result.
cout << endl << "Median is:" << median << endl;
system("pause");
return 0;
}