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

Username:   Password: 
 RegisterRegister   
 sorting once again...
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tallguy




PostPosted: Fri Feb 13, 2009 12:55 pm   Post subject: sorting once again...

hey guys, back to sorting and arrays FUN!!!....NOT
any ways here is my problem

Quote:
Write a program which inputs a number of names in any order. Use a sentinel to stop
user input. Output to the screen the names in alphabetical order so that there are no
duplicate names output. At the end of the program output the names of the duplicates.


i have it done, except it always deletes one name, and i have no idea why...can someone help?
thanks



vandermout_array#2.t
 Description:

Download
 Filename:  vandermout_array#2.t
 Filesize:  1.19 KB
 Downloaded:  56 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Fri Feb 13, 2009 1:17 pm   Post subject: RE:sorting once again...

In your duplicate method, you're never outputting the first entry in the array. It's not deleted, it's just not shown. Add
code:
put names(1)
at the start of the method to fix it.

I should also point out that your duplicates-detecting algorithm fails if you have more than two copies of a single name. For example, type in "joe" three times and it outputs "joe, joe," for duplicates.
copthesaint




PostPosted: Sat Feb 14, 2009 10:03 am   Post subject: RE:sorting once again...

code:
/*2. Write a program which inputs a number of names in any order. Use a sentinel to stop
 user input. Output to the screen the names in alphabetical order so that there are no
 duplicate names output. At the end of the program output the names of the duplicates.
 Save as lastname_array#2.t.*/


var number : int
var strg : string
loop
put "How many names are there?"
get strg
put ""
cls
if strintok (strg) = true then
number := strint (strg)
exit
else
put "Wrong character"
end if
end loop
var doublednames : array 1 .. number * number of string
var names : array 1 .. number of string
var tempory : string := " "
var random : boolean := true
var doubleint : int := 0
put "Enter ", number, " Names"
put ""
for i : 1 .. number
    get names (i)
    cls
end for
for i : 1 .. number
    for j : 1 .. number
        if i not= j then
            if names (i) = names (j) then
                doubleint += 1
                doublednames (doubleint) := names (i)
            end if
        end if
    end for
end for
cls
put "The names in order are.."
for i : 1 .. number
    put names (i)
end for
put "The duplicated names are.."
for i : 1 .. doubleint
    put doublednames (i)
end for

Now this works but you would have to figure out how to Eliminate 1 set of each double. Because if you put 5 of one name it will out put 25 doubles.
Also you can choose if you will learn for this program. Your only hurting your self if you don't take the time to understand this.
Tallguy




PostPosted: Tue Feb 17, 2009 8:35 am   Post subject: Re: sorting once again...

DemonWasp wrote:

In your duplicate method, you're never outputting the first entry in the array. It's not deleted, it's just not shown. Add
code:
put names(1)
at the start of the method to fix it.

I should also point out that your duplicates-detecting algorithm fails if you have more than two copies of a single name. For example, type in "joe" three times and it outputs "joe, joe," for duplicates.


umm, okay....
so this is my duplicate thingy...
code:

proc duplicate
    for i : 2 .. number
        if names (i) = names (i - 1) then
            tempory := names (i) + "," + tempory
        else
            put names (i)
        end if
    end for
end duplicate

would i put
code:
put names(1)
after the else statement, or in the if?


I got it to work, a bit unorthodox though, see what you think...



vandermout_array#2.t
 Description:

Download
 Filename:  vandermout_array#2.t
 Filesize:  1.23 KB
 Downloaded:  42 Time(s)

DemonWasp




PostPosted: Tue Feb 17, 2009 9:17 am   Post subject: RE:sorting once again...

That method will also work; all you've done is shifted which element is the special case from the first element to the last element. Instead of having
code:
put names(1)
immediately before the for-loop, you've got
code:
put names(number)
immediately after the for-loop. It works out to the same thing.

You do, however, still have the problem with multiple-duplicates. If you put in the same name more than twice, it will appear in the "duplicates" list more than once, which I would assume to be incorrect behaviour.
Tallguy




PostPosted: Tue Feb 17, 2009 1:28 pm   Post subject: RE:sorting once again...

oo, okay thanks
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  [ 6 Posts ]
Jump to:   


Style:  
Search: