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

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




c++ programming help

 
Reply to this topicStart new topic

c++ programming help, how to calculate the intermediate value ??

tikotiko
14 Oct, 2007 - 09:17 AM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
Consider the following process, which can be applied to any positive integer: if the integer is odd, multiply it by 3 and add 1; if the integer is even, divide it by 2. This process is repeated until the integer remaining is 1. As an example, consider 34, the process terminates in 13 steps, producing the following sequence: 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1. The process stops when 1 occurs. Write a program that receives an integer from a user and determines the number of steps it takes for termination and the largest intermediate value generated ( 13 and 52 for the example respectively).


my code was like that :


CODE

#include <iostream>



using namespace std;

int main()
{
    int num;
    int steps = 0;
    cout << "Please enter a number: ";
    cin >> num;
    
    while(num >1)
    {
        cout << num << " ";
        if((num % 2) != 0)    
        {
            num *= 3;
            num++;
        }
        else
            num /= 2;
            ++steps;
    }
    cout << num << endl;
    cout << "Number of steps: " << steps << endl;

    system("PAUSE");
    return 0;    
}

User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: C++ Programming Help
14 Oct, 2007 - 09:19 AM
Post #2

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
Please post the problem you are facing smile.gif.
And welcome to </DIC> smile.gif.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: C++ Programming Help
14 Oct, 2007 - 11:47 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
As far as I can tell the program works.

Here is my overly fancy recursive version of the program (I am avoiding doing work, so this was a mild distraction).

CODE
#include <iostream>

using namespace std;

int find1(int x);

int main()
{
    int num;
    int steps = 0;
    cout << "Please enter a number: ";
    cin >> num;    
    steps = find1(num);
    cout << "Number of steps: " << steps << endl;
    system("PAUSE");
    return 0;    
}


int find1(int x) {
    cout << x <<" ";
    return (x > 1)  ? find1((x % 2) ? (x * 3 + 1) : (x / 2 )) + 1 : 0;
}

User is offlineProfile CardPM
+Quote Post

tikotiko
RE: C++ Programming Help
14 Oct, 2007 - 06:14 PM
Post #4

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
right , it is working for the number of steps but I didn't know how to get the intermediate value,,any help would be appreciated....
User is offlineProfile CardPM
+Quote Post

aceofspades686
RE: C++ Programming Help
14 Oct, 2007 - 08:45 PM
Post #5

D.I.C Regular
Group Icon

Joined: 8 Oct, 2007
Posts: 261


Dream Kudos: 100
My Contributions
Looks to me by this would be most easily accomplished by creating a new variable that holds the highest intermediate value thus far, and compare it to the current number on each run through to see if a higher number was achieved in that step. If it was, save it in that variable.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:06PM

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