1

It works fine for the first few iterations, but afterwards it starts changing the final digit into a number that I know is not correct. Here is the code:

long long isbnToArray(long long userNum)
{

int i;
char isbnArray[20];
for (i = 0; i <12; i++)
{
    long long userNumFirst;
    long long userNumRem;
    int j = 11 - i;  // gives us the power of 10 required for the divisor/ modulus operators
    long long int divR = pow(10.0, j);

    userNumFirst = userNum / divR;  // value of the first digit
    userNumRem = userNum % divR;  // remainder after division
    isbnArray[i] = userNumFirst;  // inputs the first ISBN digit value into the array
//    printf("%d.", isbnArray[i]);
    userNum = userNumRem; // sets the isbn number as the remainder for the next iteration of the loop, eg 123451234512 becomes 23451234512.

    printf("userNum = %lli\n", userNum);
}
}

After the 2nd iteration, the final digit turns to '5' when it should remain a '2', and then it spirals after that. I cant figure out why.

When run, the values are:

userNum = 23451234512 (Expected)

userNum = 3451234512 (Expected)

userNum = 451234515 (Expected 451234512)

userNum = 51234519 (Expected 51234512)

userNum = 1234524 (Expected 1234512)

userNum = 234524 (Expected 234512)

userNum = 34524 (Expected 34512)

userNum = 4527 (Expected 4512)

userNum = 527 (Expected 512)

userNum = 32 (Expected 12)

userNum = 2 (Expected 2)

userNum = 0 (Expected 0)

  • Just as a node the input to the expected output is generally also a very important information to give. Also I can not replicate the behaviour. Is this correct ? https://onlinegdb.com/rydmG2URz – Kami Kaze May 14 '18 at 06:50
  • The dupe is quite probably correct. – Kami Kaze May 14 '18 at 06:54
  • The dupe was absolutely correct. I used round() in the pow function and now it works flawlessly. I would have never caught it without your guys' help! – Alex Wittwer May 14 '18 at 06:59
  • don't do that. There are only a very limited number of integer powers of 10 in `long long` range, so you just need a small lookup table – phuclv May 14 '18 at 08:49

0 Answers0