Help with basic C program of the introduction to programming class

Asked

Viewed 81 times

-1

The exercise asks a program to read a binary number in the form of a 'char' vector from the keyboard and return its corresponding to base 10. I wrote a code but the values it returns are wrong, someone can tell me why?

#include <stdio.h>
int main(){
  int expon(int a, int x){
    int i, um;
    um = 1;
    for(i=0; i<x;i++){
      um = um * a;
    };
  return um;
  }


  char number[1024];
  scanf("%s", number);
  int i;
  int n = strlen(number) - 1;
  int novo = 0;
  for(i=0; i<n+1; i++){
    novo = novo + (number[i] * expon(2, n-i));
  }

  printf("%d", novo);

}

Also, I think a few tips on how to make my code more readable wouldn’t go amiss

  • 1

    There’s something very wrong there. You’re declaring a function within the main(). Look, I recommend one more time because I’m missing basic knowledge so it’s hard to help.

  • 1

    In addition to what Shinforinpola said note that number is a char vector but in your account you do not want to treat the numerical representation of the characters but the value 0 or 1 (the character '0' corresponds to decimal 48). Do: new += ((number[i] - '0') * expon(2, n-i));

  • Use #include <string. h> for strlen to be used and in principle it will work

  • Possible duplicate of How to convert binary to decimal?

2 answers

2

Your main problem is that number[i] will contain the value 48 for the 0 and 49 to the 1. The reason is that in the ASCII table, these are the codes for the characters that represent the 0 and the 1. Therefore, you must subtract 48 (or subtract '0') to convert the character to the number.

There are other secondary problems too:

  • The exponentiation function within the main and not outside.

  • scanf with %s without limit, behaving like gets.

  • The exponentiation function can be replaced by a shift-left.

  • novo = novo + blablabla can be simplified to novo += blablabla.

  • You use a -1 when assigning the n to then use a +1 in the for.

Your code goes like this:

#include <stdio.h>

int main() {
    char number[1024];
    scanf("%1023s", number);
    int n = strlen(number);
    int novo = 0;
    for (int i = 0; i < n; i++) {
        novo += ((number[i] - '0') * (1 << (n - i - 1)));
    }

    printf("%d", novo);
}

See here working on IDEONE.

0

Opa Cristus..

it is difficult to understand, I recommend that you review more the basic concept, and think more about the logic...

however see this basic example that converts a binary number to decimal:

    #include<stdlib.h>
    #include<stdio.h>
    #include<math.h>

    int main(){

    int bin, dec = 0, i;

    printf("\nEntre com o numero binario: ");
    scanf("%d", &bin);

    for(i = 0; bin > 0; i++){
        dec = dec + pow(2, i) * (bin % 10);
        bin = bin / 10;
    }
    printf("\nNumero convertido em Decimal: %d\n", dec);
    return 0;
 }

Browser other questions tagged

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