0

i want to pass array to recursive function , but i need to keep all the arrays

output :

1 0 0 0 0 
1 2 0 0 0 
1 2 3 0 0 
1 2 3 0 0 
1 2 3 0 0

when i return to the previous step i want to use array : 1 2 0 0 0

second return : 1 0 0 0 0

not this one : 1 2 3 0 0

my goal is to see output like this

1 0 0 0 0 
1 2 0 0 0 
1 2 3 0 0 
1 2 0 0 0 
1 0 0 0 0

.

    #include<iostream>
    using namespace std;
    int a[100];
    void go (int a[100],int x)
    {
         a[x]=x;
         for(int i=1;i<=5;i++)cout<<a[i]<<" ";cout<<endl;
         if(x==3)return;
           go(a,x+1);
        for(int i=1;i<=5;i++)cout<<a[i]<<" ";cout<<endl;
    }
    main()
    {
         go(a,1);
    }

it uses only 1 array :/ i need it to create another arrays 'a' and when it returns to previous step not to use the last array but the one which was remembered on that step

please help :(

1
  • Valid indexes of array size N are 0 to N-1. It may not be a problem here because of array size being 100 and you are accessing 1 to 5. Commented Sep 2, 2013 at 13:17

3 Answers 3

2

In general case, you can pass C++11 array class (or vector or even some your struct that contain array and overload operator[] if you cannot use C++11), but it's will be more slowely because every time when you pass array it will be copied and there will much more memory required in stack.

#include<iostream>
#include<array>
using namespace std;
array<int, 100> a;
void go (array<int, 100> a,int x)
{
    a[x]=x;
    for(int i=1;i<=5;i++)cout<<a[i]<<" ";cout<<endl;
    if(x==3)return;
    go(a,x+1);
    for(int i=1;i<=5;i++)cout<<a[i]<<" ";cout<<endl;
}
int main()
{
    go(a,1);
}

I think better use std::vector instead std::array if recursion will enought deep because it requiest less memory on stack and because in some programs (not in your example) for vector can use move version of constructor/operator= instead of copying.

Version, that use std::vector:

#include<iostream>
#include<vector>
using namespace std;
vector<int> a(100);
void go (vector<int> a,int x)
{
    a[x]=x;
    for(int i=1;i<=5;i++)cout<<a[i]<<" ";cout<<endl;
    if(x==3)return;
    go(a,x+1);
    for(int i=1;i<=5;i++)cout<<a[i]<<" ";cout<<endl;
}
int main()
{
    go(a,1);
}
Sign up to request clarification or add additional context in comments.

4 Comments

yes that is what i want :3 i want to copy the array for each step (i dont mind the memory) P.S i use dev c++ 4992 and it gives me error at : #include<array> , array<int, 100> a;
@AnimeaAnimea your compiler doesn't support C++11, because it header from that standart. I add code for version with std::vector, try it.
@user1837009, Do you know why simple array int a[100] is not created in each step of recursion? Why I must use vector or smth else insted of int a[]?
This will allocate a new copy of vector a each time the recursive function "go" is called. I have a program that does something similar to this, but then keeps running and does other things afterwards, and memory is an issue. Should I delete the local copy of a ("delete a;") in the function before it returns?
2

It's because you are using pointers, so you write not in the temporary array but in the memory.

If using vector is satisfactory for you, you can write:

#include<iostream>
#include<vector>
using namespace std;

void go(vector<int> a, int x)
{
    a[x] = x;
    for(int i = 1; i <= 5; i++)
        cout << a[i] << " ";
    cout << endl;
    if(x == 3)
        return;
    go(a, x+1);
    for(int i = 1; i <= 5; i++)
        cout << a[i] << " ";
    cout << endl;
}

int main()
{
    vector<int> a(100, 0);

    go(a, 1);
}

Executable version: https://ideone.com/rPgjoG

// Please format your code before posting.

1 Comment

This will allocate a new copy of vector a each time the recursive function "go" is called. I have a program that does something similar to this, but then keeps running and does other things afterwards, and memory is an issue. Should I delete the local copy of a ("delete a;") in the function before it returns?
0

for that you need to delete the values you've put in the array, by adding the line a[x + 1]=0; after the recursion call:

#include<iostream>
using namespace std;
int a[100];
void go (int a[100],int x)
{
     a[x]=x;
     for(int i=1;i<=5;i++)cout<<a[i]<<" ";cout<<endl;
     if(x==3)return;
     go(a,x+1);
     a[x + 1]=0;
     for(int i=1;i<=5;i++)cout<<a[i]<<" ";cout<<endl;
}
main()
{
     go(a,1);
}

also you don't need an array of 100, or an array at all, you can just do:

#include<iostream>
using namespace std;
void go (int x)
{

     for(int i=1;i<=5;i++)cout<<(i <= x ? i : 0);
     cout << endl;
     if(x==3)return;
     go(x+1);
     for(int i=1;i<=5;i++)cout<<(i <= x ? i : 0);
     cout << endl;
}
main()
{
     go(1);
}

7 Comments

yes but i dont want to delete it, i want the code to create its array 'a' for all the recursive steps. i dont want to restore the array myself -.-
@AnimeaAnimea if you are going to print the array after putting values in it then you are going to print the values in it. you've put the values "2" and "3" in the array so if you want to print them, print them. if not, delete them. you can't save the values in the array and expect they won't be there
my code remembers value of x in each step ; i want it also to remember the array in each step (like x) and when i 'return' use it.
@AnimeaAnimea you can copy the array to a new array and send the new one. is that good to you? would you like an example of that?
i want to do it with only one array :)
|

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.