简体   繁体   中英

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

smallest element present in the given array is : -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 is overwritten each time.

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

That does nothing. Technically it loops from 0 to randnumber doing nothing. It doesn't initialize 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 . It needs to go from 0 to 99, the size of a . But a is not initialized so it's full of garbage. That's why you're getting weird results.


The mistakes are you need to iterate from 0 to the size of a . You need to bring your first two loops together to initialize a . And you need to seed rand or it will always produce the same numbers.

// 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.

You need to put i = 0 because an array starts from 0. Or you can put a[i-1]. I would recommend i = 0.

Remove the second for loop that has the "a[randomnumber]".

Add "a[i] = randomnumber;" 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. Changing it to i = 0 allows a[0] to be filled with one of the values created in random.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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