0

I'm trying to code a void function that takes 50 random numbers between two parameters and assigns to an array for homework. And output gives me a bunch of 0 and weird numbers like 4254585 or 11277136, how can i fix that.

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

using namespace std;

void rndnums(int max ,int min, int arr[50])
{
    int rnd;

    srand(time(NULL));

    for(int i=0; i<50; i++)
    {
        rnd = rand() % max + min;
        arr[i] = rnd;
    }
}

int main()
{
    int arr[50];
    rndnums(30, 10, &arr[50]);

    for(int i=0; i < 50; i++)
        cout << i + 1 << ". = " << arr[i] << endl;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Gryes
  • 5
  • 1
  • 5
    That's _undefined behavior_: `&arr[50]` – πάντα ῥεῖ Jan 02 '22 at 10:54
  • 2
    Try `rndnums(30, 10, arr);` and try not to guess syntax. – paddy Jan 02 '22 at 10:57
  • 1
    Also `srand(time(NULL));` should be called only once in `main()`. – πάντα ῥεῖ Jan 02 '22 at 10:59
  • `rnd = rand() % max + min;` This formula is wrong: just consider the case where `min=1000` and `max=1001`: The maximum value this expression yields is `min + (max-1) = 2000 > 1001 = max`. You need to use `rnd = rand() % (max - min + 1) + min;` instead (assuming both bounds are included in the desired range) – fabian Jan 02 '22 at 11:01
  • C++ does btw provide more functionality than C does via the standard library; Imho it would be preferrable to use this functionality: https://en.cppreference.com/w/cpp/numeric/random – fabian Jan 02 '22 at 11:07
  • 1) You can't give size for an array in argument list you should define it as a pointer or array definition like " int arr[] ". So, you should fix your function definition like " void rndnums(int max ,int min, int arr[]) " in line 7. 2) You can't send an array with size in parameter list in a function. You should fix your function call like " rndnums(30, 10, arr); " in line 23. – Harun Cetin Jan 02 '22 at 11:40

1 Answers1

1

In the declaration of your function, int arr[50] means "a pointer to the first element of an array". The compiler ignores the 50 (for historical reasons).

When calling the function, you should provide just that: rndnums(30, 10, &arr[0]).

Also, C++ has a dedicated syntax for that, inherited from C: rndnums(30, 10, arr). Why it works is a little complicated to explain, but it was designed with exactly this use case in mind — send a pointer and array size to a function which fills an array.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • Probably better to change the function signature to `void rndnums(int max, int min, int (&arr)[50])` which would prevent the use of `rndnums(30, 10, &arr[0])` which would imho be for the better. Note that there's still a bug in the way the random numbers are generated... – fabian Jan 02 '22 at 11:14
  • Yes, that would be better. But it seems the code is in "C-like C++", which would require a pointer instead of reference, and probably deeper understanding of language. And if you have such understanding, just use the C++ standard library (`std::vector` or `std::array`). – anatolyg Jan 02 '22 at 11:20
  • 1
    `"Why it works is a little complicated to explain"` -- [What is array to pointer decay?](https://stackoverflow.com/q/1461432/12149471) – Andreas Wenzel Jan 02 '22 at 11:20