-1

so im new to c and im trying to write a function that finds the median of an array then display it. I am getting error that say id returned 1 exit status and undefined reference to display median. I tried to look them up but none of the post applied to this. Can someone take a look and tell me whats wrong?(there is more to this code but i just posted what part im getting errors at)

   #include <stdio.h>
  #define MAX 10

 void getdata(int array[]);
   int displaylargest(int array[]);
   int displaysmallest(int array[]);
   int displayaverage(int array[]);
   int displayrange(int array[]);
   int displaymedian(int array[]);

    void displaydata(int array[]);





    int main ()
      {

       int array[MAX];
       int largest;
       int smallest;
       int average;
       int range;
       int median;


          printf("\nEnter ten numbers \n\n");

           getdata(array);

           displaydata(array );


           largest=displaylargest( array);
               printf("\nThe  largest %d\n", largest);

             smallest=displaysmallest( array);
                printf("\nThe smallest is %d\n", smallest);

            average=displayaverage(array);
             printf("\nThe average is %d\n", average);

            range=displayrange(array);
            printf("\nThe range is %d\n", range);

            median=displaymedian(array);
            printf("\nThe median is %d\n", median);
     return 0;
  }

 void getdata(int array[])

  {
        int x;
             printf ("Enter a number\n ",x+1);
        for(x=0;x<MAX;x++)

              scanf ("%d",&array[x]);

    }

     int displaylargest(int array[])
    {
         int x, largest=array[0];


        for (x=0; x<MAX; x++)
       {
         if (array[x]>largest)

           largest=array[x];

       }


        return(largest);


    }

   int displaysmallest(int array[])


    {

        int x, smallest=array[0];

        for (x=0; x<MAX; x++)
       {
         if (array[x]<smallest)

           smallest=array[x];

       }


        return(smallest);


    }


                int displayaverage(int array[])

    {
        int x;
        int sum=0;
        int average;

            for (x=0; x<MAX; x++)
        {
        sum+=array[x];  
        }
        {
            average=sum/MAX;    
        }




        return(average);

    }
    int displayrange(int array[])
    {

        int x;
        int range;
        int largest=array[0];
        int smallest=array[0];
        for (x=1; x <MAX; x++)
        {

         if (array[x] < smallest) smallest = array[x];
         if (largest < array[x])largest = array[x]; 

        }

         range=largest-smallest;
         return range;

    }


    int median(int array[]) {
int temp;
int median;
int x, y;
// the following two loops sort the array x in ascending order
for(x=0; x<MAX-1; x++) {
    for(y=x+1; y<MAX; y++) {
        if(array[y] < array[x]) {
            // swap elements
            temp = array[x];
            array[x] = array[y];
            array[y] = temp;
        }
    }
}

if(MAX%2==0) {
    // if there is an even number of elements, return mean of the two elements in the middle
    median=(array[MAX/2] + array[MAX/2 - 1]) / 2.0;
  return median;
} else {
    // else return the element in the middle
   median= array[MAX/2];
    return median;
}
 }


       void displaydata(int array[])
      {

       int x;
     for(x=0; x<MAX; x++)
    {
    printf("%d, ",array[x]);

       }
    }
  • 4
    I'm like 99% sure that was `ld`, not `id`, that returned a 1 exit status. – zwol Nov 03 '17 at 15:36
  • 2
    where is your `displaymedian` function? The linker doesn't know either. – yano Nov 03 '17 at 15:37
  • 2
    _One_ of the problems with this code is that you cannot define functions inside of other functions in C. I'm surprised it even got as far as complaining about the missing `displaymedian` function. – zwol Nov 03 '17 at 15:37
  • yes correct sorry – andreanobles Nov 03 '17 at 15:37
  • its not missing added my display function i had to edit – andreanobles Nov 03 '17 at 15:39
  • 1
    I just noticed you said "(there is more to this code)" But if you want us to help you with compiler errors we need to see the _entire program_, because it's very common that the problem turns out to be in the code you didn't think was relevant and didn't show us. – zwol Nov 03 '17 at 15:40
  • should i add the whole code? like i stated there is more but this is where the problem is – andreanobles Nov 03 '17 at 15:40
  • Please read and follow the instructions at https://stackoverflow.com/help/mcve . – zwol Nov 03 '17 at 15:41
  • 1
    The code calls a function named `displaymedian` but the "display function" you added is named `displaydata`, not `displaymedian`... – zwol Nov 03 '17 at 15:42
  • 2
    Please, do us (and yourself as well) a favour and format your code properly. This is merely readable. – alk Nov 03 '17 at 15:42
  • sorry like i said i am new but i figured it out i named a function display median and didnt call it that i called it int median thanks. – andreanobles Nov 03 '17 at 15:46
  • 1
    @andreanobles please do yourself a favor and learn how to format C code correctly. Things are so much easier with corretly formatted code. – Jabberwocky Nov 03 '17 at 15:49
  • 1
    You should NOT change your question in this way. All answers and commetns are relative to your previous version. – Paul Ogilvie Nov 03 '17 at 15:49
  • You call a `displaymedian` function, but there _is_ no such function in your code. The error message you get is quite clear isn't it? – Jabberwocky Nov 03 '17 at 15:55

1 Answers1

0

You call a function named displaymedian, but in your code there is no function with that name, but there is a function named median.

So just change:

int median(int array[])

to:

int displaymedian(int array[])

With this correction your program compiles (with warnings though) and appears to run correctly, but I didn't check if the calculations are actually correct.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115