How can I use recursion instead of loops? For the functions? #include <cstdio> #
ID: 3666441 • Letter: H
Question
How can I use recursion instead of loops? For the functions?
#include <cstdio>
#include <iostream>
using namespace std;
//======================================================================
// next(n) returns the number that follows n in the hailstonesequence.
// For example, next(7) = 22 and next(22) = 1 and next(1) = 1.
//======================================================================
int next(int n)
{
if (n == 1)
{
return n;
}
else if (n%2==0)
{
return n=n/=2;
}
else
{
return n=3*n+1;
}
}
//======================================================================
// sequence(n) takes the integer n and outputs the integer in the
// hailstorm sequence starting with the user input and ends when the
// sequence equals 1.
//======================================================================
void sequence(int n)
{
cout << n << " ";
if(n!=1)
{
n=next(n);
sequence(n);
}
}
//======================================================================
// lenght(n) takes the integer n and counts all the numbers that are in
// the hailstorm sequence and adds them up.
//======================================================================
int length(int n)
{
if ( n == 1) return 1;
else return 1 + length(next(n));
}
//======================================================================
// large(n) takes the integer n and checks to see if the number islarger
// than n and then returns the largest number in the hailstormsequence
//======================================================================
int largest(int n)
{
int maxint=1;
while(n!=1)
{
if(maxint < n) maxint = n;
n = next(n);
}
return maxint;
}
int main(int argc, char** argv)
{
int n;
printf("What number shall I start with? ");
scanf("%i", &n);
printf("The hailstone sequence is starting with %i is ", n);
sequence(n);
printf(" ");
printf("The lenght of the sequence is %i ", length(n));
printf("The largest number in the sequence is %i ", largest(n));
printf("The longest hailstone sequence starting with a number up to %i has length %i. ", n, length(n));
printf("The longest hailstone sequence starting with a number up to %i begins with %i. ", n, largest(n));
return 0;
}
Explanation / Answer
int largest(int n) is the only function where we are using loops , the following is the recurssive version of it
code with loop:
int largest(int n)
{
int maxint=1;
while(n!=1)
{
if(maxint < n) maxint = n;
n = next(n);
}
return maxint;
}
code:
int largest1(int n) //largest of the sequence that start with n
{
if(n==1) // if n ==1 , then it have only one element 1, return it
return 1;
// else, largest of the sequence = maximum(n, largest of the sequence that start with next(n))
int maxint = largest1(next(n)); // largest of the sequenc that start with next(n) and store it in maxint
return maxint>n? maxint:n; // return maximum of n, maxint
}
Total code with both recursion and loops :
#include <cstdio>
#include <iostream>
using namespace std;
//======================================================================
// next(n) returns the number that follows n in the hailstonesequence.
// For example, next(7) = 22 and next(22) = 1 and next(1) = 1.
//======================================================================
int next(int n)
{
if (n == 1)
{
return n;
}
else if (n%2==0)
{
return n=n/=2;
}
else
{
return n=3*n+1;
}
}
//======================================================================
// sequence(n) takes the integer n and outputs the integer in the
// hailstorm sequence starting with the user input and ends when the
// sequence equals 1.
//======================================================================
void sequence(int n)
{
cout << n << " ";
if(n!=1)
{
n=next(n);
sequence(n);
}
}
//======================================================================
// lenght(n) takes the integer n and counts all the numbers that are in
// the hailstorm sequence and adds them up.
//======================================================================
int length(int n)
{
if ( n == 1) return 1;
else return 1 + length(next(n));
}
//======================================================================
// large(n) takes the integer n and checks to see if the number islarger
// than n and then returns the largest number in the hailstormsequence
//======================================================================
//code with loop
int largest(int n)
{
int maxint=1;
while(n!=1)
{
if(maxint < n) maxint = n;
n = next(n);
}
return maxint;
}
//code with recursion
int largest1(int n) //largest of the sequence that start with n
{
if(n==1) // if n ==1 , then it have only one element 1, return it
return 1;
// else, largest of the sequence = maximum(n, largest of the sequence that start with next(n))
int maxint = largest1(next(n)); // largest of the sequenc that start with next(n) and store it in maxint
return maxint>n? maxint:n; // return maximum of n, maxint
}
int main(int argc, char** argv)
{
int n;
printf("What number shall I start with? ");
scanf("%i", &n);
printf("The hailstone sequence is starting with %i is ", n);
sequence(n);
printf(" ");
printf("The lenght of the sequence is %i ", length(n));
printf("The largest number in the sequence is %i ", largest(n));
printf("The largest number in the sequence is %i ", largest1(n));
printf("The longest hailstone sequence starting with a number up to %i has length %i. ", n, length(n));
printf("The longest hailstone sequence starting with a number up to %i begins with %i. ", n, largest(n));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.