1

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;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
dawa
  • 15
  • 4
  • 2
    Whenever you gt such an error, and you know that you have defined the function, take a close look at the function definition and compare it to the declaration, maybe copy the declaration and put it just above the definition so you could look at both at the same time. Doint that will immediately reveal any differences. – Some programmer dude Feb 17 '16 at 17:40
  • This is one of the reasons I try to always define a function before its uses, and avoid pre-declarations. A separate declaration is a form of code duplication which has to be kept in sync with the definition. Don't Repeat Yourself! – Rob K Feb 17 '16 at 18:49
  • http://stackoverflow.com/a/12574403/3747990 – Niall Feb 18 '16 at 06:24

3 Answers3

1

You are declaring a

void showarray(int [],int );

but defining a

void showarray(const float array[], int size)

Both are not the same. At the point the compiler tries to compile the function call, he only knows about the first one and uses this. But the linker can only find the second one, which is why he produces an error.

Anedar
  • 4,235
  • 1
  • 23
  • 41
1

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;
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Your function declaration needs to match your function definition:

void showarray(int [],int );

And

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

}

Need to have matching function signatures.

shafeen
  • 2,431
  • 18
  • 23