What's new

Closed I need your logic please.

Status
Not open for further replies.

Princess Serrano

Eternal Poster
Joined
Apr 2, 2016
Posts
429
Reaction
285
Points
285
Supposed to be im going to create an java console program that computes the basic statistics function ( mean median and mode ) .

For mean and median, they are okay because they are powered by formula. But how about Mode? Regardless the formula for mode, but the basic operation is to count which data has the highest frequency.

For example.
I let my user to input the number of data.

Data to be input { 2,5,5,6,4,7 }
Number of data = 6

After i ask the number of data then i will create and initialized an array where datas would be inputed.

Now, imagine the stored value are { 2,4,5,5,5,6,2,8,8 }

Seeing the data given, we can quickly conclude that 5 is the mode for it has the greater number of occurence.

This is the question: How could i determined which number in the given data has the greatest number of occurence.

I hope you understand, i will wait for your feedback. Thanks and Godbless.
 
Maybe make a variable for each number that can count their quantity?

Example of variables:
count_num1 = 0
count_num2 = 0
count_num3 = 0
. . .
count_num9 = 0

For each loop you generate a number for an array and identify what number it is then give +1 to the variable with the same number.

Example:
Loop 1 {
if( number = 1 ) {
count_num1 += 1; }
elseif( number = 2 ) {
count_num2 += 1; }
. . .
elseif( number = 9 ) {
count_num9 +=1; }
} //end of loop 1

To determine which number has the most occurence. All you have to do is compare all count_numx variables' value if what number have the highest value.

Sorry if this is not the answer you are looking for. But I hope it helps you.
 
Last edited:
MODE



public static int mode(int a[]) {
int maxValue, maxCount;

for (int i = 0; i < a.length; ++i) {
int count = 0;
for (int j = 0; j < a.length; ++j) {
if (a[j] == a) ++count;
}
if (count > maxCount) {
maxCount = count;
maxValue = a;
}
}

return maxValue;
}
 
MODE



public static int mode(int a[]) {
int maxValue, maxCount;

for (int i = 0; i < a.length; ++i) {
int count = 0;
for (int j = 0; j < a.length; ++j) {
if (a[j] == a) ++count;
}
if (count > maxCount) {
maxCount = count;
maxValue = a;
}
}

return maxValue;
}

if(a[j] == a [ i ]

maxValue = a [ i ];
 
count occurrences of each number
your sample output: [2,4,5,5,5,6,2,8,8]

Maraming puwedeng gawin:

Heto ang isa.

step1: duplicate the result array - [2,4,5,5,5,6,2,8,8]
step3: then remove all duplicates (not manually, use code) - matitira na lang ay [2,4,5,6,8]
step4: ngayon meron ka ng:

originalArray[2,4,5,5,5,6,2,8,8]
duplicateArray[2,4,5,6,8]

step 5: now find the "occurrences" of each of the values inside duplicateArray in the originalArray.
Parang ganito:
- ang number 2 ba ay present sa orginalArray? kung oo, ilan?
- ang number 4 ba ay present sa orginalArray? kung oo, ilan?
- ang number 5 ba ay present sa orginalArray? kung oo, ilan?
- ang number 6 ba ay present sa orginalArray? kung oo, ilan?
- ang number 8 ba ay present sa orginalArray? kung oo, ilan?
 
Status
Not open for further replies.

Similar threads

Back
Top