Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what does this "Segmentation fault (core dumped)" means ?

0 votes
asked Mar 15, 2019 by anonymous
Its merge sort program and after entering values in array in result it shows this error .whats does this error mean and how i can resolve it?

here is the code:

#include <iostream>
void merge(int a[],int start,int end);
void mergesort(int a[],int start,int mid,int end);
using namespace std;

int main()
{
    int a[30],i,n;
    cout<<"enter total number";
    cin>>n;
    cout<<"enter numbers"<<endl;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }merge(a,0,n);

    return 0;
}
void merge(int a[],int start,int end)
{
    if(end==start+1)
    {
        return;
    }
    int mid =(start+end)/2;
    merge(a,start,mid);
    merge(a,mid,end);
    mergesort(a,start,mid,end);
}

void mergesort(int a[],int start,int mid,int end)
{
    int i,j,temp[30],index=start;
    for(i=start;j=mid;((i<mid)||(j<end)))
    {
       if((i<mid)&&(j<end))
       {
           if(a[j]>a[i])
           {
               temp[index]=a[j];
               j++;
           }else
           temp[index]=a[i];
           i++;
       }
       else
       {
            if(i<mid)
            {
                temp[index]=a[i];
                i++;
            }else
            {
                temp[index]=a[j];
                j++;
            }
       }
        index++;
    }
    for(i=start;i<end;i++)
    {
        a[i]=temp[i];
        cout<<a[i]<<" ";
    }

}

1 Answer

0 votes
answered Mar 16, 2019 by Admin (5,100 points)

You for loop initialisation has an issue. 
It should have been 

for(i=start,j=mid;((i<mid)||(j<end));)


Here is fixed code: https://onlinegdb.com/BkJAwY9DN

Here is more details of segmentation fault. 
http://question.onlinegdb.com/18/what-is-segmentation-fault
 

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...