Since I can't understand why the position of the X or ? in the ISBN values, makes a difference, the whole if(th1 > th2) block of code, is a mystery to me.
Here's some pseudo code that I'd suggest studying:
Code:
define X 10
This is all in main():
integers isbn[10]={2,4,6,3,7,8,1,X,9,'?'}; //2463781X95
integers i, valid=0, sum=0, mysteryNum=0, trial;
for i=0;i<10;i++
if(isbn[i]==10)
print "%c ",'X';
else if isbn[i]=='?'
print"%c ",'?';
else
print "%d ",isbn[i];
if isbn[0]==X
valid=0;
print "\n Invalid Code: The ISBN first value, can't be an X.\n";
this is the else to the if directly above, and it is the heart of the
program:
for i=0,sum=0;i<10;i++ //multiply each digit by it's index +1, but
if isbn[i] != '?' //exclude the mystery number, if present
sum += isbn[i] * (i+1);
else
mysteryNum=i+1; //we have a mystery number at i, but it could be at index zero, (which we don't want, so +1
print "\nOriginal ISBN sum: %d Original sum mod 11: %d\n\n",sum, sum%11;
if sum % 11==0 //we have a valid ISBN number
if mysteryNum //with a mystery number
mysteryNum=0; //so the mystery number if zero, gives us a valid ISBN number
valid=1; //and the ISBN number is valid
else we need to test the mystery number // ISBN number is invalid - test the mystery array[mysteryNum-1], for values.
for i=1;i<11;i++ { //i=0 has already been tested, and failed
trial = sum + (i*mysteryNum);
/* for debug only (I made an error down below, and this is
a good way to show how you can find and fix errors)
if i==2
printf("trial: %d sum: %d mysteryNum: %d\n",trial,sum, mysteryNum); getchar();
*/
if trial % 11==0
valid=1;
isbn[mysteryNum-1]=i;
sum = trial;
break;
end of for
end of else
if sum
print "\nFinal ISBN sum: %d Final sum mod 11: %d\n\n",sum, sum % 11
if valid
print "The ISBN code is valid\n"
else
print "The ISBN code is invalid\n"
for i=0;i<10;i++
if isbn[i]==10
isbn[i]='X';
if isbn[i]=='?' OR isbn[i]=='X'
print "%c ",isbn[i];
else
print "%d ",isbn[i];
end of for
end of if
end of else
printf("\n");
return 0;
end of program