简体   繁体   English

使用数组编写C程序,以从100个随机数的列表中找到最大和最小的数

[英]Write a C program using array to find largest and smallest number from a list of 100 Random numbers

Here is the program I have written. 这是我编写的程序。 This program is generating random numbers correctly but when I execute, the code produces the following output: 该程序正确生成了随机数,但是当我执行该代码时,将产生以下输出:

largest element present in the given array is : -858993460 给定数组中存在的最大元素是: -858993460

smallest element present in the given array is : -858993460 给定数组中存在的最小元素为: -858993460

int main()
{
    int randnumber;
    int a[100], i, large, small;

    for (i = 1; i <= 100; i++)
    {
        randnumber = rand() % 100 + 1;
        printf("%d  ", randnumber);
    }
    for (i = 0; i < randnumber; i++)
    {
        a[randnumber];
    }
    large = small = a[0];
    for (i = 1; i < randnumber; i++)
    {
        if (a[i] > large)
        {
            large = a[i];
        }
        else if (a[i] < small)
        {
            small = a[i];
        }
    }
    printf("\n largest element present in the given array is : %d", large);
    printf("\n smallest element present in the given array is : %d", small);

    return 0;
}

A lot of your loops aren't doing what you think. 您的许多循环都没有按照您的想法进行。

for (i = 1; i <= 100; i++)
{
    randnumber = rand() % 100 + 1;
    printf("%d  ", randnumber);
}

That just sets and prints randnumber 100 times. 那只是设置并打印randnumber 100次。 randnumber is overwritten each time. randnumber被覆盖。

for (i = 0; i < randnumber; i++)
{
    a[randnumber];
}

That does nothing. 那什么也没做。 Technically it loops from 0 to randnumber doing nothing. 从技术上讲,它从0循环到randnumber什么也不做。 It doesn't initialize a . 它不会初始化a

for (i = 1; i < randnumber; i++)
{
    if (a[i] > large)
    {
        large = a[i];
    }
    else if (a[i] < small)
    {
        small = a[i];
    }
}

This searches a for the largest and smallest, but it does it from 0 to randnumber . 这会搜索a ,以查找最大和最小的对象,但它会从0到randnumber进行randnumber It needs to go from 0 to 99, the size of a . 它需要从0到99(大小为a But a is not initialized so it's full of garbage. 但是a尚未初始化,因此充满了垃圾。 That's why you're getting weird results. 这就是为什么您得到奇怪的结果的原因。


The mistakes are you need to iterate from 0 to the size of a . 该错误是需要从0迭代到的大小a You need to bring your first two loops together to initialize a . 你需要把你的前两个循环一起初始化a And you need to seed rand or it will always produce the same numbers. 您需要播种rand ,否则它将始终产生相同的数字。

// Seed the random number generator.
// Note this is a terrible seed.
srand((unsigned int)time);

// Fill a with random numbers.
for (i = 0; i < 100; i++)
{
    randnumber = rand() % 1000 + 1;
    a[i] = randnumber;
}

large = small = a[0];
for (i = 1; i < 100; i++)
{
    if (a[i] > large)
    {
        large = a[i];
    }
    else if (a[i] < small)
    {
        small = a[i];
    }
}

I've also bumped up the random range to 1000. If you pick 100 random numbers from 1 to 100 odds are the smallest will be 1 and the largest will be 100. Not very interesting. 我还将随机范围提高到1000。如果您从1到100的赔率中选择100个随机数,则最小的将为1,最大的将为100。这不是很有趣。

You need to put i = 0 because an array starts from 0. Or you can put a[i-1]. 您需要放置i = 0,因为数组从0开始。或者您可以放置​​a [i-1]。 I would recommend i = 0. 我建议我= 0。

Remove the second for loop that has the "a[randomnumber]". 删除具有“ a [randomnumber]”的第二个for循环。

Add "a[i] = randomnumber;" 加“ a [i] =随机数;” to the first loop so the random values are set in the array as they are generated. 到第一个循环,因此在生成随机值时会在数组中设置它们。

int main()
{
int randnumber;
int a[500], i, large, small;

for (i = 0; i < 500; i++)
{
    randnumber = rand() % 100 + 1;
    printf("%d  \n", randnumber);
    a[i] = randnumber;

}
large = small = a[0];
for (i = 0; i < 500; i++)
{
    if (a[i] > large)
    {
        large = a[i];
    }
    else if (a[i] < small)
    {
        small = a[i];
    }
}
printf("\n largest element present in the given array is : %d", large);
printf("\n smallest element present in the given array is : %d", small);

return 0;
}

Currently, the array is never filled with the random numbers. 当前,数组永远不会填充随机数。 It is reading "junk" values from the array to find the largest and smallest numbers. 它正在从数组中读取“垃圾”值,以找到最大和最小的数字。

for (i = 0; i < 100; i++)
{
    randnumber = rand() % 100 + 1;
    printf("%d  ", randnumber);
    a[i] = randnumber;
}

Also, with i = 1 , this leaves a[0] with a value that wasn't created in random. 另外,在i = 1的情况下 ,这会给a [0]保留一个并非随机创建的值。 Changing it to i = 0 allows a[0] to be filled with one of the values created in random. 将其更改为i = 0可使a [0]填充随机创建的值之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM