View Single Post
  #4 (permalink)  
Old July 12th, 2008, 09:43 PM
elvisfeverr elvisfeverr is offline
Authorized User
 
Join Date: Jan 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

using string arrays string[10] to hold names is a better choice than using c-style character strings char[10][]
there are many sorting algorithms for sorting strings and bubble sort is not an efficient sorting algorithm.you may want to use quicksort for internal sorting.so you want to work with functions.you can make 3 functions with you code.first one maybe named void getinput(*array) and you can get the names in this function into your array.another function maybe names output(*array) you can copy paste your code into this function to output names.and another function maybe named sortarray(*array) you can copy paste your code into that function that's simple.

//you can make it global so you dont need to pass it to each function for that little program.
//put the function prototypes here.

main()
{
      string name[10];
      GetArray(&name);
      SortArray(&name);
      cout<<"The sorted names are: "<<endl;
      OutputArray(&name);


getch();

}

void GetArray(&name)
{
  for(int i=0;i<name.length();i++)
      {
              cout<<"\nEnter name"<<i+1<<": ";
              cin>>string[i];
      }

}

void SortArray(&name)
{
string temp;
for (int iteration=0;iteration<=9;iteration++)
      {
          for(int k=0;k<=8;k++)
          {
              if (tolower(name[k])>tolower(name[k+1])
              {
              temp=name[k]; //these 3 lines are to swap elements in array
              name[k]=name[k+1];
              name[k+1]=temp;
              }
          }

      }

}

void OutputArray(&names)
{
    for(int l=0;l<=name.length();l++) //to avoid magic numbers dont use explicit numbers like 9
    {
            cout<<name[l]<<endl;
    }


}

well i am not an expert and i am sure i had some errors in the code but this is the logic.






Reply With Quote