Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,086 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,571 people online right now. Registration is fast and FREE... Join Now!




NEED HELP!

 
Closed TopicStart new topic

NEED HELP!, C++ Programming

Vimrod
21 Apr, 2008 - 03:43 AM
Post #1

New D.I.C Head
*

Joined: 27 Mar, 2008
Posts: 20

CODE

#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 your choice: 0
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 your choice: 1
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 your choice: 2
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 your choice: 0
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 Apr, 2008 - 08:24 PM
User is offlineProfile CardPM
+Quote Post

KYA
RE: NEED HELP!
21 Apr, 2008 - 03:48 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,791



Thanked: 91 times
Dream Kudos: 1200
My Contributions
I don't have any advice yet on the program, but this irritates the living hell out of me:

cpp

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


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

cpp

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 Apr, 2008 - 03:48 AM
User is online!Profile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 12/1/08 08:15PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month