Hello all. Anywho ive been studying c now for a few weeks, and up until now it has not been too steep of a learning curve for me but that has changed recently. My problem is basically that i need to create an array with 50 random integers using srand() . Now i've determined that the easiest way to do this is to use a for loop. However i'm unsure as to how i would set up this for loop so that it does all 50 integers within the array.
Any help would be greatly appreciated.
Thanks!

Recommended Answers

All 3 Replies

/*
 * Giveme50.c
 * Show how to assign random integer into array.
 */

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

int main( void )
{
    int fifty[50];
    int i;
    
    srand( (unsigned ) time( NULL ) );
    
    for( i = 0 ; i < 50; i++ )
    {
        fifty[i] = rand();
    }
    
    for( i = 0; i < 50; i++ )
    {
        printf( "%d\n", fifty[i] );
    }
    
    getchar();
    return 0;
}
commented: I'm Rashakil Fol and I disapprove of this message. -2
commented: Balance++ +9

could also print as you populate :p

Of course I could have ask it to give me a `croissant and a cup of coffee'. But I didn't. On purpose.
The thing I would change is the `magic number' 50.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.