Search code examples
carraysrandominitializer

Initializes an array by a given size in random values between 1 and 500 in c


I have assignment to write program that sort an array and search for a specific number, that part I've already done, My problem is how to Initialize the array in the size that the user sets with random values ​​smaller than 500? I know how to do that with known size but not with unknown size? example for input/output: "Please enter the size of the array: 5"


Solution

  • This will help you.

    int main(void)
    {
        int n,i;
        n=rand()%500; // Get random value
        int arr[n]; // Initialize the dynamic array
        for(i=0;i<n;i++)
            scanf("%d",&arr[i]);
        // Do your stuff
    }