|
|
 |
| 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 .
|
 |
|
|
 |

May 17th, 2008, 02:11 PM
|
|
Authorized User
|
|
Join Date: Jan 2007
Location: , , .
Posts: 46
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
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
|

May 19th, 2008, 06:18 AM
|
|
Friend of Wrox
|
|
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 478
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|

May 20th, 2008, 04:09 PM
|
|
Authorized User
|
|
Join Date: Jan 2007
Location: , , .
Posts: 46
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
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
|

July 12th, 2008, 09:43 PM
|
|
Authorized User
|
|
Join Date: Jan 2008
Location: , , .
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

October 16th, 2008, 01:43 AM
|
|
Authorized User
|
|
Join Date: Oct 2008
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |