Jump to content

Mode Program (java)


demigod

Recommended Posts

It seems really easy, but I just can't figure out how to find the mode. The decription of the program is as follows:

 

The mode of a list of numbers is the number listed most often. Write a program that takes 10 numbers as input and displays the mode of these numbers. Your program should use parallel arrays and a method that takes an array of numbers as a parameter and returns the maximum value in an array.

 

What I have so far is below. Any help would be appreciated.

 

 

 

 

import java.util.ArrayList;

import TerminalIO.KeyboardReader;

 

public class modeFinder

{

public static void main(String [] args){

 

KeyboardReader reader = new KeyboardReader();

int[] numbers = new int[10];

int[] mode = new int[11];

 

double total = 0;

int num;

int i = 0;

int j = 0;

int cc = 1;

int modenum = 0;

 

 

for (i = 0; i <= 9; i++)

{

num = reader.readInt("Enter an integer: ");

numbers = num;

}

 

for (i = 0; i <= 9; i++)

{

for (j = 0; j <= 9; j++)

{

if (numbers == numbers[j])

{

mode = cc;

}

}

}

 

for (i = 0; i <= 9; i++)

{

if (mode > mode[i+1])

{

modenum = i;

}

}

System.out.println("Mode: " + numbers[modenum]);

}

}

Share this post


Link to post
Share on other sites

When it says 'parallel arrays', I am guessing he wants you to use atleast two arrays to do this? I think if you sorted the original array you could probably do it with just the original array and a couple of variables maybe, otherwise I can only think of a way to do it with.. the original array of numbers and.. two extra arrays? Is that OK?

Share this post


Link to post
Share on other sites

first no need to imort ArrayList since your using static arrays. Second, Are you taking the AP computer Science A/AB exam in May or are you taking this in college? Ill have to think a second as to how to do this the way the assignment says bc i would have approached it very differently. Once i code it ill try to give you a hint.

 

EDIT: ok why in the HELL do you need "parrallel" arrays. I would code it to scan several times.....wait... humm crap btw use the

 code here 

tags to make your ocde more readable.

 

 

EDIT: ok again WTF?!?!? It says 1st find mode, next it says use parallel arrays (still dont now why) next it says you shouod use a method to find the max num in the array?????why mode doesnt need the max, mean would.....but I dont get it are these your exat directions typed? if not type it out EXACLTLY as the assignment reads then ill look at ti again, otherwise this is a really crappy assignment.

Edited by cchalogamer

Share this post


Link to post
Share on other sites

Badly worded perhaps? Probly just means the maximum value as like, the value that occurs most.

 

But yeah, you're right.. doesn't actually seem to need another array :s

 

EDIT: Actually, that was a little wrong :) Oops

 

EDIT: Has an assignment the wrong way around, should work properly now.. I think...

 

import java.util.Random;

public class Mode {
      public static void main(String args[]) {
             // random numbers for me to test with
             Random rndGen = new Random();
             int numbers[] = new int[10];

             for (int i = 0; i < numbers.length; i++) {
                 // change this to your keyboard input line
                 numbers[i] = rndGen.nextInt(10) + 1;
             }

             int occur = 0;
             int tmpOccur = 0;
             int mode = 0;

              for (int i = 0; i < numbers.length; i++) {
                 for (int j = 0; j < numbers.length; j++) {
                     if (numbers[j] == numbers[i]) {
                        tmpOccur++;
                        modes[j] = tmpOccur;
                     }
                 }

                 if (tmpOccur >= occur) {
                    occur = tmpOccur;
                    mode = i;
                 }

                 tmpOccur = 0;
             }

             System.out.println("mode = " + mode);
      }
}

 

Buut it doesn't use parallel arrays so I aint sure how much use it is to you. You could change it so it'll use a parallel array but that would be totally pointless...

 

EDIT : aaand you could probably do better than that by not looping from min to max :)

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...