Thread: ISBN Code Checking...!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    5
    Quote Originally Posted by Adak View Post
    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
    I think the position of X and ? do matters... the X could be anywhere except at the front... and also ? could be any where even at the front... So while Multiplying we have to skip the ? and also have to remember it's position so that the MystNumb we have found can be multiplied with the position and added to the sum of rest 9 numbers. and the new sum will be divisible by 11.

    so what about this ex. I copied it from a books ISBN Code....
    8121?2605X

    now the number that I have hided from you is 9. So in place of ? it would be 9. We need to find the numb via C.

    And very much thank you for your response.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Tipu Sultan View Post
    the X could be anywhere except at the front...
    As Tim and I have already told you that's wrong. The X is only valid as the last digit (check digit).

    From http://www.isbn.org/standards/home/i...ml/usmapp.htm:
    ...
    3. Construction of an international standard book number
    An international standard book number consists of ten digits[4] made up of the following parts
    ...
    Footnote 4: The digits are the arabic numerals 0-9; in the case of the check digit only, an X can sometimes occur (see 2.4)
    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    5
    Quote Originally Posted by AndiPersti View Post
    As Tim and I have already told you that's wrong. The X is only valid as the last digit (check digit).

    From http://www.isbn.org/standards/home/i...ml/usmapp.htm:


    Bye, Andreas
    THANKS A LOT... that reduces my work a lot more... now we just need to deal with 9 numbers and accordingly no need to deal with the position of the X... Thanks again....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking for a code in a string
    By shin_ono in forum C Programming
    Replies: 13
    Last Post: 10-01-2008, 02:04 AM
  2. Checking lines of code
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-10-2008, 02:05 AM
  3. Looking for Code Analysis / Checking Tool
    By nvoigt in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-01-2007, 03:34 AM
  4. Please restore the code tag checking script
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 76
    Last Post: 09-09-2006, 12:35 AM
  5. file existance checking /w code
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 02-08-2002, 02:40 PM

Tags for this Thread