Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions