Search code examples
cfor-loopmaxminfunction-definition

Printing the largest element of an array


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.


Solution

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