Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Arrays help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
PerylDemise




PostPosted: Thu Apr 06, 2006 9:05 pm   Post subject: Arrays help

I'm supposed to make a program that asks the user how many student names they want to be stored. Then I have to take those names and display them again but in alphabetical order and I have to have a seperate list for the names that had been entered more than once. I don't know how to alphabetize the names. Please help ^^;

This is what I have so far; it isn't much just the basics.

code:

%variable for how many student's names will be entered
var number : int

%user decides how many arrays will be needed
put "How many names will you be entering"
get number

%restrieves the names from the user
var names : array 1..number of string
for i : 1.. number
get names(i)
end for
cls
%displays the names -- change to display them in alphabetical order
for i : 1.. number
put names(i)
end for
Sponsor
Sponsor
Sponsor
sponsor
codemage




PostPosted: Fri Apr 07, 2006 7:53 am   Post subject: (No subject)

Sounds like you'll have to use a sorting algorithm.

If you're just starting out (if that sounds like an obscure topic to you), you might want to do a search in the Turing forum for bubblesort or selection sort.

You read in the data to an array,
sort the array
output the sorted array.
Delos




PostPosted: Fri Apr 07, 2006 7:56 am   Post subject: (No subject)

For this we'll approach it on the basis that you'll just be using regular old arrays. Of course, it could be made a lot more dynamic with the use of flexy arrays, but I doubt your teacher would like that very much.

The names are entered and stored. The next task is to sort them. Sorting is one of the most beloved and utterly detested areas of programming, in general at least. The basic idea that we'll use here (also known as the Bubble Sort) is as follows:
- compare element k to element k+1
- if element k+1 is less than element k, swap their positions
- repeat through the array as needed, until all elements are in their rightful position

So, with the four numbers {5, 1, 2, 7}, the order of movement would be:
sorting:

{5, 1, 2, 7}
{1, 5, 2, 7}
% k1 and k2 exchanged
{1, 2, 5, 7}
% k2 and k3 exchanged


This is not much different when sorting strings. The easiest way to compare to strings is to do a direct 'greater than' or 'less than' comparison (i.e., "Hello" > "World" sort of thing).

If you want to see more powerful sort techniques that deal with numbers, check out zylum's legendary post on sorting in [Turing Source].

Now, onto the second half. You are to find out which elements are repeated and then add them to a second list. I don't particularly like this idea, but hey, it's a good exercise.
The first thing you might want to ask is "How do I know an element is repeated?" After sorting, this is simply a case of comparing pairs, nothing could be simpler.
Next, you'll ask "How large of an array should I make, what if I make it n elements long and I end up with n+1 repeats?" Again, sorting will help you tremendously here. This part of your description is a little vague, but this is how I interpretted it: "Create a secondary array to keep track of any element that has repititions." In other words, if "Hello" occurs twice, it would be listed once in the secondary array. Even if it occured 10 times, it would still be listed once in the secondary array.
This interpretation may be wrong, it depends on what your teacher wants.

So, to answer the second question: given that there are n elements in the array, the maximum number of repeats you could have would be n/2 - i.e., the array consists entirely of pairs. (This argument hinges directly on my above assumption).
Hence, once my array is sorted, I will create a new array of n/2 elements (What should you do if you have an odd number of elements in array1?). I will now search through array1 and identify an repeats. I will assign these to array2, but only once.

Go give it a try and post up your code when you're done so we can look at it and give you some feedback.
TokenHerbz




PostPosted: Fri Apr 07, 2006 3:03 pm   Post subject: (No subject)

i would look at the first letters on the names, as an int (by using ord), ofcourse befor this you make all the caps small (by -the correct number)

This is cause Capitals and smalls are places different, then simply fun threw a loop, if this num 1st then put, etc:



Note: My way might not be the simplist, just i like complexity... Smile
Delos




PostPosted: Fri Apr 07, 2006 4:50 pm   Post subject: (No subject)

And what about the words "Hello" and "Help"? Both words have the same first letter. If you're going for over-the-top complexity, you might want to go along those paths. Otherwise a simple

code:

if "Hello" > "Help" then
   put "Help", "" : 3, "Hello"
else
   put "Hello", "" : 3, "Help"
end if


would work perfectly well.
PerylDemise




PostPosted: Mon Apr 10, 2006 7:18 am   Post subject: (No subject)

ok. thanks. Very Happy How do you compare the words when they are variables that the user has to input. How would I do that? Everytime I try to do a variable value swap and then try to compare the two variables with each other, using the greater than or less than sign, it never works. It says my *just an example* number(i+1) doesn't have a value, but number (i) has retrieved a value already.
Delos




PostPosted: Mon Apr 10, 2006 7:55 am   Post subject: (No subject)

Let's see your code.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: