I have to write a program in C that will take a base b from the user (assuming b is between 2 and 10), a natural number n and then n numbers that represent the digits of some number m in base b. The program should print out what decimal number m was input. For example, if you put b=5 and n=4 and then the numbers 3 ,4, 2 and 1 the program should output 486 because m=3*5^3+4*5^2+2*5^1+1*5^0=486

Note: You can assume that the digits will be the numbers between 0 and b-1.

So here's what I've done:

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

int main(void) {
    int x,n,b,k=0,num=0,i,j;
    scanf("%d", &b);
    scanf("%d", &n);
    for(i=1; i<=n; i++) {
        scanf("%d", &x);
        for(j=1; j<b; j++){
            if(j>k){
                num=num+x*(pow(b,n-j));
                k=j;
                break;
                }
        }
    }

    printf("m=%d", num);

return 0;
}

Can you tell me why this doesn't work for the numbers given in the example above? It outputs 485 instead of 486, while if I take for example b=7, n=3 and then numbers 5, 6 and 1, I get the correct solution m=288.

3

There are 3 answers

0
RoadRunner On BEST ANSWER

I suggest checking the return value of scanf(), Something like this is the right idea:

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

int
main(int argc, char *argv[]) {
    int base, n, i, x, sum = 0, power;

    printf("Enter base: ");
    if (scanf("%d", &base) != 1) {
        printf("Invalid base.\n");
        exit(EXIT_FAILURE);
    }

    printf("Enter n: ");
    if (scanf("%d", &n) != 1) {
        printf("Invalid n.\n");
        exit(EXIT_FAILURE);
    }

    power = n-1;

    printf("Enter numbers: ");
    for (i = 0; i < n; i++) {
        if (scanf("%d", &x) != 1) {
            printf("Invalid value.\n");
            exit(EXIT_FAILURE);
        }
        sum += x * pow(base, power);
        power--;
    }

    printf("Sum = %d\n", sum);

    return 0;
}

Input:

Enter base: 5
Enter n: 4
Enter numbers: 3 4 2 1

Output:

Sum = 486
0
Malcolm McLean On

OK, so given a binary number, we can output a decimal number very easily. Just printf("%d%\n", x);

Next job is to convert a number given digits and base into a binary (machine representation) number.

  int basetointeger(const char *digits, int b)
  {
      assert(b >= 2 && b <= 10);
      // code here

      return answer;
  }

Now hook it all up to main

int main(void)
{
     int x;
     int base;
     char digits[64];  // give more digits than we need, we're not worrying about oveflow yet
     /* enter base *?
     code here
     /* enter digits */
     code here
     x = basetointger(digits, base);
     printf("Number in decimal is %d\n, x);
}
0
Niklas Rosencrantz On

You need some small change to your logic.

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

int main(void) {
    int x, n, b,  num = 0, i;
    scanf("%d", &b);
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        scanf("%d", &x);
        num += x * pow(b, n - i);
    }
    printf("m=%d", num);
    return 0;
}

Test

gcc -Wall main.c -lm
$ ./a.out
5
4
3
4
2
1
m=486  

Test 2

 ./a.out
7
3
5
6
1
m=288