print in alphabetical order.

how can i get the program to print automactically in alphabetical odrer instead of manually setting in the array myself.i appreciate any feedback.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace std;

void bubbleSort(string[], int);

int main ()
{
  int y = 0;
  string Client[12] =  { "Acme Construction                Machinery design", "Johnson Electrical               Switch manufacturing", "Brown Heating and Cooling        Boiler design",  "Jones Computers                  Computers sales",  
	                       "Williams Cleaning                Equipment Machinery sales","Smith Switches                   Switch manufacturing"};
  cout << "Client"<< "                           Business type\n" << endl;
  for (int i=0; i<12; i++)
	 cout << Client[i] << endl;   
  return 0;
}

void bubbleSort(string arr[], int n)
{
   bool swapped = true;
   int j = 0;
   string tmp;

   while (swapped)
   {
      swapped = false;
      j++;
      for (int i = 0; i < n + j; i++)
      {
         if ( arr[i].compare(arr[i + 1]) )
         {

            tmp = arr[i];
            arr[i] = arr[i +1];
            arr[i + 1] = tmp;
            swapped = true;


         }
      }
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
1>Deleting intermediate and output files for project 'WeekSixProgram', configuration 'Debug|Win32'
1>Compiling...
1>WeekSixProgram.cpp
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Linking...
1>LINK : C:\Users\Martin\Documents\Visual Studio 2008\Projects\WeekSixProgram\Debug\WeekSixProgram.exe not found or not built by the last incremental link; performing full link
1>Embedding manifest...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Build log was saved at "file://c:\Users\Martin\Documents\Visual Studio 2008\Projects\WeekSixProgram\WeekSixProgram\Debug\BuildLog.htm"
1>WeekSixProgram - 0 error(s), 0 warning(s)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

The first solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <set>
#include <string>
#include <algorithm>
#include <iterator>
#include <iostream>

int main()
{
    typedef std::set < std::string > Container;
    
    Container arr;
    arr.insert("Acme Construction                Machinery design");
    arr.insert("Johnson Electrical               Switch manufacturing");
    arr.insert("Brown Heating and Cooling        Boiler design");
    arr.insert("Jones Computers                  Computers sales");
    arr.insert("Williams Cleaning                Equipment Machinery sales");
    arr.insert("Smith Switches                   Switch manufacturing");
    arr.insert("Jones Computers                  Computers sales");
    arr.insert("Jones Computers                  Computers sales");

    std::copy(arr.begin(), arr.end(), std::ostream_iterator<Container::value_type>(std::cout , "\n"));

    return 0;
}
The second:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <map>
#include <iostream>

int main()
{
    typedef std::string Client;
    typedef std::string Business;
    typedef std::map <Client, Business> Container;

    Container arr;
    arr.insert(Container::value_type("Acme Construction", "Machinery design"));
    arr.insert(Container::value_type("Johnson Electrical", "Switch manufacturing"));
    arr.insert(Container::value_type("Brown Heating and Cooling", "Boiler design"));
    arr.insert(Container::value_type("Jones Computers", "Computers sales"));
    arr.insert(Container::value_type("Williams Cleaning", "Equipment Machinery sales"));
    arr.insert(Container::value_type("Smith Switches", "Switch manufacturing"));

    for(Container::iterator it(arr.begin()), end(arr.end()); it != end; ++it)
    {
        std::cout << "Client: " << it->first << '\t' << "business type: " << it->second << '\n';
    }

    return 0;
}


Of course I think you can find many others perfect solutions ^)
Hi. So you want the output to cout to be in alphabetical order? (And it doesn't matter what the input order is)

I can see from your main file that you have a bubbleSort() function ... but no calls are being made to sort your array.

Fix your bubbleSort() function and call it on your Clients array. The result should be a sorted array.

Carlo
thank you for all your help.
Topic archived. No new replies allowed.