Search code examples
c++arraysfunctionbubble-sortunresolved-external

Unresolved external symbol error when compiling


when ever am trying to compile it, i get an error message

 unresolved external symbol "void __cdecl showarray(int * const,int)"

am not sure what i am doing wrong? what should I do, my code looks like this

#include<iostream>
#include<string>

using namespace std;


void sortArray(int [], int );
void showarray(int [],int );

int main(){
    int endtime[10] = { 70, 38, 8, 101, 11, 127, 313, 14, 16, 127 };

    cout << "unsorted" << endl;
    showarray(endtime, 10);

    sortArray(endtime, 10);

    cout << "the sorted value are :" << endl;
    showarray(endtime, 10);

    return 0;
}

void sortArray(int array[], int size){
    bool swap;
    int temp;

    do{
        swap = false;
        for (int count = 0; count < (size - 1); count++){
            if (array[count] > array[count+1]){
                temp = array[count];
                array[count] = array[count + 1];
                array[count + 1] = temp;
                swap = true;
            }
        }
    } while (swap);
}

void showarray(const float array[], int size){
    for (int i = 0; i < size; i++)
        cout << array[i] <<" "<< endl;
}

Solution

  • At first you declared function

    void showarray(int [],int );
    

    but defined function

    void showarray(const float array[], int size)
    {
        //...
    }
    

    Change the both declaration and the definition like

    void showArray( const int [], int );
         ^^^^^^^^^  ^^^^^^^^^^^^
    

    For example (the function definition)

    void showArray( const int array[], int size )
    {
        for ( int i = 0; i < size; i++ )
        {
            cout << array[i] << ' ';
        }
        cout << endl;
    }
    

    And do not forget to change function calls using name showArray.

    Take into account that the variable temp should be declared inside the if statement. Also you could use standard function std::swap as for example

    std::swap( array[count], array[count+1] );
    

    The program can look like

    #include <iostream>
    
    void sortArray( int a[], size_t n )
    {
        while ( !( n < 2 ) )
        {
            size_t last = 0;
            for ( size_t i = 1; i < n; i++ )
            {
                if ( a[i] < a[i-1] )
                {
                    int temp = a[i];
                    a[i] = a[i-1];
                    a[i-1] = temp;
                    last = i;
                }
            }
            n = last;
        }
    }
    
    void showArray( const int a[], size_t n )
    {
        for ( size_t i = 0; i < n; i++ ) std::cout << a[i] << ' ';
        std::cout << std::endl;
    }
    
    int main()
    {
        const size_t N = 10;
        int endtime[N] = { 70, 38, 8, 101, 11, 127, 313, 14, 16, 127 };
    
        std::cout << "unsorted: ";
        showArray( endtime, N );
    
        sortArray( endtime, N );
    
        std::cout << "  sorted: ";
        showArray( endtime, N );
    }        
    

    Its output is

    unsorted: 70 38 8 101 11 127 313 14 16 127 
      sorted: 8 11 14 16 38 70 101 127 127 313 
    

    With using standard function std::swap the function sortArray can be more compact and clear

    void sortArray( int a[], size_t n )
    {
        while ( not ( n < 2 ) )
        {
            size_t last = 0;
            for ( size_t i = 1; i < n; i++ )
            {
                if ( a[i] < a[i-1] )
                {
                    std::swap( a[i], a[i-1] );
                    last = i;
                }
            }
            n = last;
        }
    }