Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
procedure problem
Author Message
kyoshiro

Posted: 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

AsianSensation

Posted: 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

Posted: 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

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)

Posted: 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

Posted: 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

Posted: 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

Posted: 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

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

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

McKenzie

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

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

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 10 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: