Home > other >  How can I print highest value in an array?
How can I print highest value in an array?

Time:06-22

I have scanf in a array I need to print the highest array stored the problem is it print 0 every print

#include <stdio.h>
int main()
{
    int n;            // To input how many set of number to be entered
    int order_number; // To count the numbers being entered
    int length;       // To count how many numbers in the array to be used in the last print
    int max;          // max computations later in last print
    float num[100], sum = 0.0, average;

    printf("Enter how numbers to be counted: ");
    scanf("%d", &n); // asks the user how many numbers to be entered

    while (n > 100 || n < 1)
    {
        printf("The entered number could not handle 100 above or below 1\n"); // shows if the user enters below 1 and above 100
        printf("Please enter a number again: ");                              // asks the user to enter a number again
        scanf("%d", &n);                                                      // asks the user how many numbers to be entered
    }

    for (order_number = 0; order_number < n;   order_number) // loop syntax for counting numbers being entered
    {
        printf("%d. Enter number: ", order_number   1); // prints the counting number being entered
        scanf("%f", &num[order_number]);                // asks the user for a number then store to the array
        sum  = num[order_number];                       // adds all the number from the array
    }

    average = sum / n; // computes the average by adding a group of numbers and then dividing by the count of those numbers.
    printf("The average is %.2f\n", average);

    // Problem start here
    length = sizeof(num) / sizeof(num[0]);
    max = num[100];
    for (int i = 0; i < length; i  )
    {
        if (num[i] > max)
            max = num[i];
    }
    printf("Largest element present in given array: %d\n", max);

    return 0;
}

CodePudding user response:

#include <stdio.h>
int main()
{
    int n;            // To input how many set of number to be entered
    int order_number; // To count the numbers being entered
    int length;       // To count how many numbers in the array to be used in the last print
    int max;          // max computations later in last print
    float num[100], sum = 0.0, average;

    printf("Enter how numbers to be counted: ");
    scanf("%d", &n); // asks the user how many numbers to be entered

    while (n > 100 || n < 1)
    {
        printf("The entered number could not handle 100 above or below 1\n"); // shows if the user enters below 1 and above 100
        printf("Please enter a number again: ");                              // asks the user to enter a number again
        scanf("%d", &n);                                                      // asks the user how many numbers to be entered
    }

    for (order_number = 0; order_number < n;   order_number) // loop syntax for counting numbers being entered
    {
        printf("%d. Enter number: ", order_number   1); // prints the counting number being entered
        scanf("%f", &num[order_number]);                // asks the user for a number then store to the array
        sum  = num[order_number];                       // adds all the number from the array
    }

    average = sum / n; // computes the average by adding a group of numbers and then dividing by the count of those numbers.
    printf("The average is %.2f\n", average);

    max = num[0];
    for (int i = 1; i < n; i  ) // computation on finding the highest number in the array using loop 
    {
        if (num[i] > max)
            max = num[i];
    }
    printf("The largest number in the array is %d\n", max);

    return 0;
}

CodePudding user response:

max = num[100]; - you are looking at the 101st element of a 100 element array, indexed from zero. If you're lucky, you don't get a segmentation fault or some other memory error, since you're looking past the end of the array, but you're still going to get something random that happened to be in that memory location.

To iterate over the array, you then use all elements of the array, since you limit the loop by length - rather than 'n', which you've set to the number of used elements in the array. So you're also including length - n elements at the end that were never initialised to a useful value.

  •  Tags:  
  • c
  • Related