0

Looking for some debugging help. I moved printf statements in and out of the code to see where the problem was coming from. I know that everything prior to this is correct and that the loop executes the proper number of times. However, when the loop gets to the val_1 statement it miscalculates. The first value prints correctly, but the ones after it are incorrect. (Note: I placed a printf statement to see if the computer was at least retrieving the correct values and it wasn't.) Not sure where I went wrong...

Ex. 1234 --> Prints 3 0

Ex. 12345678 --> prints 7 0 0 0

Note: number_of_digits, cc_number, agg_val_1 and val_1 are all declared earlier as int, long long, int and int, respectively. number_of_digits is the number of digits in the cc_number and calculates correctly.

Thanks.

int mod_val = 100;
int n = 10;

for (int i = 0; i < number_of_digits / 2; i++) { 
    val_1 = (cc_number % mod_val)/n;
    agg_val_1 = agg_val_1 + val_1 * 2;
    mod_val = mod_val + 100;
    n = n + 100;
    printf ("%i\n", val_1);
}
1

let's run through the first example:

for (int i = 0; i < number_of_digits / 2; i++) { 
    val_1 = (cc_number % mod_val)/n;
    agg_val_1 = agg_val_1 + val_1 * 2;
    mod_val = mod_val + 100;
    n = n + 100;
    printf ("%i\n", val_1);
}

cc_number is 1234

loop i = 0:
val_1 = (1234%100)/10;   -->  = 34/10  --> = 3
agg_val_1 = agg_val_1 + 3*2;  -->  = 6
mod_val = mod_val + 100  -->  = 200
n = n + 100  -->  = 110
print 3

loop i = 1:
val_1 = (1234%200)/110  --> = 34/110 --> = 0
agg_val_1 = 6 + 0*2  --> = 6
mod_val = 200 + 100  --> = 300
n = 110 + 100 --> = 210
print 0

So, it's done exactly what you told it to do.

Second example:

cc_number is 12345678

loop i = 0:
val_1 = (12345678%100)/10;   -->  = 78/10  --> = 7
agg_val_1 = agg_val_1 + 7*2;  -->  = 14
mod_val = mod_val + 100  -->  = 200
n = n + 100  -->  = 110
print 7

loop i = 1:
val_1 = (12345678%200)/110  --> = 78/110 --> = 0
agg_val_1 = 14 + 0*2  --> = 14
mod_val = 200 + 100  --> = 300
n = 110 + 100 --> = 210
print 0

loop i = 2:
val_1 = (12345678%300)/210  --> = 78/210 --> = 0
agg_val_1 = 14 + 0*2  --> = 14
mod_val = 300 + 100  --> = 400
n = 210 + 100 --> = 310
print 0

loop i = 3:
val_1 = (12345678%400)/310  --> = 78/310 --> = 0
agg_val_1 = 14 + 0*2  --> = 14
mod_val = 400 + 100  --> = 500
n = 310 + 100 --> = 410
print 0

etc. etc. Your code is doing exactly what you told it to do.

What are you expecting?

Can you see what you are doing wrong? Why are you adding 100 to to mod_val? and why are you adding 100 to n? Do you perhaps want to be multiplying?

2
  • YES! I'm not sure why I put a plus, I meant to be MULTIPLYING so that I could move down the string and grab every other value! Thanks! Such a stupid error, but no matter how many times I went through it, I just couldn't pick it out. – Daniel Levi Oct 4 '14 at 19:35
  • Glad to help. Be sure to mark my answer as accepted so this question falls off the unanswered queue. :) – curiouskiwi Oct 5 '14 at 2:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .