Home > Net >  Finding MAX and MIN of random numbers
Finding MAX and MIN of random numbers

Time:10-30

I'm writing a program where the user enters numbers and the program will find MAX and MIN and the position of these numbers. I want to give the user a choice for the program to fill in the numbers for him using rand().

It's working almost perfectly: the program will find the MAX number with the position but the problem occurs when printing MIN number with position -- it always prints number 8 and position 1.

Where is the problem?

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct elementposition {
    int min;
    int max;
    int positionMax;
    int positionMin;
} elementposition;

int main() {
    struct elementposition minmax;

    srand(time(NULL));
    int a[500], i;
    int c = sizeof(a) / sizeof(a[0]);
    char y;

    printf("How many numbers you want to enter: ");
    scanf("%d", &c);
    minmax.positionMax = minmax.positionMin = 0;

    printf("Want to fill with random numbers? (Y/N)");
    scanf(" %c", &y);

    if (y == 'Y' || y == 'y') {
        for (i = 0; i < c; i  ) {
            a[i] = rand() % 10000   1;

            if (minmax.max < a[i]) {
                minmax.max = a[i];
                minmax.positionMax = i;
            }

            if (minmax.min > a[i]) {
                minmax.min = a[i];
                minmax.positionMin = i;
            }
        }

        for (i = 0; i < c; i  ) {
            printf("Number #%d: %d\n", i   1, a[i]);
        }
    } else {

        printf("------------------------------------ \n");

        printf("Enter (%d) numbers: \n", c);
        scanf("%d", &a[0]);
        minmax.max = minmax.min = a[0];

        for (i = 1; i < c; i  ) {
            scanf("%d", &a[i]);

            if (minmax.max < a[i]) {
                minmax.max = a[i];
                minmax.positionMax = i;
            }

            if (minmax.min > a[i]) {
                minmax.min = a[i];
                minmax.positionMin = i;
            }
        }
    }
    printf("\nMax number is %d, number position %d. \n", minmax.max, minmax.positionMax   1);
    printf("Min number is %d, number position %d. \n", minmax.min, minmax.positionMin   1);

    printf("------------------------------------ \n");

    getch();
    return 0;
}

CodePudding user response:

You use minmax.min and minmax.max before initializing them. Here the problem for finding the min is probably that the minmax.min happens to initialy contain the value 8 and that all the values are greater.

The common way is to initialize the min to the highest possible value and max to the lowest one. As you use int values:

struct elementposition minmax = { INT_MAX, INT_MIN };

should be enough.

CodePudding user response:

You never initialize minmax.min nor minmax.max in the random case. The code has undefined behavior because it depends on uninitialized values which may be anything, including trap values on some rare architectures.

You should separate the input/generation phase from the scanning phase and use a common loop for that. Also check that c is positive and does not exceed the length of the array.

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>

typedef struct elementposition {
    int min;
    int max;
    int positionMax;
    int positionMin;
} elementposition;

int main() {
    struct elementposition minmax;
    int a[500];
    int i, count, len = sizeof(a) / sizeof(a[0]);
    char y = 'y';

    printf("How many numbers you want to enter: ");
    if (scanf("%d", &count) != 1 || count < 1 || count > len) {
        printf("invalid count\n");
        return 1;
    }

    printf("Want to fill with random numbers? (Y/N)");
    scanf(" %c", &y);
    if (y == 'Y' || y == 'y') {
        srand(time(NULL));
        for (i = 0; i < count; i  ) {
            a[i] = rand() % 10000   1;
            printf("Number #%d: %d\n", i   1, a[i]);
        }
    } else {
        printf("Enter (%d) numbers:\n", c);
        for (i = 0; i < count; i  ) {
            if (scanf("%d", &a[i]) != 1) {
                printf("invalid input\n");
                return 1;
            }
        }
    }

    minmax.positionMax = minmax.positionMin = 0;
    minmax.max = minmax.min = a[0];

    for (i = 1; i < count; i  ) {
        if (minmax.max < a[i]) {
            minmax.max = a[i];
            minmax.positionMax = i;
        }
        if (minmax.min > a[i]) {
            minmax.min = a[i];
            minmax.positionMin = i;
        }
    }

    printf("------------------------------------\n");
    printf("Max number is %d, number position %d.\n", minmax.max, minmax.positionMax   1);
    printf("Min number is %d, number position %d.\n", minmax.min, minmax.positionMin   1);
    printf("------------------------------------\n");

    getch();
    return 0;
}
  • Related