Write a recursive function to print the contents of a vector

Page 1 of 1

8 Replies - 8767 Views - Last Post: 29 November 2012 - 03:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=301986&amp;s=e7b67b34e42dde137c8256f16f47e173&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 richardm1985

Reputation: 4
• Posts: 31
• Joined: 28-November 12

Write a recursive function to print the contents of a vector

Posted 28 November 2012 - 06:49 PM

Hi guys,

Recently I bought C++ Primer 5th Edition and this sh*t f**k book, which suprisingly provides questions...WITHOUT answers! So progressing through chapters unsure if I am doing the exercises correctly. I have gotten to an exercise asking me to:

"Write a recursive function to print the contents of a vector."

I have searched the web for days but nothing is exactly what my question is...so I really need help on this one please.

```int temp[] = {0,1,2,3,4,5,6,7,8,9};
vector<int>vec (begin(temp), end(temp));

int recursive (vector<int> val)
{
if (val.begin() != val.end())

return recursive(val - 1) * val;
return 1;
}

int main()
{
recursive(vec);
}
```

Is This A Good Question/Topic? 0

Replies To: Write a recursive function to print the contents of a vector

#2 #define

• Duke of Err

Reputation: 1857
• Posts: 6,684
• Joined: 19-February 09

Re: Write a recursive function to print the contents of a vector

Posted 28 November 2012 - 07:17 PM

Like a loop, a recursive function usually has a condition that ends the loop/recursion.

So you want to pass the vector and an index or iterator.

```int recursive (vector<int> numbers, int index);

```

The index will be tested against the size of the vector.

#3 richardm1985

Reputation: 4
• Posts: 31
• Joined: 28-November 12

Re: Write a recursive function to print the contents of a vector

Posted 28 November 2012 - 07:35 PM

Hey Define!

Thank you so much, here is my new code which works:

```int temp[] = {0,1,2,3,4,5,6,7,8,9};
vector<int>vec (begin(temp), end(temp));

int recursive (vector<int> val, int index)
{
if (index > 1)
{
int temp = val[index];
temp *= val[index];
cout << temp << endl;

return recursive(val,index -1);
}
return 1;
}

int main()
{
int num;
cin >> num;

recursive(vec,num);
}
```

#4 #define

• Duke of Err

Reputation: 1857
• Posts: 6,684
• Joined: 19-February 09

Re: Write a recursive function to print the contents of a vector

Posted 28 November 2012 - 08:01 PM

Is it ok that it doesn't print for indexes 0 and 1?

#5 CTphpnwb

• D.I.C Lover

Reputation: 3810
• Posts: 13,838
• Joined: 08-August 08

Re: Write a recursive function to print the contents of a vector

Posted 28 November 2012 - 09:15 PM

And why are you squaring the value?
```temp = value;
temp *= value;

```

is the same as:
```temp = value * value;
```

Why not do this?
```cout << val[index] << endl;

```

#6 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

Re: Write a recursive function to print the contents of a vector

Posted 28 November 2012 - 10:02 PM

Quote

int recursive (vector<int> val, int index)
{
if (index > 1)
{
int temp = val[index];
temp *= val[index];
cout << temp << endl;

return recursive(val,index -1);
}
return 1;
}

Any time you do recursion you should be passing your data structures by reference - or const reference. Every call to a recursive function will need to allocate a new stack frame. If you are passing your data structures by value, then copies will be made for every stack frame.

Say you had ten thousand values in your vector, then your function would require the vector to be copied ten thousand times simply do display its contents - sounds a little inefficient right?

i.e.
```#include <iostream>
#include <vector>

const int SIZE = 5;

void printVec(const std::vector<int> &vec, int size) {
if(--size)
printVec(vec, size);
std::cout<<vec[size]<<std::endl;
}

int main() {
int vals[SIZE] = {1, 2, 3, 4, 5};
std::vector<int> ints(vals, vals + SIZE);

printVec(ints, SIZE);
return 0;
}

```

Not really a big deal in this case, but it's good to know.

This post has been edited by jjl: 28 November 2012 - 10:15 PM

#7 richardm1985

Reputation: 4
• Posts: 31
• Joined: 28-November 12

Re: Write a recursive function to print the contents of a vector

Posted 28 November 2012 - 10:47 PM

Thanks guys for all your help

Here is what I have done so far:

- changed the vector data structure to a const reference.
- removed the extra "temp"
- allowed program to print for index 1

```int ret = 1;

int recursive (const vector<int> &val, int index)
{
while (index)
{
ret *= val[index];
cout << ret << endl;

return recursive(val, index - 1);
}
return 1;
}

int main()
{
int temp[] = {0,1,2,3,4,5,6,7,8,9};
vector<int>vec (begin(temp), end(temp));

int num;
cin >> num;

recursive(vec,num);
}
```

Thanks guys I feel confident to move on lol

Quote

std::vector<int> ints(vals, vals + SIZE);
, It works just fine, but I'm sticking to the begin(),end() stuff for now till I finish this book.

#8 CTphpnwb

• D.I.C Lover

Reputation: 3810
• Posts: 13,838
• Joined: 08-August 08

Re: Write a recursive function to print the contents of a vector

Posted 29 November 2012 - 06:20 AM

I must not be understanding what you're trying to do. I made this change to your code:
```		cout << ret << "\t" << val[index] << endl;
```

and got this output:
```9 // My input
9	9
72	8
504	7
3024	6
15120	5
60480	4
181440	3
362880	2
362880	1

```

The second column is the actual values in the vector. The first is the result of multiplying the value by the previous values. Is that what you intend? It's not what your initial post says:
"Write a recursive function to print the contents of a vector."

Oh, and notice that your condition still doesn't show vec[0] at all. Instead of while(index) you should use if(index >=0)

#9 richardm1985

Reputation: 4
• Posts: 31
• Joined: 28-November 12

Re: Write a recursive function to print the contents of a vector

Posted 29 November 2012 - 03:54 PM

Thanks CT! that was great changing the while to if did print index 0.

The examples in the book have a number, lets say 5, to go through a recursive loop that should do 5 x 4 x 3 x 2 x 1.

But on line 9 and 10 (of your post) I also can't make the values go beyond 362880.