GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Apr-2007, 04:54
wilen wilen is offline
New Member
 
Join Date: Apr 2007
Posts: 3
wilen is on a distinguished road

How to sort in C++ alphabetically


I'm trying to figure out how to sort if I enter 10 different names then the program will sort it alphabeticaly using array this is not my assignment. I'm just practising c++.
  #2  
Old 18-Apr-2007, 08:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,712
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: need help how to sort in C++ alphabeticaly


Quote:
Originally Posted by wilen
im trying to figure out how to sort if i enter 10 different names then the program will sort it
1. How are you representing the list of the names?

Array of pointers to char, where each pointer has the address of an array of characters (a "C-style string")? Array of C++ std::strings? Vector of strings? What?


2. Suppose you had an array of integer variables. How would you sort them?


Regards,

Dave
  #3  
Old 19-Apr-2007, 01:36
wilen wilen is offline
New Member
 
Join Date: Apr 2007
Posts: 3
wilen is on a distinguished road

Re: need help how to sort in C++ alphabeticaly


only array of integer

e.g the user will be asked to input ten times different name, after they input the ten names the program will ask if the user wants to sort it alphabetically in descending or ascending is fine.
  #4  
Old 19-Apr-2007, 08:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,712
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: need help how to sort in C++ alphabeticaly


Quote:
Originally Posted by wilen
only array of integer

e.g the user will be ask to input ten times diffirent name after they input the
ten names the program will ask if the user wants to sort it alphabeticaly in decending or acending is fine

I hate to repeat myself, but I'm thinking that you don't understand why I asked these questions:

Quote:
Originally Posted by davekw7x
1. How are you representing the list of the names?

Array of pointers to char, where each pointer has the address of an array of characters (a "C-style string")? Array of C++ std::strings? Vector of strings? What?
There are two reasons that I asked this question:

Firstly: I want to know what you are working with and what you can use. This is the C++ board, and using C++ strings and vectors might be appropriate if people have had these topics in class. On the other hand, many beginning classes in C++ actually cover topics that are consistent with C, that is arrays of chars and arrays of pointers to chars.

Secondly: I want to know how much you have thought about how you will solve the problem. This means that in order to sort a list of names, first I must have a list of names. What is a list of names? Some kind of data structure that is suitable for sorting. How are you going to get names into the list? Merely repeating the assignment doesn't give me any of this information about your situation in completing the assignment.

So I still don't know your answer, and I personally don't know how to start helping you. Maybe there are other people reading this thread who can help.

Quote:
Originally Posted by davekw7x
2. Suppose you had an array of integer variables. How would you sort them?
The purpose of this question was to try to get you to tell me what topics you have covered in class, and, therefore to give me some kind of clue about what to suggest. If you have had bubble sort and nothing else, then this could be used to sort a list of names. If you have had quicksort, then this suggests that your class is somewhat more advanced. If you have been able to use standard library sorting functions (qsort from <cstdlib> or sort() from <algorithm>), then the actual program may be simpler but you might need some help in getting your comparison functions properly formed to be able to use them.

So I still don't know your answer, and I personally don't know how to start helping you. Maybe there are other people reading this thread who can help. (But I said that already.)


Regards,

Dave
  #5  
Old 19-Apr-2007, 21:54
wilen wilen is offline
New Member
 
Join Date: Apr 2007
Posts: 3
wilen is on a distinguished road

Re: need help how to sort in C++ alphabeticaly


ok just to clear this up because i dont know the sort function in C++
using array

e.g the name to be inputed of the use is the ff.


joker
eric
aileen

after the user type this name how the program will sort this to become like
the ff.

aileen
eric
joker



CPP / C++ / C Code:
#include<iostream>
#include<string.h>

using namespace std;

int main()
{
char name[20], name1[20], name2[20];

cout<<"enter your name: "<<endl;
cin>> name;
cout<<"enter a different name: "<<endl;
cin>> name1;
cout<<"enter a different name again: "<<endl;
cin>> name2;
cin.ignore();
cin.get();
}

the code above will not sort the name it will ask the user to input a different name 3 times if you dont mind helping me again to make it sort the name
after the user inputed it 3 times
  #6  
Old 20-Apr-2007, 14:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,712
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: need help how to sort in C++ alphabeticaly


Quote:
Originally Posted by wilen
the code above will not sort the name it will ask the user to input a different name 3 times if you dont mind helping me again to make it sort the name
after the user inputed it 3 times

In general, if you want to make a program that sorts three things, you can create three different variables and make some simple code to do a few comparisions and swaps values of the variables until the names and the values correspond to some sorted list.

For example, suppose you have three "somethings". I will give them different names: item1, item2, item3. Each of the "somethings" has a value, and I want to rearrange things, somehow, so that item1 will have the smallest value, and item3 will have the largest value.

One simple way is to do something like this:

Code:
IF (the value of item1 is greater than the value of item2) THEN swap the values of item1 and item2 END IF IF (the value of item2 is greater than the value of item3) THEN swap the values of item2 and item3 END IF IF (the value of item1 is greater than the value of item2) THEN swap the values of item1 and item2 END IF

At this point, the value of item1 will be less than or equal to the value of item2 and the value of item2 will be less than or equal to the value of item3.

In other words the list of three items has been sorted.

Try it with pencil and paper. With three items there are exactly six different starting configurations, so it's not too hard or too time consuming to go through all six. Instead of names, suppose we have a list of three integers. I will use a list consisting of the integers 1, 2, and 3.

