Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : SouthAmericanCountries.cpp
- // Author :
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- using namespace std;
- void swapPlace(string myArray[],int a, int b)
- {
- string temp = myArray[a];
- myArray[a]= myArray[b];
- myArray[b]= temp;
- }
- string* sortByCharacterLength(string countries[])
- {
- string countryByCharacterLength[12];
- //populate countryByCharacter with contries array
- for(int i = 0; i<12; i++)
- {
- countryByCharacterLength[i]=countries[i];
- cout << countryByCharacterLength[i] << endl<<endl;
- }
- bool needsSorting = false;
- //sort countryByCharacterLength
- do{
- needsSorting=false;
- for(int i = 0; i<11; i++)
- {
- if (countryByCharacterLength[i].length() > countryByCharacterLength[i+1].length())
- {
- swapPlace(countryByCharacterLength, i, i+1);
- needsSorting=true;
- }
- }
- }while(needsSorting);
- cout << endl;
- for(int i = 0; i<12; i++)
- {
- cout << countryByCharacterLength[i]<<endl;
- }
- return countryByCharacterLength;
- }
- int main() {
- string countries[12];
- countries[0]= "Chile";
- countries[1]= "Peru";
- countries[2]= "Argentina";
- countries[3]= "Venezuela";
- countries[4]= "Bolivia";
- countries[5]= "Ecuador";
- countries[6]= "Uruguay";
- countries[7]= "Paraguay";
- countries[8]= "Brazil";
- countries[9]= "Guyana";
- countries[10]= "Suriname";
- countries[11]= "Colombia";
- sortByCharacterLength(countries);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement