Want to creat a ascending order sortin character array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 1
Reputation: alikoli is an unknown quantity at this point 
Solved Threads: 0
alikoli alikoli is offline Offline
Newbie Poster

Want to creat a ascending order sortin character array

 
0
  #1
May 17th, 2008
Hello every body..............
any one can help me about this program. I want to create a simple program in C++ i want
When the program runs they prompt
"Enter the Names:"
for example:
Name1: Abc
Name2: Cup
Name3: Altavista
Name4: Yahoo
Name5: Mbni
Name6: Sun
Name7: Mani
Name8: Google
Name9: Pakisan
Name10: Watan

Then tey asort these numbers and prompted.
"The Sorted names in ascending order are as:

Abc
Altavista
Cup
Google
Mani
Mbni
Pakistan
Sun
Watan
Yahoo

and thene they prompt

"Press any key to continue......."
and when i press any key then the program is exit.

Remember i have use a Dev-C++ editor to create a program so Please provide me codes in .cpp format and they run correctly in Dev-C++ editor. Because I am a very bigining in C++.
Thanks.
Ali.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Want to creat a ascending order sortin character array

 
0
  #2
May 17th, 2008
This looks like homework.

If it isn't, you can use the std::sort() algorithm.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 30
Reputation: Compton11 is an unknown quantity at this point 
Solved Threads: 0
Compton11 Compton11 is offline Offline
Light Poster

Re: Want to creat a ascending order sortin character array

 
0
  #3
May 18th, 2008
alikoli, is the program always going to prompt the user for 10 inputs (and only 10 inputs) or are there a variable number of inputs (as defined by the user)? Because if the user is always expected to input 10 names, then we can simply define an array with a defined size of 10 and then sort it from there. But if the user can enter any number of names as he/she wishes, this might be more involved (but not hard). If it's a variable number of names, then we might have to define some sentinel value, such as "Press 'e' to indicate that the user has finished entering all the names" and then sort the array from there. Of course, since array dimensions can't be calculated dynamically, we must define the array to have a default size of 100, for instance. I don't think anyone is going to do this assignment for you, but we are willing to help you if you actually try it. If you try and still get problems, post the errors and we'll try to assist you. Hope this helps!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Want to creat a ascending order sortin character array

 
0
  #4
May 18th, 2008
> Because I am a very bigining in C++.
So do the very beginning thing of reading in 10 names and printing them out again.

That at least would mean you've made a start, and not just another homework sponge looking for a free ride through life.
Also, read all the forum rules, which you obviously missed the first time around.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 14
Reputation: scarface3288 is an unknown quantity at this point 
Solved Threads: 1
scarface3288 scarface3288 is offline Offline
Newbie Poster

Re: Want to creat a ascending order sortin character array

 
0
  #5
May 18th, 2008
Please take this help lightly , don't be a sponge actually try to understand why the function I show you works and you will be better off. All programs can be written using regular conditional statements but this usually takes a long time to write. Try to think about what you want to do first on paper, then try to translate it into code. The more functions and implementations you know how to use helps alot so google functions you don't know and try to understand them so you can use in the future. Now on to your problem.

There are many sorting algoritims( google this) out there that you can use to do this.
look up the command strcmp()(google this), and know your logic(conditional statements(google this).

Definition

sorting - an operation that segregates items into groups according to a specified criterion;

in your situaton you want to segerate the names into groups accoding to alphabetical order.

Simple case

Names

B
A
C

should be

A
B
C

implementation of strcmp()

string letters[3] = { "B", "A","C" };

int i,j, min, min_pos;
char min_name[5], temp[5];

for(i=0;i<3;i++)
{
        strcpy(min_name, letters[i]);//takes the first letter as the index
        min_pos=i;
        for(j=i;j<3;j++)
        {
              	if ((strcmp(min_name, letters[j])>0))
	{
		strcpy(min_name,letters[j]);
		min_pos=j;// rember position
	}
        }
        strcpy(temp,letters[i]);
        strcpy(letters[i],letters[min_pos]);
        strcpy(letters[min_pos],temp);
}
Now if you cout or output into a file the letters[i] array it should be in alphabetical order.
Last edited by scarface3288; May 18th, 2008 at 3:22 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC