4 Replies - 1047 Views - Last Post: 30 April 2008 - 10:33 AM Rate Topic: -----

#1 ma7shykrombkteer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 30-April 08

Recursion in factorial (needs some extras)

Posted 30 April 2008 - 09:48 AM

Here is the question

It is interesting to watch recursion “in action.” Modify the factorial function of, to print
its local variable and recursive call parameter. For each recursive call, display the outputs
on a separate line and add a level of indentation. Do your utmost to make the outputs
clear, interesting and meaningful. Your goal here is to design and implement an output
format that helps a person understand recursion better. Print your output for each of the
following inputs, 5, 6, 7 and turn them in.



here is my code
#include<iostream>
using namespace std;



int main()
{
	int num, onum;
	float fact = 1;

   
	cout<<"Please enter a number: ";
	cin>>num;
	onum = num;
	cout<<endl;

	
	while(num>=1)
	{
		fact = fact*num;		
		num = num-1;
		cout<<"factorial: "<<fact<<endl;
	}

	
	cout<<endl<<"The final factorial of "<<onum<<" is: " <<fact<<endl;
system("pause");
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Recursion in factorial (needs some extras)

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Recursion in factorial (needs some extras)

Posted 30 April 2008 - 09:57 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

While I agree that code has been submitted, it is clear that you have not completed the assignment - what have you tried to do so?



Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 ma7shykrombkteer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 30-April 08

Re: Recursion in factorial (needs some extras)

Posted 30 April 2008 - 10:08 AM

View PostAmadeus, on 30 Apr, 2008 - 09:57 AM, said:

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

While I agree that code has been submitted, it is clear that you have not completed the assignment - what have you tried to do so?



Thanks.


I tried the following:
#include<iostream>
#include<iomanip>
using namespace std;



int main()
{
	int num, onum, w=0;
	float fact = 1;

   
	cout<<"Please enter a number: ";
	cin>>num;
	onum = num;
	cout<<endl;

	
	while(num>=1)
	{
				 w=w+5;
		fact = fact*num;		
		num = num-1;
		cout<<setw(w)<<"factorial: "<<fact<<endl;
	}

	
	cout<<endl<<"The final factorial of "<<onum<<" is: " <<fact<<endl;
system("pause");
	return 0;
}



what else should I do to make it more clear?
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Recursion in factorial (needs some extras)

Posted 30 April 2008 - 10:31 AM

Perhaps you can add an output statement - something like "Factorial function is being called here", and an output statement that specifies the values of each variable being used (factorial, loop iteration, etc...) Also, do you need this as a separate function?
Was This Post Helpful? 0
  • +
  • -

#5 ma7shykrombkteer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 30-April 08

Re: Recursion in factorial (needs some extras)

Posted 30 April 2008 - 10:33 AM

I don't understand you.
Please clarify more.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1