0

What is the error in this mergesort program in C??

// Online C compiler to run C program online #include <stdio.h> void mergesort(); void merge(); int main() { int n,i; printf("enter size of array"); scanf("%d",&n); int a[n]; printf("enter elements"); for(i=0;i<n;i++); scanf("%d",&a[i]); mergesort(a,0,n-1); } void mergesort(int a[],int first,int last) { int mid; if(first<last) { mid=(first+last)/2; mergesort(a,first,mid); mergesort(a,mid+1,last); merge(a,first,mid,last); } } void merge(int a[],int first,int mid,int last) { int b[50]; int i,j,k; i=first,j=mid+1,k=first; while(i<=mid&&j<=last) { if(a[i]<=a[j]) { b[k]=a[i]; k++,i++;} else{ b[k]=a[j]; k++,j++; } } if(i>mid) {while(j<=last) b[k]=a[j]; k++,j++;} else { while(i<=mid) b[k]=a[i]; k++,i++; } for(i=first;i<=last;i++) a[i]=b[i]; printf("sorted elements"); int n; for(i=0;i< n;i++) printf("%d",a[i]); }

6th Mar 2022, 3:06 PM
Diya
1 Antwort
0
You have mismatching function prototypes.. You have semicolon after a loop in main, remove it. And some of missing, mismatching braces {, } in merge function. Check again.. And Last 4 lines should be put in main at last instead of in merge() function. In that remove int n; declaration. It is Unitialized there and initialized in main. Try these changes and it works... Hope it helps...
6th Mar 2022, 3:32 PM
Jayakrishna 🇮🇳