Reputation: 175
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?
#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]);
}
}
#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
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
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