Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SO I AM HAVING DIFFICULTY COMPREHENDING THIS PROGRAM. IT GIVES YOU THE MOST FREQ

ID: 3839660 • Letter: S

Question

SO I AM HAVING DIFFICULTY COMPREHENDING THIS PROGRAM. IT GIVES YOU THE MOST FREQUENTLY OCCURING NUMBER IN AN ARRAY.

COULD SOMEBODY ANSWER THE QUESTIONS BY ADDING COMMENTS BELOW EACH QUESTION???

import java.util.*;

public class lastIndexOf

{

public static void main(String[] args){   

int a[]={27, 15, 15, 11, 27, 27};

int b[]={27, 15, 15, 11, 27,15};

  

int c[]={27, 15,11, 15, 11, 27};

System.out.println("The mode of a is " + mode(a));

System.out.println("The mode of b is " + mode(b));

System.out.println("The mode of c is " + mode(c));

}

  

public static int mode(int[ ] array) {

//QUESTION: Why do we need to make a new array that is 101 in length?

      

int[ ] spareArray = new int[101];

  

for (int i = 0; i < array.length; i++) {

  

//QUESTION: I get that this loop will keep going on until the length of the array is met,

//           but why do we need this 'for' loop if it copies the original array into "spareArray"?

//           Couldn't we just use the original 'array' for this?    

//QUESTION: Does the ++ at the end add "1" to the array index value at the index if it happens twice?

//QUESTION: Would spareArray (for int [ ] a) only have values of: 2 on index 15 ... 3 on index 27 .. and 2 on index 11?

//Would all the other index numbers be at 0 then for spareArray?

spareArray[array[i]]++];

}

  

//QUESTION: Why do we even need this variable?! What's the purpose of it and why is it at 101?

  

int mode = 101;

//QUESTION: Why do we need this?

  

int count = 0;

  

//QUESTION: Wouldn't this loop be exactly like the first loop?

  

for (int i = 0; i < spareArray.length; i++) {

  

//QUESTION: Wouldn't this "if" variable be true 100% of the time since count is 0?

//           or would count's new value loop within this 'for' loop

//           Example: int[ ] a: @1 = 27 > 0 --> 27 ...... @2 = 15 > 27 (print nothing, count still equals 27)    

      

if (spareArray[i] > count) {

//QUESTION: Would the 'count' always stay at the value of the last array index that got substituted for it? (same as above question)

   count = spareArray[i];

//QUESTION: Why do we make 'mode' equal to the "i" value of the array?! That would only mean that the index number is kept track?

//           Why do we even need to know about the index number?        

  

   mode = i;

      

}

}

  

//QUESTION: What does 'return' do to the mode? Does it store it somewhere and tally it up or something? This is my biggest question.

  

return mode;

}

}

Explanation / Answer

Okay, First of all lets summarize the function before moving on to your question. Here's how the function is supposed to work. What it does is, it counts the number of times each element appears in the array, and stores it in spareArray.

spareArray[x] will give you the number of times x appears in the original array. Of course, this will work only if the lements in your array array are between 0 and 100(both inclusive)

Once you have the spareArray, the program traverses through the spareArray, to find the maximum element, that will be the maximum frequency of any element in the original array(as spareArray[x] is the frequency of x in the original array) and hence the index with maximum freqency (that is, x here becomes the mode).

Lets take 'a' as an example here:

array = {27, 15, 15, 11, 27, 27}

Spare array will be:

{0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

that is spareArray[11]=1, spareArray[15]=2 and spareArray[27]=3, the other (101-3 = )98 values are all 0.

Maximum element here is 3, which is at index 27, hence the mode becomes 27.

Now, that the summary is clear lets answer your questions, each answer appears in bold:

public static int mode(int[ ] array) {

//QUESTION: Why do we need to make a new array that is 101 in length?

We need this new array to store frequncies of each element, as explained, spareArray[x] would store the frequency of x in the array.

int[ ] spareArray = new int[101];

  

for (int i = 0; i < array.length; i++) {

  

//QUESTION: I get that this loop will keep going on until the length of the array is met,

//           but why do we need this 'for' loop if it copies the original array into "spareArray"?

//           Couldn't we just use the original 'array' for this?

No, it doesn't just copy the original array into the spare array, look closely. "spareArray[array[i]]++" increments the value of spareArray[i].

//QUESTION: Does the ++ at the end add "1" to the array index value at the index if it happens twice?

No, it increments the value of spareArray[array[i]], so if array[i] = 11, this will increment spareArray[11], that denotes the frequency of 11 in the original array

//QUESTION: Would spareArray (for int [ ] a) only have values of: 2 on index 15 ... 3 on index 27 .. and 2 on index 11?

//Would all the other index numbers be at 0 then for spareArray?

Yes you got that right. However there seems to be a type the value at index 11 is 1 and not 2.

spareArray[array[i]]++;

}

  

//QUESTION: Why do we even need this variable?! What's the purpose of it and why is it at 101?

This is the element with maximum frequency, it has just been initialized to 101, it will later be over-written

int mode = 101;

//QUESTION: Why do we need this?

This count keeps the maximum value of any element in the spareArray (which is the maximum frequency)

int count = 0;

  

//QUESTION: Wouldn't this loop be exactly like the first loop?

No, this one loops over spareArray and not array  

for (int i = 0; i < spareArray.length; i++) {

  

//QUESTION: Wouldn't this "if" variable be true 100% of the time since count is 0?

//           or would count's new value loop within this 'for' loop

//           Example: int[ ] a: @1 = 27 > 0 --> 27 ...... @2 = 15 > 27 (print nothing, count still equals 27)    

You have to loop over spareArray and not array. Hence, @11 1>0 -> count =1, @15 2>1 -> count =2, @27 3>2 -> count=3

if (spareArray[i] > count) {

//QUESTION: Would the 'count' always stay at the value of the last array index that got substituted for it? (same as above question)

No, count will be the maximum value of spareArray at any point

   count = spareArray[i];

//QUESTION: Why do we make 'mode' equal to the "i" value of the array?! That would only mean that the index number is kept track?

//           Why do we even need to know about the index number?        

Yes, we need to. As explained the value is maximum frequency if the 'index', since we need to find the mode, we need to know which element has the maximum frequency and the index would exactly tell us that

mode = i;

      

}

}

  

//QUESTION: What does 'return' do to the mode? Does it store it somewhere and tally it up or something? This is my biggest question.

This will simply return the value of mode to where the function is called from. In out case System.out.println("The mode of a is " + mode(a)); the 'return mode;' statement will return 27 and this will replace the call 'mode(a)' in the print call, and will print "The mode of a is 27" as output.

return mode;

}

}