8 Replies - 1588 Views - Last Post: 29 November 2012 - 03:54 PM Rate Topic: -----

#1 richardm1985  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,966
  • 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.
Was This Post Helpful? 3
  • +
  • -

#3 richardm1985  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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);
}

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,966
  • 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?
Was This Post Helpful? 1
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • 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;


Was This Post Helpful? 1
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • 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

Was This Post Helpful? 3
  • +
  • -

#7 richardm1985  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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 :bigsmile:

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.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • 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)
Was This Post Helpful? 1
  • +
  • -

#9 richardm1985  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1