Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   decimal to octal on C++ (http://www.go4expert.com/forums/decimal-octal-cpp-t23240/)

compscichick 8Sep2010 01:02

decimal to octal on C++
 
The questions is: obtain the number of divisions performed (converting decimal to octal) to get the octal form.

I have a c++ program that already does decimal to octal, but idk how to show how many divisions (or times the program goes through the algorithm). Any help?

LordN3mrod 8Sep2010 01:22

Re: decimal to octal on C++
 
easiest way - store a global variable, say numOfDiv. Every time you perform a division, increment the variable. That's all :)

compscichick 8Sep2010 09:52

Re: decimal to octal on C++
 
Quote:

Originally Posted by LordN3mrod (Post 72524)
easiest way - store a global variable, say numOfDiv. Every time you perform a division, increment the variable. That's all :)


could you give an example...it doesn't have to pertain to my solution, its just been over a year since I have been programming and I cant find anything on google search

LordN3mrod 8Sep2010 14:25

Re: decimal to octal on C++
 
OK, I'll write a program that does, ermm... say, prime-factorization

Code:

#include <iostream>
int main()
{
    using namespace std;
    int n;
    cin >> n;
    cout << n << " = 1" ;
    for(int i = 2; i * i <=n ;++i)
    {
        while(n%i == 0)
        {
            cout  << " * " << i;
            n /= i; //n = n/i
        }
    }
    if(n > 1)
        cout <<" * " << n;
    cout << endl;
    cin.get();
}

So, if you wanna know how many times operator / was called do it like this

Code:

#include <iostream>
int divCount = 0;
int main()
{
    using namespace std;
    int n;
    cin >> n;
    cout << n << " = 1" ;
    for(int i = 2; i * i <=n ;++i)
    {
        while(n%i == 0)
        {
            cout  << " * " << i;
            n /= i;// DIVISION HERE!!! INCREMENT DIVCOUNT
            ++divCount;
        }
    }
    if(n > 1)
        cout <<" * " << n;
    cout << endl << divCount << endl;
    cin.get();
}

You may rightfully think that % is also division, and you might wanna count them too. Like this

Code:

#include <iostream>
int divCount = 0;
int main()
{
    using namespace std;
    int n;
    cin >> n;
    cout << n << " = 1" ;
    for(int i = 2; i * i <=n ;++i)
    {
        int rem = n%i; //DIVISION HERE INCREMENT divCount;
        ++divCount;
        while(rem == 0)
        {
            cout  << " * " << i;
            n /= i;// DIVISION HERE!!! INCREMENT DIVCOUNT
            ++divCount;
            rem = n%i; //DIVISION HERE INCREMENT divCount;
          ++divCount;
        }
    }
    if(n > 1)
        cout <<" * " << n;
    cout << endl <<divCount << endl;
    cin.get();
}

Hope this helped
Best Regards

compscichick 8Sep2010 22:22

Re: decimal to octal on C++
 
Code:

#include <iostream>
using namespace std;
int numOfDiv;

void dec2oct(int n) {
       
        int leastSig=0;
        int mostSig=0;

        leastSig=n%8;
        ++numOfDiv;
        mostSig=n/8;
        ++numOfDiv;
        if(mostSig>7){
                dec2oct(mostSig);
        }
        else{
                cout<<mostSig;
        }

        cout<<leastSig;
       
}

int main(){
        int num = 35;
        cout<< numOfDiv << endl;
        cout << oct << num << endl;
        cout << dec;
        dec2oct(num);
        int x;
        cin>>x;
        return 0;

}

it never counts, I just get a return output of 0. Is there something I am missing?

shabbir 9Sep2010 09:16

Re: decimal to octal on C++
 
You are outputting that variable before increment and then the program ends. Variables do not have persistency i.e. store values across runs.

LordN3mrod 9Sep2010 14:48

Re: decimal to octal on C++
 
instead of the final int x; cin >> x; write just
Code:

cin.get();
Also, move the line
Code:

cout<< numOfDiv << endl;
after the line
Code:

dec2oct(num);
And everything will be fine :)


All times are GMT +5.5. The time now is 04:38.