Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Maximum & Minimum Values

1 view
Skip to first unread message

😉 Good Guy 😉

unread,
Jun 18, 2021, 10:06:57 PM6/18/21
to

From time to time students are asked to write a program to find the maximum and minimum value in an array.  One way to do this is as in this example:

/*
 * Copyright (c) 2021, Good Guy
 * Program may contain bugs . . .
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_SIZE 256 // Maximum array size

int main()
{
    int arr[MAX_SIZE];
    int max, min, size;
    srand((unsigned)time(NULL));

    /* Input size of the array */
    printf("Enter the size of your array: ");
    scanf("%d", &size);

    /* Input array elements */
    for (int j = 0; j <= size; j++)
    {
        arr[j] = rand() % 101;
    }
    printf("Your numbers are: \n");
    for (int j = 0; j < size; j++)
    {
        printf("%4d", arr[j]);
    }
    printf("\n");

    /* The first element in the array is taken as maximum and minimum */
    max = arr[0];
    min = arr[0];

    /*
     * Now find the actual maximum and minimum in array elements.
     */
    for (int i = 1; i < size; i++)
    {
        /* If current element is greater than max */
        if (arr[i] > max)
        {
            max = arr[i];
        }

        /* If current element is smaller than min */
        if (arr[i] < min)
        {
            min = arr[i];
        }
    }

    /* Print maximum and minimum element */
    printf("Maximum element = %d\n", max);
    printf("Minimum element = %d\n", min);

    return EXIT_SUCCESS;
}

--

With over 1.3 billion devices now running Windows 10, customer satisfaction is higher than any previous version of windows.

0 new messages