Advertisement
MrsMcLead

Countries

Apr 24th, 2014
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. //============================================================================
  2. // Name        : SouthAmericanCountries.cpp
  3. // Author      :
  4. // Version     :
  5. // Copyright   : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. void swapPlace(string myArray[],int a, int b)
  13. {
  14.     string temp = myArray[a];
  15.     myArray[a]= myArray[b];
  16.     myArray[b]= temp;
  17. }
  18.  
  19. string* sortByCharacterLength(string countries[])
  20. {
  21.  
  22.     string countryByCharacterLength[12];
  23.     //populate countryByCharacter with contries array
  24.     for(int i = 0; i<12; i++)
  25.     {
  26.         countryByCharacterLength[i]=countries[i];
  27.         cout << countryByCharacterLength[i] << endl<<endl;
  28.     }
  29.     bool needsSorting = false;
  30.     //sort countryByCharacterLength
  31.     do{
  32.         needsSorting=false;
  33.  
  34.     for(int i = 0; i<11; i++)
  35.     {
  36.         if (countryByCharacterLength[i].length() > countryByCharacterLength[i+1].length())
  37.         {
  38.             swapPlace(countryByCharacterLength, i, i+1);
  39.             needsSorting=true;
  40.  
  41.         }
  42.     }
  43.  
  44.     }while(needsSorting);
  45.     cout << endl;
  46.             for(int i = 0; i<12; i++)
  47.                 {
  48.  
  49.                     cout <<  countryByCharacterLength[i]<<endl;
  50.                 }
  51.     return countryByCharacterLength;
  52. }
  53.  
  54. int main() {
  55.     string countries[12];
  56.     countries[0]= "Chile";
  57.     countries[1]= "Peru";
  58.     countries[2]= "Argentina";
  59.     countries[3]= "Venezuela";
  60.     countries[4]= "Bolivia";
  61.     countries[5]= "Ecuador";
  62.     countries[6]= "Uruguay";
  63.     countries[7]= "Paraguay";
  64.     countries[8]= "Brazil";
  65.     countries[9]= "Guyana";
  66.     countries[10]= "Suriname";
  67.     countries[11]= "Colombia";
  68.  
  69.     sortByCharacterLength(countries);
  70.  
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement