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

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




PostPosted: Fri Dec 12, 2003 12:02 am   Post subject: procedure problem

I need to write a procedure that gets numbers from the user and display the mode (most frequently occurring number).

To begin i asked the user for the number of numbers he/she is going to enter. Then in a for loop, i stored the numbers in the array. I then passed this into a procedure.

Now the problem lies is how i can find the mode. I had an idea of finding the greatest number and smallest number in the array. Then creating a new array with values from smallest to greatest. Then i can compare the values of my 1st array from user and with my new array. Then i can set a "if statement" if the numbers from the 1st array = my new array.. then i can add a counter. In the end of all this looping... i can then compare the values of the counter and display the mode..

This basic algorithm.. is all i have so far. Could anyone perhaps propose a better idea of a program to solve this problem or perhaps show me the source code Smile

ty in advance Razz Razz
Sponsor
Sponsor
Sponsor
sponsor
AsianSensation




PostPosted: Fri Dec 12, 2003 12:10 am   Post subject: (No subject)

sort the list, once the list is sorted, all you have to do is keep a counter. increase the counter if the element in the array is equal to the previous. Once the element aren't equal, record the value in the counter and then reset the counter, keep on doing this until you went through the array.
McKenzie




PostPosted: Fri Dec 12, 2003 4:40 am   Post subject: Re: procedure problem

kyoshiro wrote:
This basic algorithm.. is all i have so far. Could anyone perhaps propose a better idea of a program to solve this problem or perhaps show me the source code Smile

Umm...I think you forgot to attach the code you were working on. AZN's way works. I prefer to handle it a little different.
1. Count how many times each element occurs (using and array of counters)
e.g.
original : 4 3 4 5 6 3 2 4
counters: 3 2 3 1 1 2 1 3
2. Find the position of the highest number in the counter array.
3. Result the original at the same position (I know you said procedure, but it works better as a function)
DanShadow




PostPosted: Fri Dec 12, 2003 4:23 pm   Post subject: (No subject)

hmmm... if I understood the question correctly...this might work.
make an array (counter) and a variable (intin) and an array (numbers). Then in a "for" statement, (1..length (numbers)) asking for numbers, then adding them to the array.
for i:1..length(numbers)
put "Enter a number: "..
get intin
numbers(i):=intin
end for
^^Use something like that, then at the end, have a "for" statement that goes through the numbers array, and adds to the "counter" array whenever a number is found twice or more. then check the "counter" arra y, see which is highest...like
for i:1..length(counter)
if counter(i)>greatest_num then
greatest_num:=counter(i)
end if
end for
^^Like that...I hope you get the idea
Andy




PostPosted: Fri Dec 12, 2003 5:42 pm   Post subject: (No subject)

dude, your method is not as good as azns... read his. its so much more effiecient... u should read other ppl's replies before posting your own cuz if you dont have anything better, dont bother posting it
AsianSensation




PostPosted: Fri Dec 12, 2003 7:25 pm   Post subject: (No subject)

come on andy, not even Asok was that harsh, be a bit more positive, it will make the world a much better place. Seriously though, saying other people's code "sux" or "gay" doesn't solve the problem. Good thing you don't have powers in the Turing forum, if you did, next thing we know, dodge comes here blazing with all the flamethrower and guns you could possibly have.
Mazer




PostPosted: Fri Dec 12, 2003 7:45 pm   Post subject: (No subject)

tru dat, AsianSensation, tru dat.

dodge - i'm disappointed in you. you've strayed from the path of the whatdotcolor warrior.
Andy




PostPosted: Fri Dec 12, 2003 8:31 pm   Post subject: (No subject)

wtf are u guys talking about? i did no such thing
McKenzie




PostPosted: Fri Dec 12, 2003 11:39 pm   Post subject: (No subject)

Hmmm ... Shocked
am I the "dude" you're talkn'(typin') bout Question
harsh dodge, harsh ...
I'll try better next time Wink
Andy




PostPosted: Sat Dec 13, 2003 4:55 pm   Post subject: (No subject)

no sir it was danshadow, and how was i harsh?
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  [ 10 Posts ]
Jump to:   


Style:  
Search: