Thread: ISBN Code Checking...!

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    5

    Question ISBN Code Checking...!

    ISBN Code is a 10 digit number where we can use 1 to 10 but 10 is represented by 'X'.

    Ex. 2463781X95

    Here X=10, but the 1st digit can not be 'X' or else it is invalid.

    Now, to check any ISBN Code whether it is valid or not we have to multiply 1st digit with 1 and second digit with 2 up to tenth digit with 10 and have to add them together. The sum must be divisible by 11 or else it is invalid ISBN Code.

    Ex. 2463781X95
    sum = 2*1 + 4*2 + 6*3 + 3*4 + 7*5 + 8*6 + 1*7 + X*8 + 9*9 + 5*10.
    and this 'sum%11=0', otherwise it is invalid.

    Now, the question is one of the digits of the ISBN Code will be hidden by '?' and we have to find the correct digit for that place from 0-10 and that no will be the answer. As far as the property is concerned the founded no can not exceed X i.e. 10, if it does the corresponding error message should be, "No Possible Number Found". If the ISBN Code starts with X the error message should be "Invalid Code".
    In case of bad inputs error message should be "Invalid Code".

    Can anybody help me with the correct solution? Please post your program codes.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Tipu Sultan View Post
    Please post your program codes.
    You should read the homework policy of this forum. You have to start with posting code :-)

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    IIRC, only the last digit can be X in a ISBN.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Or, if you honestly don't know how to get started, throw your conception of this problem at us.

    We will not do your work for you, but we are often happy to help a person find their way.

    Soma

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Tipu Sultan!

    This little puzzle is far too good to just blurt out the answer. How would you go about solving this, without a computer? Use that as a guide to the logic you'll use for your program.

    We're glad to help, but you need to start this off, and post some code, first. Your problem solving skills will increase considerably, if you give them sufficient exercises like this.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Step 1: Write a program that lets the user enter an ISBN. Then display the entered ISBN.
    Step 2A: Add the function to verify that the entered ISBN is valid range of input numbers and X char.
    Step 2B: Add the function to verify that the entered ISBN check digit is valid.
    Step 3. Let the user enter a "?" mark in a spot of the entered ISBN.
    Step 4. Figure out have to determine the correct value to be place in the "?" position.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Why waste any of your time on the OP, when the entirety of his contribution is

    Can anybody help me with the correct solution? Please post your program codes.
    He. Only. Wants. Your. "Codes".

    He has no intention of doing this himself.

    Sigh.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    He has no intention of doing this himself.
    But... but sometimes people just need a smack.

    I just happen to be especially good at that. ^_^

    Soma

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    5
    Sorry for the misunderstanding, actually I didn't know the policy and who am I to ask you to do my work? I just wanted the main portion to be cleared and find out where my faults are... I do have a code but it is not working properly...
    Code:
    #include<stdio.h>
    int i;
    int checkx(int isbn[10])
    {
        for(i = 0; i < 10; i++)
        {
    		  if(isbn[i] == 'X')
            break;
        }
        return i;
    }
    int checko(int isbn[10])
    {
        for(i = 0; i < 10; i++)
        {
    		  if(isbn[i] == '?')
            break;
        }
        return i;
    }
    int main()
    {
    	 int isbn[10], th1, th2, sum = 0, j;
        char ten = 'X';
        printf(" Enter the ISBN Code: ");
    	 scanf("%s", isbn[10]);
    	 th1 = checkx(isbn[10]);
    	 th2 = checko(isbn[10]);
        if(th1 > th2)
        {
            for(i = 0; i < th2; i++)
                sum = sum + isbn[i]*(i+1);
            for(i = (th2 + 1); i <th1; i++)
                sum = sum + isbn[i]*(i+1);
            for(i = (th1+1); i <10; i++)
                sum = sum + isbn[i]*(i+1);
            sum = sum + 10*(th1+1);
        }
        else
        {
            for(i = 0; i < th1; i++)
                sum = sum + isbn[i]*(i+1);
            for(i = (th1+1); i < th2; i++)
                sum = sum + isbn[i]*(i+1);
    		  for(i = (th2+1); i < 10; i++)
                sum = sum + isbn[i]*(i+1);
            sum = sum + 10*(th1+1);
        }
        while((sum%11) != 0)
        {
    		  sum = sum + (th2+1)*j;
            j++;
        }
        if(j == 10)
        printf("\n The Missing No. is 'X'.");
        else
        {
            if(j > 10)
            printf("\n There is no possible no.");
            else
            printf("\n The No. is: %d",j);
        }
        return 0;
    }

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could simply try all 10 possibilities no matter where the ? is. If you sum the first 9 of the ISBN numbers in the usual way, then mod 11, you either get the last digit - which is a check digit - or not. If you manage to go through all the digits before finding the check digit, the ISBN is faked and there is no answer.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First thing I noticed was line 26. That should be giving you an error when you try to compile (or at least a warning).

    Code:
         scanf("%s", isbn[10]); //error
    
        /* isbn[10] is ONE element, and it is outside the isbn array of ten elements. In C, array elements are numbered FROM 0 TO n-1, (so here, from 0 to 9, instead of 1 to 10.
    
    Also, the %s format in scanf("%s"...), requires a pointer to the isbn array (which in C, is the name of the array itself: "isbn"). 
    
    So the correct scanf would be: scanf("%s",isbn);
    
    AND, if you want to hold the isbn array values as a string, then you should use %s and the isbn should be a char array with 11 elements in it, instead of 10. Why? Because in C, chars are just char's, and NOT strings, unless they have an end of string char marking the end of the string: '\0'.
    
    Integer arrays are not marked with an end of digit marker of any kind. So an array of 10 integers (numbers of any kind), would need 10 array elements in it (for it's size). A char array NOT a string, would also need just 10 elements for it's size, to hold 10 char's: array[10]. But an array of chars TO BE A STRING, needs 11 elements, because it needs one extra char to go on the end of it: array[11].
    
    
         th1 = checkx(isbn[10]);
         th2 = checko(isbn[10]);
    You need two nested for loops for your "find the answer" block of code. The outer for loop will control the position (which digit of the number), that is currently being worked on to find the answer, and the inner for loop will actually try the various values. If your other functions take the place of the outer for loop, then only one for loop will be needed for the testing loop.

    Testing should be done ONLY inside the inner for loop. No while loop is needed (could be used, but they generally make things more difficult for beginners). For loops give
    you extra structure to build a correct loop.

    Most of what you have in your program, you don't need. This is common with good students, however. It's one of the things you will find programmers always need to be on guard against -- going "Rube Goldberg".
    Last edited by Adak; 08-24-2012 at 02:49 PM.

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    First step: Turn on the compiler warnings.

    One warning will tell you that the scanf()-call in line 26 doesn't work because isbn[10] would be the eleventh element in the isbn array (but unfortunately it's out of bounds because there are only 10 elements declared (valid indices 0-9)) which would be an int but "%s" expects a string and needs an address/pointer as the second argument.

    Second you don't pass the array correctly to the functions:
    Code:
    th1 = checkx(isbn[10]);
    would pass only the value of the eleventh element (which as said before is not valid) to the function.

    To pass an array (or more accurate: to pass a pointer to the first element of the array) your call would look like this:
    Code:
    th1 = checkx(isbn);
    So I really recommend reviewing how arrays work in C.

    I would also check how ISBN10-numbers work because your explanations don't agree with the official description. Thus I don't think your algortihm will work with real ISBN-numbers.

    Bye, Andreas

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Wow, I stand corrected. Sorry OP, you're at least giving it a try. It's a (very) failed try, but...it's at least a try.

    Here are some things for you to read:
    scanf(3): input format conversion - Linux man page
    Arrays in C - Cprogramming.com
    Tutorial: Arrays in C and C++

  14. #14
    Registered User
    Join Date
    Aug 2012
    Posts
    5
    Sorry for being such a dumb programmer.... I solved it and it is showing that NO POSSIBLE NUMBERS FOUND!

    Code:
    #include<stdio.h>
    int i;
    int checkx(char isbn[])
    {
        for(i = 0; i < 10; i++)
        {
    		  if(isbn[i] == 'X')
            break;
        }
        return i;
    }
    int checko(char isbn[])
    {
        for(i = 0; i < 10; i++)
        {
    		  if(isbn[i] == '?')
            break;
        }
        return i;
    }
    int main()
    {
    	 int th1, th2, sum = 0, j;
    	 char isbn[10];
        char ten = 'X';
        printf(" Enter the ISBN Code: ");
    	 scanf("%s", isbn);
    	 th1 = checkx(isbn);
    	 th2 = checko(isbn);
        if(th1 > th2)
        {
            for(i = 0; i < th2; i++)
                sum = sum + isbn[i]*(i+1);
            for(i = (th2 + 1); i <th1; i++)
                sum = sum + isbn[i]*(i+1);
            for(i = (th1+1); i <10; i++)
                sum = sum + isbn[i]*(i+1);
            sum = sum + 10*(th1+1);
        }
        else
        {
            for(i = 0; i < th1; i++)
                sum = sum + isbn[i]*(i+1);
            for(i = (th1+1); i < th2; i++)
                sum = sum + isbn[i]*(i+1);
    		  for(i = (th2+1); i < 10; i++)
                sum = sum + isbn[i]*(i+1);
            sum = sum + 10*(th1+1);
        }
        while((sum%11) != 0)
        {
    		  sum = sum + (th2+1)*j;
            j++;
        }
        if(j == 10)
        printf("\n The Missing No. is 'X'.");
        else
        {
            if(j > 10)
            printf("\n There is no possible no.");
            else
            printf("\n The No. is: %d",j);
        }
    	 getch();
        return 0;

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Like I said, you should just try every digit in place of ? until you pass the regular test. There are only 10 to try.

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