Starting Configuration 1: 1 2 3
Starting Configuration 2: 1 3 2
Starting Configuration 3: 2 3 1
Starting Configuration 4: 2 1 3
Starting Configuration 5: 3 1 2
Starting Configuration 6: 3 2 1

Go the three steps that I show for each of the six starting configurations, and you should end up with "1 2 3" each and every time.

Now, there are other ways to do the deed, but I like this way because it is extensible into a more general purpose method to sort any number of items using loops (It is actually a prototypical bubble sort for three items.)


If you want to make a program that sorts ten things, you can create ten different variables and do something similar. (But the number of comparisons and the amount of logic makes me want to do it differently, somehow. It's just too much work. Try it if you want to. Maybe I will try loops or some such programming thingie.)

If you want to make a general purpose program that can sort lists of different lengths (that is, the length of the list is not known when you write the program, but will be read in as part of the problem), then you really have to consider something other than making a different variable for each item in the list. Yeah, those loop thingies.

It's hard to think of a way to have the program create new variable names for an unknown number of list items. In C, we do it with arrays. That is we have a bunch of itms of the same type. They all have the same name (the name of the array), and we access each item with an index.

So, if we have an array of 100 ints, declared as int x[100];, we can access the individual members with the notation x[i] for values of i that are greater than or equal to zero and less than or equal to 99.

Then, we make a program that puts the items in an array. Then we make some program constructs (loops) that access the members the arrays, somehow, and make comparisons and swap the values of some of the array elements where appropriate.

So: if you want to sort a list that consists of ints, you start with an array of ints.

If you want to sort a list of names, then you start with an array of names.

Here's the thing: an int is a built-in C language data type (C++ also). You make an array with a fixed number of ints just by declaring it. (int x[100];, for example, to declare an array of 100 ints.) This is perfectly satisfactory if you know ahead of time what the maximum number of elements in a list will be. You can read in as many as there are (as long as there aren't more than the number that you allocated) and work on them in your program with loops and stuff.

Or

You can dynamically allocate memory during the program execution itself (with the standard library function malloc()) and, by knowing about C index and pointer notation, you can treat the resulting block of memory as an array of ints. That is, you can use array notation with a pointer whose value was assigned to be something returned by malloc().

A name like the ones in your illustration is sometimes called a "string", but there is no "string" data type in C (or C++ either, but there is a standard library class called std::string, that is really the way to go, when you get ready for it).

Anyhow back to your example: your names are stored in char arrays. In C, so-called "C-style strings" used in all of the standard library string functions, and almost universally used by C programmers everywhere, consist of a sequence of chars, terminated by a zero byte (a char whose bits are all zero). In other words a "string" in C is stored in an array of chars.

Analogous to the statement about needing an array of ints to sort a list of ints: If you want to sort a list of names, you need an array of names. If your names are going to be stored in arrays of chars, then you need an array of (arrays of chars). In C this is sometimes called a 2-Dimensional array of chars, but it really isn't; it's an array of array of chars. (Although notation that makes it look like a 2-Dimensional array is defined to make it easy to use such things.)

Just as in arrays of ints: there are more than one way to create a data structure that you need to sort a list of names. If you know ahead of time how many names are on the list (or, at least, the maximum number of names) and you know what the longest name will be (or, at least the length of the longest possible name), you can just declare a 2-dimensional array of chars.
For example char names[100][21]; declares an array that can be used for up to 100 names, where each name is 20 chars long. (The '21' is needed since the terminating zero byte takes up a memory location, but isn't actually part of the name that you will enter and sort and print.)

There are ways to use malloc() to allocate storage for as many names as you need, and to allocate just enough storage for each name as you read it. Let's leave that for later, OK?

The logic to sort the array of names is just like the logic to sort an array of ints, but the details are necessarily different, due to the fact that "strings" are not built into the C language and certain standard library functions would be used to compare one "string" with another and to copy one "string" to another. (It's actually easier in C++ with the standard library string class, but you have more to learn before you get started.)

So: Where would you like to start?

Go back and look at my previous questions: What kind of data structure will you use for your list of names? How will you get the names into the list?

What sort algorithm will you use?

If you haven't ever implemented a sorting algorithm, I respectfully suggest that you make sure that you can sort a list of integers before trying a list of names. You don't have to do it in that sequence, but it makes sense to me to try to learn one thing at a time.

Maybe like this:


1. Learn something about sorting. Use an array of ints as your data structure. Start with simple logic, but make sure that it will handle lists of varying lengths.

2. Learn something about arrays of chars and 2-Dimensional arrays. Learn how to get "names" into an "array of strings" or a 2-Dimensional array of chars. (Or if you are going to do this with C++ std::string objects, you can study them and then you can either have an array of std::string objects or, even more interestingly, a std::vector of std::strings.)

3. Look at the integer sort program that you have written and tested in step 1 and see what you can do about making a similar program to sort the list of names.

Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help with Bubble sort program that uses vectors instead of arrays sasuke101 C++ Forum 9 25-Oct-2005 11:26
Help with a bucket sort please. glulu76 C++ Forum 11 18-Apr-2005 15:33
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 08:48
insert sort saphir55 C Programming Language 4 06-Dec-2004 14:00
help with Sort arrays/Size justachessgame C Programming Language 1 12-Nov-2004 23:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:26.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.