Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm writing a c++ program that will print a hailstone sequence and some stats about it using recursion, no loops, and no arrays. Whenever I

I'm writing a c++ program that will print a hailstone sequence and some stats about it using recursion, no loops, and no arrays. Whenever I run my code I get the results of my first 2 methods but then i get this error:

makefile:12: recipe for target 'run' failed make: *** [run] Segmentation fault

Can anyone tell me what i'm doing wrong? Here's my code"

// **This program will calculate and print the hailstone sequence of any given integer**

#include

#include

using namespace std;

//For any given integer("n"), this method calculates the next

//integer in a hailstone sequence based on whether n is even

//or odd.

//@param n A given integer.

//@return next The following integer in a hailstone

//sequence beginning with "n".

int nextNum(int n){

int next = n;

if(next % 2 == 0){

next = next / 2;

}

else {

next = next * 3 + 1;

}

return next;

}

//This method echoes the hailstone sequence from beginning

// integer "n" to 1.

//@param n A given integer

void hPrint(int n){

int currentNum = n;

if (currentNum == 1){

printf("%i ", currentNum);

}

else{

printf("%i ", currentNum);

hPrint(nextNum(currentNum));

}

}

//This method will count the integers in a given hailstone

//sequence and return the length of the hailstone sequence.

//@param n A given number

//@return length Length of the hailstone sequence

int hLength(int n)

{

int length = 1;

int currentNum = n;

if(currentNum != 1){

currentNum = nextNum(currentNum);

length++;

}

else{

currentNum = hLength(nextNum(currentNum));

}

return length;

}

//This method will compare every integer in a given

//hailstone sequence and return the largest integer

//@param n A given number

//@return large Largest number in the hailstone sequence

int largestLength(int n)

{

int number = nextNum(n);

int length = 0;

int currentNum = 0;

if(n == 1)

{

length = 1;

}

else

{

currentNum = largestLength(number);

if (currentNum > n)

{

length = currentNum;

}

else

{

length = n;

}

}

return length;

}

//This method will return the integer between n and 1 from which

// the longest hailstone sequence can be created

//@param n an integer

//@return longesNum The integer between n and 1 that starts a longer hailstone sequence than the other integers between n and 1.

int longestSeqStart(int n)

{

int longestLength = largestLength(n);

int longestNum = 0;

int count = n;

if(longestLength < largestLength(count) && count >= 0)

{

longestLength = largestLength(count);

longestNum = count;

}

else{

longestLength = largestLength(count);

longestNum = longestSeqStart(count);

}

return longestNum;

}

//This method will return the length of the longest hailstone sequence

//up to a given number

//@param n an integer

//@return longestLength The length of the largest hailstone sequence beginning with every number between n and 1.

int longestSeq(int n)

{

int longestLength = largestLength(n);

int longestNum = 0;

int count = n;

if(longestLength < largestLength(count) && count >= 0)

{

longestLength = largestLength(count);

longestNum = count;

}

else{

longestLength = largestLength(count);

longestNum = longestSeqStart(count);

}

return longestLength;

}

int main()

{

int n = 0;

printf("Please type an integer, followed by the enter key. ");

scanf("%i", &n);

hPrint(n);

printf("length: %i largest: %i ", hLength(n), largestLength(n));

printf("start: %i ", longestSeqStart(n));

printf("longest: %i ", longestSeq(n));

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

9. Make sure goals are internalized and accepted by the athlete.

Answered: 1 week ago

Question

1. Describe the factors that lead to productive conflict

Answered: 1 week ago