Decimal to octal conversion

Asked

Viewed 1,906 times

3

I need to create an algorithm that converts decimal to octal, but my ta converting wrong, someone can help me as it would be right?

  #include <stdio.h>  

int decim(int n)    
{    
    int i,a;    
    int octal;    


    if(n<=7)  
    {  
        octal=n;  
    }  

    else    
    {  

        while(n>=8 )   
            {       
                a=n%8;  
                n=n/8;  
            }  
            n=n%8;

    }

return octal;

}


int main()  
{
    int n,octal;  

    printf("Informe um numero decimal:");  
    scanf("%d",&n);  

    octal=decim(n);  

    printf(" o numero octal eh: %d\n",octal);    



    return 0;    
}

3 answers

5


The code you have does not use the variable octal which is the one you use to return the calculated value, so you will always give the random value you pick up in memory, unless you enter the first case of n<=7.

Nor is it using the a which corresponds to each of the digits that will form the value in octal, with the particularity of them being obtained from right to left.

Trying to make the most of the code you already have, and letting it work, you need to:

  • initialize the octal and increase its value with each digit it interprets
  • initialize the i and uses it as base factor 10 to create the digits in reverse.

Implementing these points I mentioned would look like this:

int decim(int n) {
    int i = 1,a; //i inicializado a 1
    int octal = 0; //octal inicializado a 0

    if(n<=7) {
        octal=n;
    }
    else {
        while(n>=8) {
            a=n%8;
            n=n/8;
            octal = octal + a * i; //calculo do octal
            i *= 10; //aumento do fator de base 10
        }
        n=n%8;
        octal = octal + n * i; //mesmo calculo aqui
    }

    return octal;
}

See code working on Ideone

Naturally the code can be more compact and optimized, but I tried my best to use what I already had instead of rewriting the logic.

  • 1

    I got it, bro, I forgot to multiply by 10 and initialize the octal and i, vlw ai

2

Missing to store the leftovers of the division in some array (inside your Else the value of the octal variable is not changed) and then invert to show the result:

#include <stdio.h>

void decimal_to_octal(int num) {
    int result[15] = {};
    int i = 0, j = 0;

    if(num >= 8) {
        printf("Decimal: %d -> Octal: ", num);

        while(num >= 8) {
            result[i++] = num % 8;
            num = num / 8;  
        }
        result[i] = num;

        // Imprimindo o valor invertido sem os zeros à esquerda
        for(j = i; j >= 0; j--) {
            printf("%d", result[j]);
        }
        printf("\n");
    } else {
        printf("Decimal: %d -> Octal: %d\n", num, num);
    }
}

int main(void) {

    decimal_to_octal(7); // 7
    decimal_to_octal(10); // 12
    decimal_to_octal(568); // 1070
    decimal_to_octal(3578); // 6772
    return 0;
}

You can check the solution on ideone.

0

The problem is the inside of the "while".

At each step, you must add to the previous result (variable octal), the digit found by the rest of the current division, offset in its proper position (exponent: 10 , 10¹, 10², etc.).

The algorithm would be:

while n!=0 {
            octal = octal + i . n%8
            i = i . 10
            n = n/8
            }

Initializing octal=0 and i=1

In this case, there is no need for "if(n<=7).

TESTING:

                 | octal |    i   |   n   |
inicialização    |     0 |      1 |  5349 |
1º passo (n!=0)  |     5 |     10 |   668 |
2º passo (n!=0)  |    45 |    100 |    83 |
3º passo (n!=0)  |   345 |   1000 |    10 |
4º passo (n!=0)  |  2345 |  10000 |     1 |
5º passo (n!=0)  | 12345 | 100000 |     0 |
6º passo (n=0)          sai do while

                 | octal |    i   |   n   |
inicialização    |     0 |      1 |     5 |
1º passo (n!=0)  |     5 |     10 |     0 |
2º passo (n=0)          sai do while

                 | octal |    i   |   n   |
inicialização    |     0 |      1 |     0 |
1º passo (n=0)          sai do while

Browser other questions tagged

You are not signed in. Login or sign up in order to post.