Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm coding a program to use selection sort in C program using functions and arrays but I'm getting error as undefined reference to swap

What I have tried:

C
#include <stdio.h>
#include <stdlib.h>
void sorting();
void swap(int,int);

int main()
{
    int array1[5];
    int i;
    int j;
    printf("enter your elements:\n");
    for(i=0;i<5;i++)
    {
        scanf("%d",&array1[i]);
    }
    sorting(array1);
 return 0;
}

void sorting( int *array2)
{

    int i=0;
    for (i=0;i<5;i++)
    {
        for (int j=i+1;j<5;j++)
          if(array2[i]>array2[j])
        {
            void swap(&array2[i],&array2[j]);
        }
    }

    void swap(int*a,int*b)
    {
        int temp;
        temp=*a;
         *a= *b;
        *b=temp;

    }
    printf("\n");
    printf("Array in sorted order is");
    for(int i=0;i<5;i++)
    {
        printf("%d ",array2[i]);
    }
}
Posted
Updated 27-Oct-21 1:22am
v3
Comments
CHill60 27-Oct-21 4:54am    
Compare the declare void swap(int,int); to the implementation void swap(int*a,int*b)
Nithish Karthikeyan 27-Oct-21 5:05am    
how do I do that?
Richard MacCutchan 27-Oct-21 5:26am    
Change the definition at line 4 to:
void swap(int*, int*);

so it matches the actual function.
CHill60 27-Oct-21 6:17am    
I meant "look at the difference" - in other words what are the differences between those two lines - Richard and OriginalGriff have told you how to fix it, but just look at those two lines! One has * in it and the other does not.

Function prototypes such as
C++
void sorting();
and
C++
void swap(int,int);
must have an identical signature to the function when it is actually declared: if it doesn't, then the two do not "match up" and the prototyped function is technically not defined at all.
Compare the prototype definitions with the actually bodied versions:
C++
void sorting( int *array2)
{
...
}
void swap(int*a,int*b)
{
...
}
And you can see why they do not match.

In addition, C does not support "functions within functions" so you need to move the declaration of the swap function outside the body of the sorting function.
 
Share this answer
 
Comments
CPallini 27-Oct-21 6:49am    
5.
Try
C
#include <stdio.h>
#include <stdlib.h>

void sort(int array[], size_t len);
void swap(int * pa, int * pb);


enum { SIZE = 5 };

int main()
{
    int arr[SIZE];

    printf("enter your elements:\n");
    for(int i = 0; i < SIZE; ++i)
    {
       if (  scanf("%d",&arr[i]) != 1 )
        return -1;
    }

    sort(arr, SIZE);


    printf("sorted array:\n");

    for (int i = 0; i < SIZE; ++i)
    {
      printf("%d\n", arr[i]);
    }


 return 0;
}

void sort( int arr[], size_t len)
{
    for (int i=0; i <(len-1); ++i)
    {
        for (int j=i+1;j<len; ++j)
        {
            if( arr[i] > arr[j] )
            {
              swap(&arr[i],&arr[j]);
            }
        }
    }
}

void swap(int * pa,int *pb)
{
    int temp;
    temp=*pa;
    *pa= *pb;
    *pb=temp;
}
 
Share this answer
 
Quote:
I'm coding a program to use selection sort in C program using functions and arrays but I'm getting error as undefined reference to swap

Reason is that in C you can't define a function inside another function.
C++
#include <stdio.h>
#include <stdlib.h>
void sorting();
void swap(int,int);

int main()
{
    int array1[5];
    int i;
    int j;
    printf("enter your elements:\n");
    for(i=0;i<5;i++)
    {
        scanf("%d",&array1[i]);
    }
    sorting(array1);
 return 0;
}

void sorting( int *array2)
{

    int i=0;
    for (i=0;i<5;i++)
    {
        for (int j=i+1;j<5;j++)
          if(array2[i]>array2[j])
        {
            void swap(&array2[i],&array2[j]);
        }
    }

    printf("\n");
    printf("Array in sorted order is");
    for(int i=0;i<5;i++)
    {
        printf("%d ",array2[i]);
    }
}

void swap(int*a,int*b)
{
    int temp;
    temp=*a;
     *a= *b;
    *b=temp;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900