Home > Back-end >  Printing the largest element of an array
Printing the largest element of an array

Time:05-30

So this title is pretty garbage, I just really don't know how else to explain it. You'll get it when you see the code though, which is this:

#include <stdio.h>

void products(float values[], int prod_num);

int main(void) {
    int prod_num;
    printf("Enter the number of products: ");
    scanf("%d", &prod_num);
    float values[prod_num];
    for (int i = 0; i < prod_num; i  ) {
        printf("Enter the value of the product: ");
        scanf("%f", &values[i]);
    }
    products(values, prod_num);
    return 0;
}

void products(float values[], int prod_num) {
    int i, j, t;
    float sum = 0, avg;
    for (i = 0; i < prod_num; i  ) {
        sum = sum   values[i];
        avg = sum / prod_num;
    }
    printf("The average is: %f\n", avg);
    for (i = 0; i < prod_num; i  ) {
        for (j = i   1; j < prod_num; j  ) {
            if (values[i] > values[j]) {
                t = values[i];
                values[i] = values[j];
                values[j] = t;
            }
        }
    }
    printf("Largest value: %f\n", values[]);
    printf("Smallest value: %f", values[0]);
}

So this code, as you can tell, is using the function products to find the average of the values of some products, which works fine, and also find the largest value and the smallest, from the values array. I thought the best way to do this is to arrange the array in ascending order and to then print values[0], as that would be the smallest value, and then print the very last element. That's my problem. How do I tell it to print out the last element of the array, when I don't know its size, since it's entered by the user? I actually had a different way of doing this part which was this (I'm showing just the function):

void products(float values[], int prod_num) {
    int i;
    float sum = 0, avg;
    for (i = 0; i < prod_num; i  ) {
        sum = sum   values[i];
        avg= sum / prod_num;
    }
    printf("The average is: %f\n", avg);
    for (i = 1; i < prod_num; i  ) {
        if (values[0] < values[i]) {
            values[0] = values[i];
        }
    }
    printf("Largest value: %f\n", values[0]);
    for (i = 1; i < prod_num; i  ) {
        if (values[0] > values[i]) {
            values[0] = values[i];
        }
    }
    printf("Smallest value: %f\n", values[0]);
}

The problem is that it would print out the second smallest value, since the values would have changed because of the for loop tasked with finding the largest value, so then I tried the first method that I showed. The solution can be for either one of these two methods, I don't mind. Or I guess it could be a completely different method that's not complete garbage, like how I'm suspecting my two could be.

CodePudding user response:

For find the min and max in your products you dont need to sort the array(Although even so you will find the right solution) because it will affect the performance of your program in terms of runtime. Anyway, if you still want to sort , use values[prod_num-1] for find the max element . for find the min and max without sorting your array you can use this example:

float maxi = values[0];
float mini = values[0];
for (i=1;i<prod_num;i  )
{
    if(maxi<values[i])
        maxi=values[i];
    if(mini>values[i])
        mini = values[i];
}

CodePudding user response:

How do I tell it to print out the last element of the array, when I don't know its size

You do know its size, in fact it was passed into the function.

The size of the array is prod_num, so the last element is values[prod_num-1].

On a side node, it's not necessary to sort the list to find the largest and the smallest. Use variables to keep track of the current smallest and largest values, initializing both to the first element of the array. Then iterate through the array, checking if the current element is either smaller than the min or larger than the max, and if update the value of the min/max.

CodePudding user response:

You know the size of the array. It is specified as the second function parameter

void products (float values[], int prod_num){

So you can write

printf("Largest value: %f\n", values[prod_num-1]);

Also there is no need to calculate the average in the for loop.

for (i = 0; i < prod_num; i  ) {
    sum = sum   values[i];
    avg= sum / prod_num;
}

You could calculate it only one time after the for loop.

In general it is a bad idea when the source array is changed.

To find the minimum and the maximum elements you could write

void products( const float values[], size_t prod_num ) 
{
    if ( prod_num != 0 )
    {
        float min = values[0];
        float max = values[0];
        float sum = values[0];

        for ( size_t i = 1; i < prod_num; i   ) 
        {
            sum  = values[i];
            if ( max < values[i] )
            {
                max = values[i];
            }
            else if ( values[i] < min )
            {
                min = values[i];
            }
        }
        printf( "The average is: %f\n", sum / prof_num );
        printf( "Largest value: %f\n", max );
        printf( "Smallest value: %f\n", min );
    }
    else
    {
        puts( "The array is empty." );
    }
}

CodePudding user response:

void products(float values[], int prod_num) {
    float sum = 0, avg, least = values[0], largest = values[0];
    for (int i = 0; i < prod_num; i  ) {
        sum = sum   values[i];
        largest = largest < values[i] ? values[i] : largest;
        least = least > values[i] ? values[i] : least;
    }
    avg = sum / prod_num;
    printf("The average is: %f\n", avg);
    printf("Largest value: %f\n", largest);
    printf("Smallest value: %f", least);
}

result

  • Related