Thread: Finding the problem creating bug during runtime

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

    Finding the problem creating bug during runtime

    I have been asked to make an array program which includes merging, sorting, averaging inserting, deleting arrays etc....
    While almost all the program has been made, i find few bugs giving false results during runtime ( no error though)... Though whole source code was copied from book, so it must be correct indeed.

    The problem is while i enter even number of arrays, it says array is not sorted, even if its sorted and in inser sort, these value creates problem
    2 4 6 8 10; no to be inserted =1...

    Here it is part of that program
    Code:
    #include<stdio.h>
    #include<conio.h>
    void insertsorted(int *,int *,int);
    void mergesort(int *,int,int *,int, int *);
    int checksort(int *, int *);
    main()
    {
        int item,n,choice,l,b,k,i,m,p,checkcounter;
        char rerun;
    
        point1:
        printf("\n Enter your choice for array operations\n\n");
    
        printf("\n\t5. Insert in sorted array");
    
        printf("\n\t10. Merge two sorted array");
        printf("\n\t11. Exit");
        printf("\n\n Your selected choice is: ");
        scanf("%d",&choice);
        if((choice<1)||(choice>11))
        {
            printf("Wrong value entered; please try again");
            getch();
            system("cls");
            goto point1;
        }
        if(choice==11)
        {
            printf("\n Thanks for using the program\n Have a great day!!!");
            getch();
            exit(0);
        }
        printf("\nEnter element size: ");
        scanf("%d",&n);
        int A[n];
        printf("\nEnter %d elements of the array: \n",n);
        for(i=0;i<n;i++)
        scanf("%d",&A[i]);
        printf("\nThe elements of arrays are: ");
        for(i=0;i<n;i++)
        printf("%d\t",A[i]);
        if(choice==10)
        {
            printf("\nEnter element size of second array");
            scanf("%d",&m);
            p=m+n;
            int B[m],sorted[p];
            printf("\nEnter %d elements of the array: \n",m);
            for(i=0;i<m;i++)
            scanf("%d",&B[i]);
            printf("\nThe elements of arrays are: ");
            for(i=0;i<m;i++)
            printf("%d\t",B[i]);
            checkcounter=checksort(A,&n);
            if(checkcounter==-1)
            {
                printf("\n\n The element of this array are not sorted, please insert a sorted array");
                printf("\n Thanks for using the program\n Have a great day!!!");
                getch();
                exit(0);
            }
            checkcounter=checksort(B,&m);
            if(checkcounter==-1)
            {
                printf("\n\n The element of this array are not sorted, please insert a sorted array");
                printf("\n Thanks for using the program\n Have a great day!!!");
                getch();
                exit(0);
            }
            mergesort(A,n,B,m,sorted);
            printf("\nThe new array is:\n");
            for(i=0;i<p;i++)
            {
                printf("%d\t",sorted[i]);
            }
            getch();
        }
        switch (choice)
        {
            case 5:
            checkcounter=checksort(A,&n);
            if(checkcounter==-1)
            {
                printf("\n The element of this array are not sorted, please insert a sorted array");
                break;
            }
            printf("\nEnter number to be inserted in array");
            scanf("%d",&item);
            insertsorted(A,&n,item);
            printf("\nThe new array is:\n");
            for(i=0;i<n;i++)
            {
                printf("%d\t",A[i]);
            }
            break;
            case 10:
            break;
            default:
            printf("\n Wrong option selected; please try again");
            getch();
            system("cls");
            goto point1;        
        }
        getch();
        system("cls");
        printf("\n Do you want to re run the program??? (y to continue; any other key to exit): ");
        fflush(stdin);
        scanf("%c",&rerun);
        if(rerun=='y')
        {goto point1;}
        system("cls");
        printf("\n Thanks for using the program\n Have a great day!!!");
        getch();
    }
    void insertsorted(int *a, int *n, int item)
    {
        int k;
        k=*n-1;
        while((item<a[k])&&(k>=0))
        {
            a[k+1]=a[k];
            a--;
        }
        a[k+1]=item;
        (*n)++;
    }
    void mergesort(int *a, int m, int *b, int n, int *c)
    {
        int na, nb, nc;
        na=nb=nc=0;
        while((na<m) && (nb<n))
        {
          if(a[na]<b[nb])
          c[nc]=a[na++];
          else
          c[nc]=b[nb++];
          nc++;
          }
          if(na==m)
          {
          while(nb<n)
          c[nc++]=b[nb++];
          }
          else if (nb==n)
          {
               while(na<m)
               c[nc++]=a[na++];
               }
               }
    int checksort(int *a, int *n)
    {
        int i=0;
        
        while(i<*n)
        {
            if(a[i]>a[i+1])
            {
                return -1;
            }
            i++;
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    10
    checkcounter returns -1, on entering array with even numbers 2nd time.... Be it merge sorted array or re running program via y character when prompted...

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by ashu123 View Post
    Though whole source code was copied from book, so it must be correct indeed.
    I wouldn't assume any code from a book is correct, especially from a book by Herbert Schildt. If this code is from one of his books, throw it out immediately (the book and the code). His writing is clear and sounds confident about the C language, but it's rubbish more often than not.

    That said, I couldn't read the code past all of the non-standard code and poor style. Even if this is not from a Schildt book, I still recommend getting a better book.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ashu123 View Post
    I have been asked to make an array program which includes
    Though whole source code was copied from book, so it must be correct indeed.
    No book worth its weight would use a goto where a loop is such a simple and obvious easy alternative. Nor would it pass n as a pointer to insertsorted or checksort. Nor would it have a buffer overrun inside checksort. Everything else you wrote indicates that you are writing this yourself. So, enough of this crap about the whole thing being from a book!

    Turn your compiler warning levels up and pay attention to them.
    The buffer overrun I mentioned is the most likely cause of the problem you know about.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by christop View Post
    I wouldn't assume any code from a book is correct.

    Well, there are some good books out there. However, yeah, I wouldn't assume code in a book is correct unless I've checked it VERY carefully. The best authors would probably encourage you to do that. The worst ones will probably be offended at the idea of such scrutiny.

    Also, even with the best code from a book, there is always a significant chance of errors made when typing it in. People invariably over-estimate their ability to copy something correctly.

    I also agree with iMalc. The code is pretty crappy. If it has been correctly transcribed from a book, that book is pretty poor. It looks like something that as been hacked, and badly hacked at that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You sure it is not a example of how NOT to write code.
    You might want to reread the book because that is really bad code.

    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
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    printf("\nEnter element size: ");
    scanf("%d",&n);
    int  A[n];
    I thought that you couldn't do that? You could malloc some memory, but you might want to get more experience programming before playing around with that.

    I'm guessing the goto's are there because this is lots of different codes glued together (right?)

    Don't be discouraged, but this code needs a lot of work.
    Last edited by Click_here; 08-18-2012 at 06:57 PM. Reason: space missing in code...

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can actually declare an array with a size from a variable in the C99 standard and some compilers support that i.e. GCC
    MSVC does not support it though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    10
    Quote Originally Posted by iMalc View Post
    No book worth its weight would use a goto where a loop is such a simple and obvious easy alternative. Nor would it pass n as a pointer to insertsorted or checksort. Nor would it have a buffer overrun inside checksort. Everything else you wrote indicates that you are writing this yourself. So, enough of this crap about the whole thing being from a book!

    Turn your compiler warning levels up and pay attention to them.
    The buffer overrun I mentioned is the most likely cause of the problem you know about.
    Code is from book was referenced to function which is created; main is written by me and that is too obvious, cause no book will provide whole source code all the functioning in one program....

  10. #10
    Registered User
    Join Date
    Aug 2012
    Posts
    10
    Quote Originally Posted by Click_here View Post
    Code:
    printf("\nEnter element size: ");
    scanf("%d",&n);
    int  A[n];
    I thought that you couldn't do that? You could malloc some memory, but you might want to get more experience programming before playing around with that.

    I'm guessing the goto's are there because this is lots of different codes glued together (right?)

    Don't be discouraged, but this code needs a lot of work.
    With GCC compiler, its quite possible, i believe.....
    Goto is just to rerun the program in case of wrong entry. i might use loops for that.... But that would work only when complete program ends....
    And i am not expert in C, else i would not have asked the question.....
    And i agree the code needs work, thats why i want help...
    Last edited by ashu123; 08-18-2012 at 11:46 PM.

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    10
    Quote Originally Posted by christop View Post
    I wouldn't assume any code from a book is correct, especially from a book by Herbert Schildt. If this code is from one of his books, throw it out immediately (the book and the code). His writing is clear and sounds confident about the C language, but it's rubbish more often than not.

    That said, I couldn't read the code past all of the non-standard code and poor style. Even if this is not from a Schildt book, I still recommend getting a better book.
    I am using Data Structure using C by RS Salaria.... Dont know how good it is... But i would like to mention his book only provide source code of function.. Main is written by me cause we were asked to club all the function in a menu driven format....

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ashu123 View Post
    I am using Data Structure using C by RS Salaria.... Dont know how good it is... But i would like to mention his book only provide source code of function.. Main is written by me cause we were asked to club all the function in a menu driven format....
    I can't say I know that book, but most books with a theme like "data structures for C" assume that you already know the basics of C. They are concerned with teaching about data structures, not with teaching the basics of C.

    It is very rare for a book on programming to provide complete source for complete programs. From a teaching perspective, there is little value in a book that requires the reader to thumb through a few hundred pages of code (it is better to provide the actual source on disk, rather than typesetting it in a book). From a logistic perspective, any book that contains pages of code from a largish bit of software would probably be heavier than most people can lift safely. Hence most introductory books provide small code samples to illustrate particular things and (the better ones at least) discuss concepts that are relevant to applying those ideas to realistic programs.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Okay, it looks like I overestimated your English skills, given English is clearly not your first language, which I can tell from your misplacement of plurals. E.g. "these value creates problem" should perhaps be "these values create the problem".
    It can sometimes be good to mention that English is not your first language, even if your English mostly seems pretty good.
    "Though whole source code was copied from book" is certainly not true, but that mustn't be what you meant to say.

    Do you need more help finding the buffer overrun in checksort?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    void insertsorted(int *a, int *n, int item)
    {
        int k;
        k=*n-1;
        while((item<a[k])&&(k>=0))
        {
            a[k+1]=a[k];
            a--;
        }
        a[k+1]=item;
        (*n)++;
    }
    If that function is copied from the book than I think you should stop reading it immediatetly because that function is just crap (it doesn't allocate space for the additional item, thus overwrites memory after the array and uses a very strange technique to go through the array)

    Bye, Andreas

  15. #15
    Registered User
    Join Date
    Aug 2012
    Posts
    10
    Quote Originally Posted by AndiPersti View Post
    it doesn't allocate space for the additional item
    Isnt (*n)++ for the same??
    Besides, I have just pointed out the mistake in line 122 ( line 8 in insertsorted function), its actually k-- instead of a--. it was minor print mistake...
    Now insertsort function works with the given set of values (2 4 6 8; inserted number =1)
    Now problem comes with re running the program with same number of elements as earlier (example 4 in above) and taking two same even sized array in merge sort. Checksort returns -1 whenever its run second time (with even numbers)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime problem
    By SantoshN in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 02:42 PM
  2. Creating an Executable in runtime
    By astropirit in forum Windows Programming
    Replies: 2
    Last Post: 07-08-2009, 10:46 AM
  3. Runtime formation and execution at runtime
    By Soham in forum C Programming
    Replies: 17
    Last Post: 08-27-2008, 08:45 AM
  4. Creating controls in runtime
    By starcatcher in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2008, 05:29 PM
  5. Runtime problem
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 10-30-2005, 06:30 PM

Tags for this Thread