-3

How do I make a function with for loop to store a given range of numbers into an array, then call that function in main program and print out the stored elements inside of the array?

int main ()
{
    testing(array, 20);
}

int testing(int array[], int k)
{
    for (int i = 0; k < 20; i++)
    {
        array[k] = i;
        k++;
    }

    for (int j = 0; j < 20; j++)
    {
        cout << array[j] << endl;
    }

}

I get the error of, "testing has to return value", which I understand that I should of have return var; for example. However, I don't know how to return an array of given elements to print out all the elements with a for loop.

K L
  • 17
  • 6
  • `int testing(...)` means that the function should return an `int`. If you don't want to return anything, make it `void testing(...)`. Also, you need to declare `int array[20];` in `main` before you call `testing`. – Ted Lyngmo Feb 05 '19 at 13:06
  • check this-->https://stackoverflow.com/questions/3473438/return-array-in-a-function – Swati Feb 05 '19 at 13:06
  • Okay, I solved that, but get a bunch of garbage values, twenty same values of: -844651320 -844651320 -844651320 . . . – K L Feb 05 '19 at 13:12

2 Answers2

1

Your function and loop constructions are not carefully designed. You can use pointers instead of having to return an array. I think this is what you are trying to do:

#include <iostream>    

void testing(int* a, int k)
{
    for (int i = 0; i < k; i++)
        a[i] = i;
}

int main()
{
    int a[20];
    testing(a, 20);
    // you can see that a's elements have changed outside the main (in testing)
    for (int j = 0; j < 20; j++)
        std::cout << a[j] << "\n";
    return 0;
}
Ekin Karadağ
  • 51
  • 1
  • 7
  • returns testing identifier not found. – K L Feb 05 '19 at 13:37
  • [K L](https://stackoverflow.com/users/10020076/k-l) are you sure you have typed testing function above `main()`? If not, do so. If you don't want to, you can just add `void testing(int* a, int k);` above `main()`. For further information [Function identifier not found](https://stackoverflow.com/a/8329124/8815822) – Ekin Karadağ Feb 05 '19 at 13:41
  • Every program starts execution from main(). That's why you have to inform the program about the function **before** main() starts. The code I have given is exactly right as it should be. Can you please directly copy everyting and try again. Because it worked on every compiler I know, including online compilers. – Ekin Karadağ Feb 05 '19 at 14:37
  • I'm not sure why it works for me in Codeblocks, but not in VS 2017. Anyway thanks, I am aware of how memory works and that we need to be careful with passing by value/reference, since the local variables in functions, located in stack frame that get invoke aren't holding values anymore when they stop executing. – K L Feb 05 '19 at 16:01
  • What is the error you get in VS 2017. Because I tried it on my VS 2017 Community and it worked flawlessly. – Ekin Karadağ Feb 05 '19 at 16:11
0

You don’t seem to ever execute the first loop. Your stop condition for your first loop is k < 20, but k already is 20. Then on the second loop, the array just prints out the garbage that was previously saved at those memory locations.

I think what you meant to do was:

for (int i= 0; i < k; i++) {
    array[i] = i;
}
for (int j = 0; j < k; ++j)  {
    cout << array[j] << endl;
}

If you wanted to print the array in main, you need to change the function a bit, by passing in a pointer to the array rather than the array itself.

Also, your main function never initialised an array to pass into your function.

int main (void) {
    int a[20];
    testing (a, 20);
    return 0;
}

Finally, set the return type of the testing function to void.

void testing (int array[], int k);

All in all, it should instead look like this:

#include <iostream>

using namespace std; // I don't recommend doing this

void testing (int * array, const int k);

int main (void) {

    const int size = 20;
    int a[size] = {0};
    testing (a, size);
    for (int i = 0; i < size; ++i) {
        cout << a[i] << endl;
    }
    return 0;
}

void testing (int * array, const int k) {
    for (int i = 0; i < k; ++i) {
         array[i] = i;
    }
}
Z4ckZ3r0
  • 113
  • 9