Loop to read array input unexpected behavior?

69 Views Asked by At

I wrote a recursive merge sort program , but it seems to get stuck whilst reading the array input itself(loop never terminates), I am not a pro when it comes to C ,I know it is some simple embarrassing mistake ,after hours no still no good , Please help:

//merge sort
#include<stdio.h>
#include<stdlib.h>

int a[20];

void mergeSort(int,int);
void merge(int,int,int);

void main()
{
//int a[20];
int n=0;
int i=0;
int x=0;

printf("Enter n");
scanf("%d",&n);

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

printf("Loop is over");

int low=0;
int high=n;

//start the sorting
mergeSort(low,high);

//done, print the result
//
for(i=0;i<n;i++)
{
printf("  %d",a[i]);
}

//return 0;
}//main ends

void mergeSort(int low,int high)
{
    while(low<high)
    {
    //split
    int mid=(low+high)/2;
    mergeSort(low,mid);
    mergeSort(mid+1,high);
    merge(low,mid,high);
    }
}//mergeSort ends


void merge(int low,int mid,int high)
{
//two lists , sort them
int b[50];

int i,k,h,j;//a[],b[],h->low,j->mid+1
i=0;k=0;h=low;j=mid+1;

while(h<=mid && j<high)
{
    if(a[h]<a[j]) 
    {
    b[i]=a[h];
    h++;
    }
    else
    {
    b[i]=a[j];
    j++;
    }

    i++;
}

if(h>mid)//left has emptied, copy whats left in right
{
    for(k=j;k<high;k++)
    {
    b[i]=a[k];
    i++;
    }
}

else//right has emptied
{
    for(k=h;k<=mid;k++)
    {
    b[i]=a[k];
    i++;
    }

}
//copy b to a

for(k=0;k<high;k++)
{
a[k]=b[k];
}


}//merge ends
3

There are 3 best solutions below

1
On

Add the below checks and you should be able to figure out

if(scanf("%d",&n) != 1)
{
  printf("Enter a valid integer\n");
  return 1;
}

if(n<0 || n>20)
{
  printf("Array range is 1 to 20");
  return 1;
}

for(i=0;i<n;i++)
{
  if(scanf("%d",&a[i]) !=1)
  printf("Scan failed\n");
}
6
On
#include <stdio.h>
#include <stdlib.h>



void mergeSort(int,int);
void merge(int,int,int);

int a[20];

int  main()
{

int n;
int i;
int x=0;

printf("Enter n: ");
scanf("%d",&n); //this is working fine don't give n value more than or equal to 20 as you have declared an global array of size 20 

    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
        printf("%d\n",i);
    }
printf("Loop is over\n");

//But after this the programme is not correct it is going in infinite time so above this all is fine but below the programme of merge sort is not correct 

I hope the below link will help you :- http://geeksquiz.com/merge-sort/

0
On
#include<stdio.h>

int a[20];

void mergeSort(int low, int high);//[low .. high-1]
void merge(int low, int mid, int high);

int  main(void){
    int i, n=0;

    printf("Enter n:");
    scanf("%d", &n);

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

    printf("Loop is over\n");

    int low=0;
    int high=n;

    mergeSort(low, high);

    for(i=0;i<n;i++)
        printf(" %d", a[i]);
    printf("\n");

    return 0;
}

void mergeSort(int low,int high){
    if(1 < high - low){
        int mid = (low+high)/2;
        mergeSort(low, mid);
        mergeSort(mid, high);
        merge(low, mid, high);
    }
}

void merge(int low, int mid, int high){
    int b[20];
    int i = 0, k, h = low, j = mid;

    while(h<mid && j<high){
        if(a[h]<a[j])
            b[i++]=a[h++];
        else
            b[i++]=a[j++];
    }
    if(h<mid)
        b[i++]=a[h++];
    if(j<high)
        b[i++]=a[j++];

    for(i=0, k=low; k<high; ++k, ++i){
        a[k]=b[i];
    }
}