我的程序的一部分是用来输出随机数数组中最小和最大的值我的程序输出数组中的最大值很好,但是当它输出最小值时,它会输出一些垃圾我已经搜索过这个网站,根据我的发现,我认为它应该工作得很好。这是我的代码,谢谢你的帮助!

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

int main(void){
    int randomNumbers[100];
    double average;
    int highest, smallest;
    int sum = 0;
    int i = 0;
    int min, max;
    srand(time(NULL));
    printf("Enter min and max respectively: ");
    scanf("%d %d", &min, &max);
    smallest = highest = randomNumbers[0];
    for (i; i < 100; i++){
        randomNumbers[i] = rand() % (max - min + 1) + min;
        sum = sum + randomNumbers[i];

        if (highest < randomNumbers[i]){
            highest = randomNumbers[i];
        }

        if (smallest > randomNumbers[i]){
            smallest = randomNumbers[i];
        }

        printf("%d ", randomNumbers[i]);
        if (i == 9 || i == 19 || i == 29 || i == 39 || i == 49 || i ==59 || i == 69 || i ==79 || i == 89)
            printf("\n");

    }
    average = sum / 100;
    printf("\nAverage: %lf", average);
    printf("\nHighest: %d", highest);
    printf("\nSmallest: %d", smallest);

}

最佳答案

问题在于您的初始化:

smallest = highest = randomNumbers[0];

randomNumbers此时尚未填充。如果您先填充所有数字,可能会更清楚。

10-06 08:07