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"
Asked
Active
Viewed 596 times
2 Answers
1
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
}

Yash Parekh
- 1,513
- 2
- 20
- 31
-
thanks man! but i need random values so i just replace it ro rand % n? – Shay Doron May 13 '17 at 07:08
-
2A VLA is not the better option in this case (you don't check if `n` is a huge number and can cause a stack overflow, see http://stackoverflow.com/q/7326547/1606345) , why not `malloc`? – David Ranieri May 13 '17 at 07:08
-
1@KeineLust You are right about memory allocation. But he already specified the range that is upto 500. – Yash Parekh May 13 '17 at 07:31
-
@Shady Doron If you want to go for dynamic memory allocation then go for this statement :- **int *arr = malloc(sizeof(*arr) * n);** instead of **int arr[n];** – Yash Parekh May 13 '17 at 07:32
-
Sure, the maximum size is 500, but you don't check if the user types something like 10000000 --> stack overflow. – David Ranieri May 13 '17 at 07:33
-
But I've generated the random no upto 500 only then it will not result into 10000000. Is it?? – Yash Parekh May 13 '17 at 07:36
-
1Thanks for that and appreciate your idea of using malloc. – Yash Parekh May 13 '17 at 07:39
-
@YashParekh the question is simply poorly worded and is not clear in actual intent. It isn't clear if the OP wants (a) the user to provide a size in `[0..500)`, then fill with random values, (b) the size is a random number in `[0..500)`, then filled with arbitrary user input, (c) a combination of both of those, or (d) something entirely different. It's why we ask for specific expectations, sample input and output, etc,., code they've done so far, otherwise is just speculation and guessing, which ultimately doesn't help future readers at all. – WhozCraig May 13 '17 at 18:50
-
i tried to use this but it's keep waiting for input. i think that you didnt understood me well. i need to get form the user just the size of the array and according to that i need to fiil th array with random values – Shay Doron May 14 '17 at 13:24
0
You can do something like this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printArray(int* p, int size)
{
for (int j = 0; j < size; ++ j) printf("%d ", p[j]);
printf("\n");
}
int main(void) {
srand(time(NULL)); // Start with a random seed based on time
int n = 0;
printf("Which array size do you need?\n");
if (scanf("%d", &n) != 1 || n < 1)
{
printf("Wrong input\n");
exit(0);
};
printf("Creating random array of size %d\n", n);
int* p = malloc(n * sizeof(*p)); // Reserve memory
for (int j = 0; j < n; ++j) p[j] = rand() % 500; // Generate random numbers
printArray(p, n); // Print the result
free(p); // free the allocated memory
return 0;
}

Support Ukraine
- 42,271
- 4
- 38
- 63