THe problem is supposed to give the recsive answer and the loop answer. The recusive anwer is always giving 0 - as its answer.and that is inccorrect It is supposed to show the steps and so is the loop answer. Can someone plase help me

#include<iostream>

using namespace std;

float wk7recur (float fnum, int inum);
float wk7loop (float fnum, int inum);

int main()
{
    
  float recurive_answer,loop_answer, num;
  int num1;
  
  
  cout<<endl;
  cout<<" This program  calculates the recusive and interation " << endl; 
  cout<<" version of the problem and displays the answers "<<endl; 
  
  do
  {
  
    recurive_answer = wk7recur( num, num1);
    loop_answer = wk7loop ( num, num1);
 
  cout <<endl <<endl;
  cout << " Please enter two numbers "; 
  
  cin >> num >>  num1;
  cout <<" " << recurive_answer <<" " << loop_answer; 
}while (num !=0);



  cout<<endl<<endl;
  cout << "Press [enter] to exit" <<endl;
  cin.ignore(); //needed because of keyboard input
  cin.get();
  return 0;
}
float wk7recur (float fnum, int inum)
{

  if (inum == 0)
      return 1;
  else
      return (fnum * wk7recur(fnum,inum -1));
  
}
float wk7loop (float fnum, int inum)
{
 for (fnum = 1; fnum <= inum; fnum++)
       
        fnum = inum;
		return fnum;     
}

I think a bigger problem is that the recursive and iterative functions do not do anything remotely similar. If this program is supposed to be comparing two equivalent algorithms that solve the same problem, it is way off target. ;)

What are these functions supposed to be doing?

Where do you give num and num1 a value before you pass it to either the recursive or the iterative process? Shouldn't line 22 and 23 be between line 28 and 29?

Expand line 46 to three lines:
double temp = fnum * wk7recur(fnum,inum -1);
cout << temp << endl;
return temp;

num1 will be an int. It will control how many recursive calls to the recursive function there will be and how many times through the loop you will need to go. To generate a new running total each time through the loop you can use a holding variable and the *= operator.

Basically you are calculating fnum raised to the inum power, or something like that, with either approach.

I think a bigger problem is that the recursive and iterative functions do not do anything remotely similar. If this program is supposed to be comparing two equivalent algorithms that solve the same problem, it is way off target. ;)

What are these functions supposed to be doing?

=Both prgorams are supposed to take two numbers and give the same answer but use different calulations to comeup with the same answer, one recusive and one as a loop. I am not sure which type of loop to use and my recursive satement is not functioing correctly

: )

Where do you give num and num1 a value before you pass it to either the recursive or the iterative process? Shouldn't line 22 and 23 be between line 28 and 29?

Expand line 46 to three lines:
double temp = fnum * wk7recur(fnum,inum -1);
cout << temp << endl;
return temp;

num1 will be an int. It will control how many recursive calls to the recursive function there will be and how many times through the loop you will need to go. To generate a new running total each time through the loop you can use a holding variable and the *= operator.

Basically you are calculating fnum raised to the inum power, or something like that, with either approach.

THanks I have got the recursive part working, now I just need to try to get the correct loop to give me the same answer.

: )

Each function call in the recursive approach decreases inum by 1 until inum is zero. When inum is zero the function stops calling itself. That same control can be written into the control structure of a for loop.

The reursive function first returns 1 at the last of the function calls, when inum equals 0; then returns 1 * fnum from the next to last function call; then returns 1 * fnum * fnum from the second to last function call, then returns 1 * fnum * fnum * fnum from the next function call back, etc. So, if a holding variable, call it temp, started out with a value of 1 then each time you go through a function call you multiply the previous value of temp by fnum. This can be readily built into the body of the for loop using a single line of code and either of two syntaxes.

This article has been dead for over six months. Start a new discussion instead.