#Trouble inputting integers into function

16 messages · Page 1 of 1 (latest)

undone pulsar
#

Working on an assignment where we convert a decimal int into a binary number, back to a decimal, then apply it as a power to a base number.
Problem: When I plug numbers into bin it changes the value of base and power to something else. Like if my input is 5, it changes to 53. Some other tests were 7 changing to 55, 3 changing to 51, and 10 changing to 49. Any ideas why they're changing?
Code:

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

int bin(int x, int y) {
    int t = 0, adder = 1, output = 1, debug = y;

     for (int i = 0; debug != 0; i++) {

         if (debug % 2 != 0) {
             for (int l = 0; l < i; l++) {
                 adder *= 2;
             }
             t += adder; //adder = 2^1
             adder = 1;
         }

         debug /= 2;
     }
     for (int i = 0; i < t; i++) {
         output *= (x - 48);
    }
     return output;
}

int main () {
    int base, power, out, test;
    printf("Enter a base:\n");
    scanf("%ls", &base);
    printf("Base: %ls\n", &base);    

    printf("Enter a power:\n");
    scanf("%ls", &power);
    printf("Power: %ls\n", &power);

    out = bin(base, power);
    printf("%d\n", out);

    test = pow(5, 5);
    printf("%d\n", test);
}
exotic quarry
undone pulsar
#

How does that change it? I’m pretty new to C cause my college does weird classes

#

Also i think it throws a warning at me that it should be ‘%ls’ because it’s an int * type and not just int but I could be misremembering

exotic quarry
#

I think %ls will be reading it as a string. So the number '7' in ascii is 55 so you're getting the ascii representation of the character rather than the integer value

undone pulsar
#

Ahh yeah that makes sense

exotic quarry
#

I don't think you should get a warning but if you do share it here and we can have a look

undone pulsar
#

Okie thanks I’ll try it out tomorrow when I get a chance

exotic quarry
#

👍

undone pulsar
#

That worked, still gives warnings but a warning isn't an error so that's good. I know I'm gonna have another question in like an hour after his office hours so keeping this open for today might be nice

exotic quarry
#

What's the warning?

undone pulsar
#

It was warning that I should use %ls instead of %d because it's an int pointer and not an int, but since I included <math.h> it doesn't seem to be thrown anymore

#

I'm also not sure if this question is applicable here or is too specific

undone pulsar
# undone pulsar

But the program's objective is to split a decimal exponent into a whole number, which is the code above, and just the decimal part, which uses this loop to find a really accurate answer

#

but I don't understand when the middle number goes to the left or right, is it just binary search?