Home Forums C Programming please help me

Viewing 3 reply threads
  • Author
    Posts
    • #2211
      HildegaBarnett
      Participant

      there is something wrong with bubbleSort argument
      #include
      void bubbleSort(float arr[],int len)
      {
      int isSorted=0;
      do
      {
      float temp;

      int i;
      isSorted=1;
      –len;
      for(i=0;i if(arr>arr[i+1])
      {
      isSorted=0;
      temp=arr
      ;
      arr
      =arr[i+1];
      arr[i+1]=temp;
      }
      }while(!isSorted);
      }

      int main()
      {
      float r[]={10,9,8,7,6,5,4,3,2,1};
      int j=10,b;
      bubbleSort(r[j],j);
      for(b=0;b<10;b++)
      printf(“%4.2fn”,r);
      return 0;
      }

      can you help me? I am a rookie.thanks.

    • #3589
      HildegaBarnett
      Participant

      Ah,I can manage it myself. :P
      in main function,”bubbleSort(r[j],j)”,there is no subscript operator”[]”.just “bubbleSort(r,j)”.

    • #3590
      GWILouisaxwzkla
      Participant

      Heres a bubble sort I wrote awhile ago ( hope this helps ):



      /****************************************************************
      * File Name : c:programstempCG.cpp
      * Date : March,28,2009
      * Comments : new project
      * Compiler/Assembler : Visual C++ 6.0
      * Modifications :
      *
      * Notes: A good sort for a small number of items. Total run time
      * is proportional to the number of items squared ( o ( N^2 ) ).
      *
      *
      * Program Shell Generated At: 11:25:48 a.m.
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


      #include < iostream >
      //#include < string.h >
      //#include < conio.h >
      //#include < math.h >
      //#include < iomanip >
      //#include < ctype.h >
      #include
      #include

      using namespace std;

      //>>>>>>>>>>>>>>>>>>>>>>>> GLOBAL DATA <<<<<<<<<<<<<<<<<<<<<<<<<
      const int MAX_NUMBERS = 50;
      int NUMBERS_PER_LINE = 15;
      //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<




      //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@

      void generateNumbers ( int * numbers , int numberItems );
      void bubbleSort ( int * array , int numberItems );
      void printArray ( int * array , int numberItems );

      //##################################################################################


      //main function ******************************

      int main ( )
      {

      int array [ MAX_NUMBERS ];
      generateNumbers ( array , MAX_NUMBERS );
      cout << endl << endl;
      cout << "unsorted array " << endl;
      printArray ( array , MAX_NUMBERS );
      bubbleSort ( array , MAX_NUMBERS );
      cout << endl << endl << "sorted array: " << endl;
      printArray ( array , MAX_NUMBERS );
      cout << endl;


      return 0 ;
      }


      /******************************* FUNCTION DEFINITION ******************************

      Name : bubbleSort
      Parameters :

      array a(n) int * ,
      numberItems a(n) int


      Returns: Void type
      Comments:



      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void bubbleSort ( int * array , int numberItems )
      {

      int endSortedArray = numberItems - 1;
      int lastSwapIndex;
      int temp;
      while ( endSortedArray > 0 ) //while not at the end of the unsorted array
      {
      lastSwapIndex = 0; //save index of the last item swapped
      int i = 0; //start at the beggining of the unsorted array
      while ( i < endSortedArray ) //while not in the sorted items
      {
      if ( array [ i ] > array [ i + 1 ] ) //if current item is smaller than next , bubble up
      {
      //swap array [ i ] and array [ i + 1 ]
      temp = array [ i ];
      array [ i ] = array [ i + 1 ];
      array [ i + 1 ] = temp;
      lastSwapIndex = i;
      }
      i ++;
      }
      endSortedArray = lastSwapIndex; //reset swap boundry
      }



      return;
      }
      /******************************* FUNCTION DEFINITION ******************************

      Name : printArray
      Parameters :

      array a(n) int * ,
      numberItems a(n) int


      Returns: Void type
      Comments:



      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void printArray ( int * array , int numberItems )
      {


      int i = 0;
      cout << endl << endl;
      cout << "array : " << endl;
      while ( i < numberItems )
      {
      cout << array [ i ] << " " ;
      if ( i != 0 )
      if ( i % NUMBERS_PER_LINE == 0 )
      cout << endl;
      i ++;
      }

      return;
      }
      /******************************* FUNCTION DEFINITION ******************************

      Name : generateNumbers
      Parameters :

      numbers a(n) int ,
      numberItems a(n) int


      Returns: Void type
      Comments:



      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void generateNumbers ( int * numbers , int numberItems )
      {

      int i = 0;

      srand ( time ( NULL ) );
      while ( i < numberItems )
      {
      numbers [ i ] = rand () % MAX_NUMBERS;
      i ++;
      }


      return;
      }
    • #3591
      HildegaBarnett
      Participant

      Thanks,dman!

Viewing 3 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.