User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 374,544 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,718 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 4256 | Replies: 2 | Solved
Reply
Join Date: Dec 2006
Posts: 7
Reputation: GuruGhulab is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
GuruGhulab GuruGhulab is offline Offline
Newbie Poster

Sort in Alphabetical order

  #1  
Dec 13th, 2006
Hello there, okie, my problem today is i i have all this names,

John, Dave, Steve, Kevin,Andrew,Scott,Colin,Timothy,Zenon 


and i have to sort them using insertion sort, i can sort number using insertion sort with no probs, but when it comes to strings, and stuff, i get lots of problems. so plz guyz help me out.

here is the code i tried,

i used the same procedure as sorting numbers, i don't know if that's the rite procedure.

#include <iostream>
#include <string>
using namespace std;

int main()
{

string arnList[9]={"John", "Dave", "Steve", "Kevin","Andrew","Scott","Colin","Timothy","Zenon"};
int nLength=9;
int nTemp;

for (int iCv = 1; iCv < nLength; ++iCv)
    {
      //the new value to be inserted into a temporary location 
      nTemp = arnList[iCv];
      // k is the index of the number to the left of the iCv.
      for (int k = iCv-1; k >= 0 && arnList[k] > nTemp; k--)
      {
        arnList[k+1] = arnList[k];
      }
      arnList[k+1] = nTemp;
    }
    for(iCv=0;iCv<nLength;iCv++) cout<<arnList[iCv]<<" ";
    cout<<endl;
return 0;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,009
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 409
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Sort in Alphabetical order

  #2  
Dec 13th, 2006
>i don't know if that's the rite procedure.
The string class is designed to act like a first class type, so the operations you use on numbers should work for string objects as well. There are a few problems with your code though.

>int nTemp;
nTemp is the temporary storage for the item you save. Since you're saving a string, ntemp should be defined as a string:
string nTemp;
>for (int iCv = 1; iCv < nLength; ++iCv)
One of the things you need to keep in mind with for loops is if you declare the counter in the loop, you can't use it after the loop. If you need iCv after the loop ends (which you do), it has to be defined outside of the loop:
int iCv;

for (iCv = 1; iCv < nLength; ++iCv)
>for (int k = iCv-1; k >= 0 && arnList[k] > nTemp; k--)
Ditto here. You use k after the loop, so it can't be defined inside the initialization clause.

The fixes are minor. All in all, you were very close to a working solution.
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string arnList[9]={"John", "Dave", "Steve", "Kevin","Andrew","Scott","Colin","Timothy","Zenon"};
  int nLength=9;
  string nTemp;
  int iCv;

  for (iCv = 1; iCv < nLength; ++iCv)
  {
    //the new value to be inserted into a temporary location 
    nTemp = arnList[iCv];
    // k is the index of the number to the left of the iCv.
    int k;

    for (k = iCv-1; k >= 0 && arnList[k] > nTemp; k--)
    {
      arnList[k+1] = arnList[k];
    }
    arnList[k+1] = nTemp;
  }
  for(iCv=0;iCv<nLength;iCv++) cout<<arnList[iCv]<<" ";
  cout<<endl;

  return 0;
}
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Dec 2006
Posts: 7
Reputation: GuruGhulab is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
GuruGhulab GuruGhulab is offline Offline
Newbie Poster

Re: Sort in Alphabetical order

  #3  
Dec 13th, 2006
YEp worked, thanks Narue, for the help,
reputaion added,
take care,
Bye
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:06 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC