0

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;

}
2
  • Calculate average as avg = (float)sum/count to convert sum to a float. Otherwise, C will first make an integer division and then assign that integer result to avg. Commented Oct 29, 2019 at 21:20
  • Please explain the exact problem with calculating the median and the things you tried. Commented Oct 29, 2019 at 21:21

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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];

1 Comment

Hi @deniz kaya, can you please illustrate your thinking with a bit of working code. Maybe try pointing out possible corrections alongside your explanations.
0

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.