I hope you didn't have to help me that much since I'm the one trying to solve it, but thanks anyway. I got it to work.
The reason I used separate functions to get the inputs for i and j was that I don't know how to return a value by reference and had to call those functions to get the values for i and j.
And as for the function for keeping the window open; that's really just in case I run the program by double-clicking the .exe file. Without that function there, when I run a program like that, I can't see the full output before the window closes. In a program that only prints to the screen, the console window would just flash on my screen and I won't get to see a thing. In a program like this one, I won't be able to see the result before the window closes.
And yeah, the time limit is 3 seconds. But when does it start? They shouldn't know I'm trying to solve it, should they? Or does the site know somehow?
Here's the updated code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
// Osman Zakir
// 10 / 16 / 2016
// Program to determine the maximum cycle length of n
// for the 3n+1 problem
#include <iostream>
int get_value_i();
int get_value_j();
int determine_cycle_length_n(int n);
int maximum_cycle_length_n(int start, int end);
void keep_window_open();
int main()
{
int i = get_value_i();
int j = get_value_j();
int cycle_length_n = maximum_cycle_length_n(i, j);
std::cout << i << ", " << j << ", " << cycle_length_n << "\n";
keep_window_open();
}
int get_value_i()
{
using namespace std;
cout << "Enter an integer: ";
int num = 0;
cin >> num;
cin.ignore(32767, '\n');
return num;
}
int get_value_j()
{
using namespace std;
cout << "Enter another integer: ";
int num = 0;
cin >> num;
cin.ignore(32767, '\n');
return num;
}
int maximum_cycle_length_n(int start, int end)
{
int maximum_length = 0;
for (int i = start; i < end; i++)
{
int cycle_length = determine_cycle_length_n(i);
if (cycle_length > maximum_length)
{
maximum_length = cycle_length;
}
}
return maximum_length;
}
int determine_cycle_length_n(int n)
{
int cycle_length = 1;
while (n != 1)
{
if (n % 2 != 0)
{
n = (3 * n) + 1;
cycle_length++;
}
else
{
n /= 2;
cycle_length++;
}
}
return cycle_length;
}
void keep_window_open()
{
using namespace std;
cin.clear();
cout << "Please enter a character to exit:\n";
char ch;
cin >> ch;
}
|
By the way, for C programs, when I compile using Clang (I found a good way to not get a Linker error about the iostream header, so I can do it from the Command Prompt now), it gives me a warning about a newline missing at the end the file (after the last closing brace). Why does it want a newline at the end of the file?
Edit: I submitted it just now.
But I'm worried about the compiler and ISO Standard version they're using for C++. Mine was generated by Clang using the 2014 standard, but they have GNU GCC with the 2011 standard as the highest. I also used Microsoft's Visual C++ compiler to do it once or twice as well, but mainly I used Clang for it. What'll happen with this?
Edit2: I submitted it, but it was rejected for being incorrect. Should I ask them what went wrong?