mahin hossen
mahin hossen

Reputation: 175

sorting with bubble sort using 2 function

At first i used bubble sort to sort some numbers.It worked.And then again i tried to sort using bubble sort with 2 function with same logic.but it is not working.I am new to work with 2/3 function.So can anyone help me to find the problem in my logic?

First source

#include<stdio.h>
main()
{
    int i,j;
    float num[5], c;

    for(i=0;i<5;i++)
    {
        scanf("%f",&num[i]);
    }
    for(j=0;j<4;j++)
    {
        for(i=0;i<5;i++)
        {
            if(num[i]<num[i+1])
            {
                c=num[i];
                num[i]=num[i+1];
                num[i+1]=c;
            }
        }
    }
    for(i=0;i<5;i++)
    {
        printf("%.2f\n",num[i]);
    }
}

Second source w/function

#include<stdio.h>
void sort(int a[]);
void main()
{
    int i;
    double a[3], A, B, C;

    for(i=0;i<3;i++)
    {
        scanf("%lf",&a[i]);
    }

    sort(a);

    printf("In The Function\n");
    for(i=0;i<3;i++)
    {
        printf("%.2lf\n",a[i]);
    }

    return;
}

void sort(int a[])
{
    int i, n;
    double temp;

    for(n=0;n<2;n++)
    {
        for(i=0;i<3;i++)
        {
            if(a[i]<a[i+1])
            {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
    }
    return;
}

Upvotes: 0

Views: 165

Answers (2)

Mohit Kumar Raj Badi
Mohit Kumar Raj Badi

Reputation: 14

You are passing the double

double a;
sort(a);

value in the main function but at the function declaration part your datatype is integer

void sort(int a[])

correct this to

void sort(double a[])

Upvotes: 0

kiran Biradar
kiran Biradar

Reputation: 12742

Do not ignore the warnings.

warning: passing argument 1 of ‘sort’ from incompatible pointer type [enabled by default]
     sort(a);
     ^
sort.c:2:6: note: expected ‘int *’ but argument is of type ‘double *’
 void sort(int a[]);
  ^

You have declared a as double but your function receives a as int. Change it to as below.

void sort(double a[]);

Upvotes: 1

Related Questions