bubbleSort array string in alphabetical order.

This is a discussion on bubbleSort array string in alphabetical order. within the C++ Programming forums, part of the General Programming Boards category; hi I have two syntax errors in this program and what its should print out is the array strings in ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    bubbleSort array string in alphabetical order.

    hi I have two syntax errors in this program and what its should print out is the array strings in alphabetical order from client and their business type. any feedback is appreciated.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void bubbleSort(string[], int);
    
    int main ()
    {
      string Client[12] =  { "Acme Construction\t\tMachinery design", "Johnson Electrical\t\tSwitch manufacturing", "Brown Heating and Cooling\tBoiler design",  "Jones Computers\t\t\tComputers sales",  
    	                       "Williams Cleaning\t\tEquipment Machinery sales","Smith Switches\t\t\tSwitch manufacturing"};
      cout << "Client\t\t\t\t"<< "Business type\n" << endl;
      for (int i=0; i<12; i++)
    	 cout << Client[i] << endl;
    
    
    
      return 0;
    }
    void bubbleSort(string arr[], int n)
    {
    	int i, j, flag=1;
    	int temp;
    
    
          for(i = 0; (i <= n) && flag; i++)
          {
    		  flag =0;
    		  for(j=0;j<(n-1); j++)
    		  {
    			  if ( arr[j+1] < arr[j])
    			  {
    				  temp = arr[i];
                      arr[j] = arr[j +1];
                      arr[j + 1] = temp;
                      flag = 1;
    			  }
    		  }
    	  }
    	  return 0;
    }
    Code:
    1>------ Rebuild All started: Project: 2-d, Configuration: Debug Win32 ------
    1>Deleting intermediate and output files for project '2-d', configuration 'Debug|Win32'
    1>Compiling...
    1>2-d.cpp
    1>c:\users\martin\documents\visual studio 2008\projects\2-d\2-d\2-d.cpp(32) : error C2440: '=' : cannot convert from 'std::string' to 'int'
    1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    1>c:\users\martin\documents\visual studio 2008\projects\2-d\2-d\2-d.cpp(39) : error C2562: 'bubbleSort' : 'void' function returning a value
    1>        c:\users\martin\documents\visual studio 2008\projects\2-d\2-d\2-d.cpp(5) : see declaration of 'bubbleSort'
    1>Build log was saved at "file://c:\Users\Martin\Documents\Visual Studio 2008\Projects\2-d\2-d\Debug\BuildLog.htm"
    1>2-d - 2 error(s), 0 warning(s)
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your temp variable has to be of the same kind of thing that you're trying to sort. If you're sorting strings, your temp can't be an int.

    If your function is marked as returning void, you can't then later try to return a value.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    In addition to using the wrong data type, your swap is logically broken.
    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"

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74
    And in the outer loop, the condition of "(i <= n)" is unnecessary. it just slow down the speed of the algorithm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21