0

I trying to send array to function, but my program gets stuck

int main()
{
    int n, i;
    bool random;

    cout << "number of elements in array:"; cin >> n;
    cout << "use random values?"; cin >> random;

    int* arr = new int[n]; //create int array with n size
    init_array(random, n, arr); //fill array using function

    for (i = 0; i <= n; i++) //display array
        cout << " " << arr[i];

    return 0;
}

This function should fill array with random number or input from keyboard

void init_array(bool random, int n, int* arr)
{
    int i;
    if (random)
    {
        srand(time(0)); //initialize random;
        for (i = 0; i < n; i++)
            arr[i] = rand() % 100;
    }
    else
        for (i = 0; i<n; i++)
            cout << "arr[" << i << "]: "; cin >> arr[i];
}

Is there any way send dynamic array to function?

4
  • 1
    for (i = 0; i <= n; i++) ... i<n just as in every other loop ;) Commented Nov 10, 2013 at 18:57
  • 1
    There is no difference between passing dynamic and static arrays by pointers. What do you mean by stuck? Commented Nov 10, 2013 at 18:58
  • Oh, btw: There's no need to declare int i; at the beginning of the function. for(int i = 0; i < n; i++) works as well, and limits the scope of i to where it is actually required. Commented Nov 10, 2013 at 18:59
  • Thanks all, problem fixed. I forgot {} brackes in second for cycle in function. Commented Nov 10, 2013 at 19:08

2 Answers 2

2

When you do not use brackets after your for-loop, only the first statement is used as a loop:

else
    for (i = 0; i<n; i++)
        cout << "arr[" << i << "]: "; cin >> arr[i];

This loop will attempt to print "arr[#]" n times, and then ask for an input (which will attempt to be placed in the item 1 after the last element in your array (UB).

What you want is this:

else
{
    for (i = 0; i<n; i++)
    {
        cout << "arr[" << i << "]: "; 
        cin >> arr[i];
    }
}

You also have a problem with your output:

for (i = 0; i < n; i++) // <= would attempt to print 1 more item than exists in the array

And just for completeness, most of these issues go away when you use a container that does all of this for you:

int main()
{
    int n = 0;
    bool useRandom = false;
    std::cout << "number of elements in array:"; 
    std::cin >> n;
    std::cout << "use random values?"; 
    std::cin >> useRandom;

    std::vector<int> arr(n);
    init_array(useRandom, arr);

    std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " "));

    return 0;
}

void init_array(bool useRandom, std::vector<int>& vec)
{
    ::srand(time(0)); //initialize random;
    int n = 0;
    std::transform(vec.begin(), vec.end(), vec.begin(), [&](int i)
    {
        if (useRandom)
        {
            i = rand() % 100;
        }
        else
        {
            std::cout << "arr[" << n++ << "]:  ";
            std::cin >> i;
        }
        return i;
    });
}
Sign up to request clarification or add additional context in comments.

Comments

2

Your code is asking for a number at the end because of last cin>>n

fix else part in init_array as:

 else
        for (i = 0; i<n; i++)
            { //Notice braces
             cout << "arr[" << i << "]: ";
             cin >> arr[i];
            }

Also fix:

for (i = 0; i < n; i++) //display array from index 0 to n-1
    cout << " " << arr[i];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.