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

Username:   Password: 
 RegisterRegister   
 Removing duplicates from an array
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ssikmace




PostPosted: Tue Apr 13, 2004 10:17 am   Post subject: Removing duplicates from an array

How would I read a list of names from a file into an array and remove all duplicates. The first line has the number of names in the file. Any help would be appreciated.

This is what I have so far:

%Reads a list of names and sorts them into array then outputs the sorted list
var streamin : int
var nameCount : int
var name : string

open : streamin, "Names.txt", get
assert streamin > 0

nameCount := 0
loop
get : streamin, skip
exit when eof (streamin)
get : streamin, name
nameCount := nameCount + 1
end loop

close (streamin)

open : streamin, "Names.txt", get
assert streamin > 0

var Names : array 1 .. nameCount of string
for i : 1 .. nameCount
get : streamin, Names (i)
end for

close (streamin)

for i : 1 .. nameCount
put Names (i)
end for

I dunno where to go from here. Question
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Tue Apr 13, 2004 10:53 am   Post subject: (No subject)

Score them.

Create one array to hold the name/ID of each type of entry...eg. "1's", "2's", "Cats", "Llama"...

Create a 2nd array to count the total number of that type and hold a reference to it...eg.
secondArray(2).tot := 3
secondArray(2).ref := "1;2;3"

Something to that effect...use records...makes your life easier.
Tony




PostPosted: Tue Apr 13, 2004 6:39 pm   Post subject: (No subject)

eh... sord them, then check if any two adjesent elements are the same. If so, remove one of them and continue checking
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Delos




PostPosted: Tue Apr 13, 2004 8:32 pm   Post subject: (No subject)

Umm...yeah. That's easier.
ssikmace




PostPosted: Wed Apr 14, 2004 10:01 am   Post subject: (No subject)

Ok thx ill give it a try.
ssikmace




PostPosted: Wed Apr 14, 2004 10:37 am   Post subject: (No subject)

Alright i got it to work. I had the program not add the name to the array if it was already in the array.

var streamin : int
var nameCount : int
var name : string

open : streamin, "Names.txt", get
assert streamin > 0

nameCount := 0
loop
get : streamin, skip
exit when eof (streamin)
get : streamin, name
nameCount := nameCount + 1
end loop

close (streamin)

open : streamin, "Names.txt", get
assert streamin > 0

var Names : array 1 .. nameCount of string (30)
var arrayCount := 1
for i : 1 .. nameCount
get : streamin, Names (arrayCount) : *
var found : boolean := false
for j : 1 .. arrayCount - 1
if Names (arrayCount) = Names (j) then
found := true
exit
end if
end for
if not found then
arrayCount := arrayCount + 1
end if
end for

put "Here are the names without Duplicates"
for i : 1 .. arrayCount - 1
put Names (i)
end for
ssikmace




PostPosted: Wed Apr 14, 2004 10:44 am   Post subject: (No subject)

Heres a funky program

function yesno (word : string) : boolean
var palin : boolean
var mid : string
var newword : string
newword := ""
for decreasing i : length (word) .. 1
newword := newword + word (i)
end for

result word = newword
end yesno


var word : string

put "Please enter a word to determine if it is a palindrome."

get word

put yesno (word)
ssikmace




PostPosted: Wed Apr 14, 2004 10:46 am   Post subject: (No subject)

var streamin, number : int
var filein := "names.txt"
var count : int

open : streamin, filein, get
assert streamin > 0


get : streamin, count

%reads a series of names and elimintes dupliiates%



var namelist : array 1 .. count of string (30)
var arraycount := 1
for i : 1 .. count
get : streamin, namelist (i)
var found : boolean := false
for j : 1 .. arraycount - 1
if namelist (arraycount) = namelist (j) then
found := true
exit
end if
end for
if not found then
arraycount := arraycount + 1
end if
end for
close : streamin
%outputs list without duplicates%
put "here are the names without the duplicates"
for i : 1 .. arraycount - 1
put namelist (i)
end for
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  [ 8 Posts ]
Jump to:   


Style:  
Search: