Using CodeLite IDE on a Mac to brush up my C++. It's a common practice to write functions in a different cpp file than the one that has the main(). I have two .cpp files, main.cpp and InsertionSort.cpp. The latter has the sorting function and the printing function.
The main is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
int unsorted[6] = {5,2,4,6,1,3};
int size = 6;
insertionSort(unsorted, size);
printArray(unsorted, size);
}
and the other insertionSort.cpp is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
void printArray(int*,int);
void insertionSort ( int array[], int size)
{
int value = 0; //temp variable to store the value of the element being moved
int holeIndex = 0; // index variable to keep track of the vacant slot
for (int i = 1; i <= size ; i++)
{
value = array[i];
holeIndex = i;
while(holeIndex > 0 && value < array[holeIndex-1])
{
array[holeIndex] = array[holeIndex -1];
holeIndex = holeIndex -1 ;
}
array[holeIndex] = value;
printArray(array,size);
}
//return array;
}
void printArray(int array[], int size)
{
for (int i = 0; i<size; i++ )
{
cout << array[i]<<" ";
}
cout << endl;
}
the problem i'm facing is that i get an undeclared identifier 'insertionSort' and undeclared identifier 'printArray' while i run this. It seems to be an import issue, but i can't figure it out in CodeLite. Also, any general rule of thumb to follow to import the other source .cpp files so I don't run into this noob problem again ? Any help would be greatly appreciated. Thanks in advance.
any include i'm missing?