Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 17th, 2008, 02:11 PM
Authorized User
 
Join Date: Jan 2007
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
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
Reply With Quote
  #2 (permalink)  
Old May 19th, 2008, 06:18 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 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
Reply With Quote
  #3 (permalink)  
Old May 20th, 2008, 04:09 PM
Authorized User
 
Join Date: Jan 2007
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
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
Reply With Quote
The Following User Says Thank You to code_lover For This Useful Post:
  #4 (permalink)  
Old July 12th, 2008, 09:43 PM
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
  #5 (permalink)  
Old October 16th, 2008, 01:43 AM
Authorized User
 
Join Date: Oct 2008
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.
[email protected]


Reply With Quote





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.