Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
I forgot my password
Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 17th, 2008, 02:11 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 46
Thanks: 2
Thanked 0 Times in 0 Posts
Default Sorting Array in C++

Now my requirement is to sort 10 names from their with respect to their first character using bubble sort or any other sorting algorithm. I have my code here:


#include <iostream.h>
#include <conio.h>
#include <string.h>



main()
{
char name[10][25];


cout<<"Enter the Names:"<<endl;


//prompt user to input 10 names
for (int i = 0; i<=9; i++)
{
cout<<"Name"<<i+1<<": ";
cin>>name[i];
}



//display 10 names
for (int j=0;j<=9;j++)
{
cout<<name[j]<<endl;
}

getch();
}


MAXOOD!

Life is an endless journey towards perfection
__________________
MAXOOD!

Life is an endless journey towards perfection
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old May 19th, 2008, 06:18 AM
Friend of Wrox
Points: 1,472, Level: 15
Points: 1,472, Level: 15 Points: 1,472, Level: 15 Points: 1,472, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 478
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Here's a generic bubble sort:
http://mathbits.com/mathbits/compsci/Arrays/Bubble.htm

You also need to look up strcmp which compares two strings, see for example:
http://www.cplusplus.com/reference/c...ng/strcmp.html

You may want to make sure that you truncate your strings to the 25 characters that you've allowed in the elements of your names array. You would typically do this with cin.read(char *buffer, int n) or use the setw function thus cin >> setw(25) >> name[i].



--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old May 20th, 2008, 04:09 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 46
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Dear ciderpunx, thankyou very much for your useful tips. You have really added to my knowledge. I have the complete code now to sort 10 names wrt their 1st character.Now can you please tell me how to modularise this code by using user-defined functions. I'm a newbie in C++ so bear with me.


#include <iostream.h>
#include <string.h>
#include <conio.h>





main()
{
      char name[10][25];
      char temp[25];

      /*How to write and use this code through my own function*/
      for(int i=0;i<=9;i++)
      {
              cout<<"\nEnter name"<<i+1<<": ";
              cin>>n[i];
      }
      /******************************************/
      cout<<"The sorted names are: "<<endl;

       /*How to write and use this code through my own function*/
      for (int iteration=0;iteration<=9;iteration++)
      {
          for(int k=0;k<=8;k++)
          {
              if (tolower(name[k][0])>tolower(name[k+1][0]))
              {
              strcpy(temp,name[k]);
              strcpy(name[k],name[k+1]);
              strcpy(name[k+1],temp);
              }
          }

      }

    for(int l=0;l<=9;l++)
    {
            cout<<name[l]<<endl;
    }
     /******************************************/
getch();

}

Thanks!

MAXOOD!

Life is an endless journey towards perfection
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old July 12th, 2008, 09:43 PM
Authorized User
 
Join Date: Jan 2008
Location: , , .
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.






Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old October 16th, 2008, 01:43 AM
Authorized User
 
Join Date: Oct 2008
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It is often necessary to arrange the elements in an array in numerical order from highest to lowest values or vice versa.If the array contains string values, alphabetical order may be needed.The process of sorting an array requires the exchanging of values. While this seems to be a simple process, a computer must be careful that no values are lost during this exchange.
Ex. Suppose that grade[1] = 10 and grade[2] = 8 and you want to exchange their values so that grade[1] = 8 and grade[2] = 10.
================================================
Victor
Our mission is to provide high quality end to end solutions to the BPO segment in a manner that will improve the operational efficiency while reducing the cost of the services to the client.
4thdimension1@gmail.com


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting 2 dimensional array rgalehouse Javascript How-To 2 December 2nd, 2006 09:46 AM
error when sorting an Array of Array nancy VBScript 2 February 17th, 2005 11:57 AM
sorting two dimensional array erin VBScript 0 February 10th, 2004 04:55 PM
Array Sorting Dinesh22 VB.NET 2002/2003 Basics 2 February 3rd, 2004 10:19 AM
Sorting an array roxusername PHP How-To 1 October 14th, 2003 01:16 PM



All times are GMT -4. The time now is 07:57 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
© 2011 John Wiley & Sons, Inc.