Thread: problem(program error) with bubble sort(code included)

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    11

    problem(program error) with bubble sort(code included)

    I have wrote written myself a bubble sort for a character array, However, when I run the code, an program error message box shown, what's wrong with this code, can anyone help.....

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <cstring>
    
    void sorting ( char[][2], int );
    void printArray ( char[][2], int );
    
    const int arraySize = 5;
    
    int main()
    {
       char array[arraySize][2] = { {'A', 'A'}, {'B', 'B'}, {'C', 'C'} , {'D', 'D'}, {'E', 'E'} };
       sorting ( array, arraySize );
       printArray ( array, arraySize );
    
       system("PAUSE");
       return 0;
    }
    
    void sorting ( char array[][2], int arraySize )
    {
       char *tmp;
       int i;
    
       cout << endl;
       for( i = 0; i < 10; i++ )
       {
          if( strcmp( array[i], array[i+1] ) > 0 )
          {
             strcpy( tmp, array[i] );
             strcpy( array[i], array[i+1] );
             strcpy( array[i+1], tmp );
          }
       }
    }
    
    void printArray ( char array[][2], int arraySize )
    {
       for ( int i = 0; i < arraySize; i++ )
       {
          cout << array[i] << endl;
       }
    }

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
    strcpy( tmp, array );
    notice how tmp is a char*

    You have not allocated memory to this pointer, therefor you try to copy memory in to this array that doesn't exist, causing a crash.

    I'm guessing you meant to do:

    Code:
    char* temp=new char[arraySize];
    -edit-
    or just do this:

    Code:
    char temp[5];
    and that would run much better, and you don't have to worry about dynamic memory
    Last edited by jverkoey; 05-16-2004 at 01:05 AM.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    11
    I've tried to edit the code as you mentioned, but there's still a program error message box from windows shown, you know the reason?

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    i'm guessing it's because in your strings, you don't have any null terminators, so it doesn't know when to stop reading, reads too far, and crashes.

    So what you need to do is instead of { 'A', 'A' } and etc...., do something like this:

    Code:
    char array[arraySize][3] = { "AA", "BB", "CC", "DD", "EE" };

    also make sure in your loop that you do i<arraySize, not i<10, or you'll read to far in to the arrray again Gotta love arrays

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    11
    Here's the edited code, and still got problem with it, really don't know why... this is a bubble sort program, but it turns out to be messy...

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <cstring>
    
    void sorting ( char[][2], int );
    void printArray ( char[][2], int );
    
    const int arraySize = 5;
    
    int main()
    {
       char array[arraySize][2];
       for ( int i = 0; i < arraySize; i++ )
       {
          cout << "Enter " << i << " no. :";
          cin.ignore ();
          cin.getline( array[i], 2, '\n' );
       }
       sorting ( array, arraySize );
       printArray ( array, arraySize );
    
       system("PAUSE");
       return 0;
    }
    
    void sorting ( char array[][2], int arraySize )
    {
       char* tmp = new char[arraySize];
       int i;
    
       cout << endl;
       for( i = 0; i < arraySize; i++ )
       {
          if( strcmp( array[i], array[i+1] ) > 0 )
          {
             strcpy( tmp, array[i] );
             strcpy( array[i], array[i+1] );
             strcpy( array[i+1], tmp );
          }
       }
    }
    
    void printArray ( char array[][2], int arraySize )
    {
       for ( int i = 0; i < arraySize; i++ )
       {
          cout << array[i] << endl;
       }
    }

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
       char* tmp = new char[arraySize];
       int i;
    
       cout << endl;
       for( i = 0; i < arraySize; i++ )
       {
    	  if( strcmp( array[i], array[i+1] ) > 0 )
    	  {
    		 strcpy( tmp, array[i] );
    		 strcpy( array[i], array[i+1] );
    		 strcpy( array[i+1], tmp );
    	  }
       }
    just for argument's sake right now, let's just make char temp be a constant size, until you get the bugs worked out. So instead of making it dynamic, just do:

    char tmp[256];

    where 256 should be a good enough sized array.

    The only other bug i can see in the code is that your input might still be overflowing the array. If you are inputting only one letter for each line, you should be fine, but if you put 2 or more, it will run out of room and crash. I'd suggest changing array's size from 2 to 64 or something like that. Just choose a number that suits your program.

    And like I said, use these constant sizes for now, until you get the bugs worked out. Dynamically-sized arrays are much better portability-wise, but when you're trying to debug it, constant sizes can sometimes work the best.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    11
    First of all, thx for jverkoey's help...eventually, I find out the problems were caused by cin.ignore()...see the code below...

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <cstring>
    #include <iomanip>
    
    void sorting ( char[][40], int );
    void printArray ( char[][40], int );
    
    const int arraySize = 5;
    
    int main()
    {
       char array[arraySize][40];
       for ( int i = 0; i < arraySize; i++ )
       {
          cout << "(Index" << i << ") Please enter your name: ";
          cin.getline( array[i], 40, '\n' );
       }
       sorting ( array, arraySize );
       printArray ( array, arraySize );
    
       system("PAUSE");
       return 0;
    }
    
    void sorting ( char array[][40], int arraySize )
    {
       char tmp[40];
    
       for( int i = 0; i < arraySize - 1; i++ )
       {
          for ( int j = 0; j < arraySize - 1 - i; j++ )
          {
             if( strcmp( array[j], array[j+1] ) > 0 )
             {
                strcpy( tmp, array[j] );
                strcpy( array[j], array[j+1] );
                strcpy( array[j+1], tmp );
             }
          }
       }
    }
    
    
    void printArray ( char array[][40], int arraySize )
    {
       for ( int i = 0; i < arraySize+1; i++ )
       {
          cout << array[i] << endl;
       }
    }
    Last edited by choykawairicky; 05-16-2004 at 12:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Alternate energy sources
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-02-2005, 07:07 PM