0

I am at pset3 and I decided to try creating my own mergeSort header file. This is something that I have never attempted and haven't learned much about yet so I am at a loss. I have created the the mergesort.c file but I get the following error when trying to compile it:

~/pset3/plurality/ $ make mergesort clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow mergesort.c -lcrypt -lcs50 -lm -o mergesort /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../x86_64-linux-gnu/crt1.o: In function _start': (.text+0x20): undefined reference to main' clang-7: error: linker command failed with exit code 1 (use -v to see invocation) : recipe for target 'mergesort' failed make: *** [mergesort] Error 1

Here is my code:

#include <stdio.h>
#include <stdlib.h>

void merge(int *a, int s, int e);

void mergeSort(int a[], int s, int e)
{
    //Base case - only 1 or 0 elements
    if(s>=e)
    {
        return;
    }

    //otherwise follw these 3 steps
    //first, divide the array
    int mid = (s+e)/2; //This is the midpoint

    //Next, recursively sort the arrays - s,mid and mid+1,e
    mergeSort(a, s, mid);
    mergeSort(a, mid+1, e);

    //Finally, merge the two parts

    merge(a, s, e);

    return;
}

//Merge function
void merge(int *a, int s, int e)
{
    int mid = (s+e)/2;
    int i = s;
    int j = mid+1;
    int k = s;

    //intialize a temporary array
    int temp[100];

    //compare current int from i array to current int from j array
    while(i<=mid && j<=e)
    {
        //if i is lower, print it in temp array
        if(a[i] < a[j])
        {
            temp[k++] = a[i++];
        }
        //if j is lower print it in temp array
        else
        {
            temp[k++] = a[j++];
        }
    }
    //enter remaining i's in temp array if j's are done
    while (i<=mid)
    {
        temp[k++] = a[i++];
    }
    //enter remaining j's in temp array if i's are done
    while (j<=e)
    {
        temp[k++] = a[j++];
    }

    //we need to then copy all elements back to original array
    for(int x=s; x<=e; x++)
    {
        a[x] = temp[x];
    }
    
    return;
}

You must log in to answer this question.

Browse other questions tagged .