eloen
eloen

Reputation: 9

Find median from a sorted array using a function in c

I have a program that takes integers from a array and prints out the largest, smallest, average and a sortes list. I need help with a function to find the median, and then print it out. Also my average only show whole numbers and no digits as I thought it would when I use float. But finding a median function is the important part.

#include <stdio.h>

#define NUMBERS_SIZE 5

int sum(int numbers[], int count)
{

    int sum = 0;

    for (int i = 0; i < count; i++)
        sum += numbers[i];

    return sum;
}


int maximum(int numbers[], int count)
{
    int a;


    int max = numbers[0];


    for (a = 1; a < count; a++)
        if (numbers[a] > max)
            max = numbers[a];

    return max;
}

int minimum(int numbers[], int count)
{
    int a;


    int min = numbers[0];


    for (a = 1; a < count; a++)
        if (numbers[a] < min)
            min = numbers[a];

    return min;
}

int average(int numbers[], int count)
{
    float avg;
    int sum = 0;

    for (int i = 0; i < count; i++)
        sum += numbers[i];

    avg = sum/count;

    return avg;
}

int cmpfunc (const void * a, const void * b) {
    return ( *(int*)a - *(int*)b );
}




int main()
{
    int numbers[NUMBERS_SIZE];

    int n;

    float median=0;

    for (int i = 0; i < NUMBERS_SIZE; i++)
    {
        printf("Enter integer: ");
        scanf("%i", &numbers[i]);
    }

    int result3 = sum(numbers, sizeof(numbers) / sizeof(numbers[0]));
    printf("The sum is: %i\n", result3);



    int count3 = sizeof(numbers)/sizeof(numbers[0]);
    printf("Largest number entered is: %d\n", maximum(numbers, count3));

    int count1 = sizeof(numbers)/sizeof(numbers[0]);
    printf("Smallest number entered is: %d\n", minimum(numbers, count1));

    int count4 = sizeof(numbers)/sizeof(numbers[0]);
    printf("Average is %d\n", average(numbers, count4));



    qsort(numbers, NUMBERS_SIZE, sizeof(int), cmpfunc);

    printf("\nSorted: \n");
    for( n = 0 ; n < NUMBERS_SIZE; n++ ) {
        printf("%d ", numbers[n]);
    }


    return 0;

}

Upvotes: 0

Views: 2316

Answers (3)

eloen
eloen

Reputation: 9

I found out why the average dident work, I used int average instead of float average when making the function. I also found a out how to make a function for median. Full code bellow

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

#define NUMBERS_SIZE 10

int sum(int numbers[], int count)
{

int sum = 0;

for (int i = 0; i < count; i++)
    sum += numbers[i];

return sum;
}


int maximum(int numbers[], int count)
{
int a;


int max = numbers[0];


for (a = 1; a < count; a++)
    if (numbers[a] > max)
        max = numbers[a];

return max;
}

int minimum(int numbers[], int count)
{
int a;


int min = numbers[0];


for (a = 1; a < count; a++)
    if (numbers[a] < min)
        min = numbers[a];

return min;
}

float average(int numbers[], int count)
{
float avg;
int sum = 0;

for (int i = 0; i < count; i++)
    sum += numbers[i];

avg = (float)sum/(float)count;

return avg;
}

int cmpfunc (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}


float median(int numbers[] , int count)
{
float medi=0;

// if number of elements are even
if(count%2 == 0)
    medi = (numbers[(count-1)/2] + numbers[count/2])/2.0;
    // if number of elements are odd
else
    medi = numbers[count/2];

return medi;
}


int main()
{
int numbers[NUMBERS_SIZE];

int n;

float medi=0;


int count = sizeof(numbers)/ sizeof(numbers[0]);

for (int i = 0; i < NUMBERS_SIZE; i++)
{
    printf("Enter integer: ");
    scanf("%i", &numbers[i]);
}










printf("Minimum: %d\n", minimum(numbers, count));

printf("Maximum: %d\n", maximum(numbers, count));

printf("Sum: %i\n", sum(numbers, count));


printf("Average: %g\n", average(numbers, count));



qsort(numbers, NUMBERS_SIZE, sizeof(int), cmpfunc);

printf("Sorted: ");
for( n = 0 ; n < NUMBERS_SIZE; n++ ) {
    printf("%d ", numbers[n]);
}



medi = median(numbers , count);

printf("\nMedian: %f\n", medi);

return 0;

}

Upvotes: 0

deniz kaya
deniz kaya

Reputation: 1

first of all the sequence of the sequence from large to small lisin then count / 2 by getting the indexed element should int medianNumber=count/2; int median=number[medianNumber];

Upvotes: 0

0x534f53
0x534f53

Reputation: 230

your average comes out rounded to an integer value because both sum and count are integers. Then, on the line avg = sum/count, it calculates sum/count first, which is rounded, then it is cast to a float and assigned to avg. You can fix this easily by casting the values to floats first, then performing the division:

avg = (float) sum / (float) count;

As for the median, since the input array is sorted, you can just find the length and index the middle value. If the length is even, just take the average of the two middle values.

Upvotes: 1

Related Questions