-2

I'm writing a program in C to add random numbers to an array. The size of the array is given (say n = 250) and using rand(), I'm generating these numbers (range is 1 to 100).

int main()
{
    int n = 250;
    int array[n];
    for (int i = 0; i < n; i++)
    {
        srand(time(NULL));
        int r = rand()%100 + 1;
        array[i] = r;
    }
    printf("Output Size: %lu\n", sizeof(array));

    return 0;
}

When I run the code, the result is- Output Size: 1000

Expected result is 250. What am I doing wrong?

userx
  • 11
  • 4
  • 1
    250 * 4 bytes int. See: https://www.programiz.com/cpp-programming/examples/sizeof-operator. Your 250 element array, where each element is an integer, and in your case 4 bytes per integer. So 250 * 4 = 1000 – Omid CompSCI May 25 '20 at 03:02
  • Gee, I feel dumb! Thanks for the clarification @OmidCompSCI :) – userx May 25 '20 at 03:04
  • 1
    You only want to call `srand` once, at the start. Don't call it every single time you create a random number. It will result in poor random numbers, and it will slow tremendously. Try to understand its purpose. You're calling `time()` for *every single number you generate*. It makes no sense. – Tom Karzes May 25 '20 at 03:05
  • 1
    @TomKarzes And if the loop runs in less than a second (which it almost certainly will), you'll probably get the same value in every element of the array. But in an case, `sizeof array` is not affected by the stored values. You could remove the loop without affecting the program's behavior. – Keith Thompson May 25 '20 at 03:19
  • Understood. I've edited the code and see the results now. Thanks again! – userx May 25 '20 at 03:20
  • userx, Curious why did you select `%lu` to use with `sizeof(array)`? – chux - Reinstate Monica May 25 '20 at 03:27
  • @KeithThompson Yes, exactly. – Tom Karzes May 25 '20 at 03:35
  • 1
    @chux-ReinstateMonica, Initially, I used %d but got an error "warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat]" so I switched to %lu and got the output 1000. Later, I used a variable to record the array size instead and used that with %d. – userx May 25 '20 at 03:50
  • @userx also: [`%zu`](https://stackoverflow.com/questions/2524611/how-can-one-print-a-size-t-variable-portably-using-the-printf-family) – Antti Haapala -- Слава Україні May 25 '20 at 04:29
  • 1
    @userx `%lu` is the correct format for `unsigned long`, but `size_t` isn't guaranteed to be `unsigned long`. `%lu` works for your implementation, but is not portable. `%zu` is correct for any implementation. (The warning could be better.) – Keith Thompson May 25 '20 at 07:06

3 Answers3

1

sizeof(type) returns the size, in bytes, of the type. To find out size of array, you can do:

    printf("Output Size: %zu\n", sizeof(array)/sizeof(array[0]);

Also, the type of the result of sizeof operator is size_t. You should use %zu format specifier instead of %lu.

H.S.
  • 11,654
  • 2
  • 15
  • 32
1

I think you're expecting n as output (number of elements in the array); but you're doing it wrong. Currently, what you're getting is 250*4 = 1000 (i.e., size of int is 4, and the number of elements is 250).

Replace sizeof(array) with sizeof(array)/sizeof (array[0])

Read this to dive deeper.

shmsr
  • 3,802
  • 2
  • 18
  • 29
0

You have 250 elements array, each array element is of size 4 bytes. 250*4 = 1000.

And you don't need to call srand in loop.

read this article about srand.

Puneet Shekhawat
  • 1,657
  • 2
  • 7
  • 13