# NEED HELP!

Page 1 of 1

## 1 Replies - 720 Views - Last Post: 21 April 2008 - 04:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=49620&amp;s=6cfd527eb2453e680327307f76e482d5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Vimrod

Reputation: 0
• Posts: 20
• Joined: 27-March 08

# NEED HELP!

Posted 21 April 2008 - 04:43 AM

```#include <iostream>
#include <vector>
using namespace std;

int syracuseNext(int n);
double larger(double x, double y);

int main()
{
int choice;
cout << "Press 0 to display a sequence" << endl;
cout << "Press 1 to search an interval for the biggest altitude" << endl;
cout << "Press 2 to search an interval for the longest flight time" << endl;
cout << "Enter your choice: " << flush;
cin >> choice;

int n;
double seed;
double max;
int evens = 0;
int odds = 0;
int zeros = 0;
int number;
int a, b, c, i, j, k, m = 0, p = 0;
vector<int> vec,kelly,cat;

if(choice == 0)
{cout << "Enter the seed: " << flush;
cin >> n;

{cout << "The next terms are: ";
for( int seed = n; seed != 1;)
{
seed = (seed % 2 == 0) ? (seed /2) : ((seed * 3) + 1);
cout << seed << " ";
max = larger(max, seed);

switch (seed % 2 == 0)
{
case 0:
evens++;
if (number == 0)
zeros++;
break;
case 1:
case -1:
odds++;
}

}
cout << endl;
}
cout << "The altitude is " << larger(max, seed) << endl;
cout << "The flight time is " << evens + zeros + odds << endl;
}
else if(choice == 1)
{cout << "Enter two positive integers a and b such that a <= b: " << flush;
cin >> a >> b;

{cout << "The seeds with biggest altitude in " << "[" << a << ", " << b << "] " << "are: ";
for (c = a; c <= b; ++c)
{
vec.push_back(c);
k = 0, j = 0;
for (i = c; i > 1; i = i)
{
if(i % 2) i = 3 * i + 1;
else i = i / 2;
++j;
if (k < i) k = i;

}
kelly.push_back(k);
cat.push_back(j);

if (m < k)
{
m = k;
n = c;
}
if (p < j)
p = j;
}
for (int x = 0; x != vec.size(); ++x)
{

if( kelly[x] == m)
cout << vec [x] << " ";

}
cout << "and the altitude is " << m << endl;
}
}

else if(choice == 2)
{cout << "Enter two positive integers a and b such that a <= b: " << flush;
cin >> a >> b;

for (c = a; c <= b; ++c)
{
vec.push_back(c);
k = 0, j = 0;
for (i = c; i > 1; i = i)
{
if(i % 2) i = 3 * i + 1;
else i = i / 2;
++j;
if (k < i) k = i;

}
kelly.push_back(k);
cat.push_back(j);

if (m < k)
{
m = k;
n = c;
}
if (p < j)
p = j;
}

cout <<"The seed with longest flight time in " << "[ " << a << ", " << b << "]" << " is: ";
for (int x = 0; x != vec.size(); ++x)
{
if(cat[x] == p)
cout << vec [x]<< " ";
}
cout << " and the flight time is " << p << endl;
}

system("pause");
return 0;
}

double larger(double x, double y)
{
if (x >= y)
return x;
else
return y;
}

```

Okay, let me explain what is happening here.

This program is suppose to compile to let the user enter 0 ,1 or 2.

If 0 is entered it will ask the user to enter a positive seed and the program will output all the subsequent terms of the sequence until it reaches 1.
It will also display the altitude (largest seed in the set of terms) and the flight time(total number of terms).

If the user enters 1 or 2, the program will ask the user to enter two integers a and b with a<=b.
If the number entered was 1, the program will search the interval [a, b] (a and b included) for the seed that gives the biggest altitude. It will output that seed together with its altitude. In case several seeds in the interval have the same biggest altitude, all the seeds should be displayed in increasing order with a proper format.
If the number entered was 2, the program will search the interval [a, b] (a and b included) for the seed that gives the longest flight time. It will output that seed together with its flight time.

I've managed to do the 0 choice right but once I place vector in the program, because I needed it to do choices 1 and 2, it screws up choice 0.

The outputs for mine are:

Press 0 to display a sequence
Press 1 to search an interval for the biggest altitude
Press 2 to search an interval for the longest flight time
Enter the seed: 15
The next terms are: 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
The altitude is 7.74335e+268
The flight time is 17
Press any key to continue . . .

Press 0 to display a sequence
Press 1 to search an interval for the biggest altitude
Press 2 to search an interval for the longest flight time
Enter two positive integers a and b such that a <= b: 10 30
The seeds with biggest altitude in [10, 30] are: 27 and the altitude is 9232
Press any key to continue . . .

Press 0 to display a sequence
Press 1 to search an interval for the biggest altitude
Press 2 to search an interval for the longest flight time
Enter two positive integers a and b such that a <= b: 1 10
The seed with longest flight time in [ 1, 10] is: 9 and the flight time is 19
Press any key to continue . . .

Suppose to be:

Press 0 to display a sequence
Press 1 to search an interval for the biggest altitude
Press 2 to search an interval for the longest flight time
Enter the seed: 15
The next terms are: 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
The altitude is 170
The flight time is 17
Press any key to continue . . .

And for choices 1 and 2, the outputs both matches.

How do I manage to do this without using vetor but just using #include<iostream>?
Desperately need help! Thanx in advance!

This post has been edited by Vimrod: 21 April 2008 - 09:24 PM

Is This A Good Question/Topic? 0

## Replies To: NEED HELP!

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: NEED HELP!

Posted 21 April 2008 - 04:48 AM

I don't have any advice yet on the program, but this irritates the living hell out of me:

```if(choice == 0)
{cout << "Enter the seed: " << flush;

```

Don't EVER do that! (putting the brace on the same line). Do this:

```if(choice == 0){
cout << "Enter the seed: " << flush;

//OR

if(choice == 0)
{
cout << "Enter the seed: " << flush;

```

This post has been edited by KYA: 21 April 2008 - 04:48 AM