@AssertNull Yes the brackets do not count for non-comment line. When I entered 7, it worked fine. I reworked all of the contacts to explain only what the function does, and not how it does it. I went back to ensure the n value wasn't being changed in all of the functions. If I need n to change, I place it within a different variable
@JamesCherrill I got ride of the else {} from my functions that didn't need it.
Now the biggest issue I'm having is the max function. AssertNull, is there something I messed up here? I couldn't have 'n' = next(n) because it would be changing the parameter, so I just placed it in a different variable, but I can't even get my program to run passed that line now. Code::Blocks isn't showing what the issue is or even a hint to what is wrong.
#include <iostream>
#include <cstdio>
using namespace std;
//Function Prototype Declaration
int next(int), sequence_Length(int), sequence_Max(int), sequence_Comparison(int),
initialcomp_Num(int);
void hailstone_Sequence(int);
int main (int argc, char** argv)
{
int user_Input;
cout << "What number shall I start with? ";
cin >> user_Input;
cout << user_Input << "\n";
cout << "The hailstone sequence starting at " << user_Input << " is: " << endl;
hailstone_Sequence(user_Input);
cout << "\nThe length of the sequence is " << sequence_Length(user_Input);
cout << ".\n";
cout << "The largest number in the sequence is " << sequence_Max(user_Input);
cout << ".\n";
cout << "The longest hailstone sequence starting with a number up to " << user_Input << " has length "
<< sequence_Comparison(user_Input) << ".\n";
cout << "The longest hailstone sequence starting with a number up to " << user_Input << " begins with ";
cout << initialcomp_Num(user_Input) << ".\n";
}
/*
//next(int n)
//Returns a number
//that follows 'n' in the sequence.
*/
int next(int n)
{
if (n > 1)
{
if ((n % 2) == 0)
{
return n / 2;
}
else
{
return 3 * n + 1;;
}
}
return n;
}
/*
//hailstone_Sequence(int n)
//Prints the entire sequence beginning with 'n'.
*/
void hailstone_Sequence (int n)
{
//Variable Declaration.
int temp = n;
cout << temp << " ";
if (temp > 1)
{
while (temp != 1)
{
temp = next(temp);
cout << temp << " ";
}
}
}
/*
//sequence_Length(int n)
//Returns the output length of
//the hailstone sequence.
*/
int sequence_Length(int n)
{
//Variable Declaration.
int count, temp = n;
//Determines the length of the sequence until
//'temp' is equal to 1.
for (count = 1; temp != 1; count++)
{
temp = next(temp);
}
return count;
}
/*
//sequence_Max(int n)
//Returns the largest value in the
//hailstone sequence starting at 'n'.
*/
int sequence_Max(int n)
{
//Variable Declaration.
int max = n, temp1;
if ( n <= 1)
{
return n;
}
temp1 = n;
while (temp1 > 1)
{
temp1 = next(n);
//temp2 = temp1;
if(temp1 > max)
{
max = temp1;
}
}
return max;
}
//return max;
/*
//sequence_Comparison(int n)
//Returns the integer with the longest hailstone
//sequence starting from 1 to 'n'.
*/
int sequence_Comparison(int n)
{
//Variable Declaration.
int count, top, aftertop;
if ( n <= 1)
{
return n;
}
top = sequence_Length(n);
for (count = 1; count != n; count++)
{
if (top < sequence_Length(count))
{
aftertop = sequence_Length(count);
top = aftertop;
}
}
return top;
}
/*
//initialcomp_Num(int n)
//Returns the starting value
//of the longest sequence that starts on a value from
//1 to 'n'.
*/
int initialcomp_Num(int n)
{
//Variable Declaration.
int count, initial, topValue, cal;
//Function will work as intended if 'n' is not less than 1.
if (n <= 1)
{
return n;
}
initial = sequence_Length(n);
for (count = 1; count != n; count++)
{
cal = sequence_Length(count);
if (initial < cal)
{
initial = cal;
topValue = count;
}
}
return topValue;
}