Perry Smith

Java Questions & Answers

Home / Java Q & A Archive Page 1 / Java Q & A Archive / Java Q & A Main Page

9-15-98

arrays

Can anybody help

I have just started learning java

i need to
a) have a declaration for an array of 20 integers

b) input values for the elements from the keyboard

c) determine and print the value in the array that occurs the most often.
e.g for the following
4,6,45,6,7,4,7,6.... the most occuring value is 6

I have manages a) & b)

stuck on c)

thanks

confused


Responses
Re: arrays

9-15-98

Just in case .. here's a) and b)

For my code, you'll have to have the following lines *before* your "class ClassName" line

import java.util.Vector;
import java.io.BufferedReader;

Vectors are dynamic arrays (the size can change --- very useful)
the BufferedReader will be used to get input from the user

you will also need the global constants/variables

private static final int ARRAYsIZE = 20;

a)

int iList[] = new int[ARRAYsIZE];

b)

public Integer[] getData
(
)
{
String strIn;
int iInput;
int iLoop = 0;
BufferedReader brIn = new BufferedReader(System.in);
/*
begin the loop to gather data
*/
while (iLoop < ARRAYsIZE)
{
System.out.println("Input an integer for data #" + ARRAYsIZE);
/*
get 1 line of user input
*/
strIn = brIn.readLine();
/*
attempt to convert the string into
an integer if possible
*/
try
{
iInput = Integer.parseInt(strIn);
IList[iLoop] = iInput;
iLoop++;
}
catch(NumberFormatException e)
{
System.out.println("Invalid input...Try again");
}
}
}

***very likely you noticed the try/catch pair there ... I wasn't sure whether to include it or not since you're new to Java, but it's there in case the user inputs bad data

now for part c
c)

well, you know that you're gonna have, at most, as many integers to test as you have array indexes... so, there are a few ways to implement this ... 1 clumsy method is to implement a 2-D Vector and use a binary search ... but this kinda complex and I don't know whether you're just new to Java ... or if you're new to programming altogether ... if you can't follow it ... post a reply letting me know ... and I'll rewrite this with less robust and less efficient but much easier-to-understand code ... and the math term for the most common number in a list is the "mode"

public void findMode
(
int iNums[];
)
{
/*
declare a vector which will be an ordered list of the data in the user-generated array ... we will make this vector 2-D by including 2-D arrays in each Vector element so ... say for your array, the first number in your array is a "4" ... the first element in this Vector will be an array containing a [4,1] representing the number and how many times it has been encountered
*/
Vector vecCount = new Vector();

/*
begin the loop to find the most common num
*/
for (int iLoop;
iLoop < java.lang.reflect.Array.getLength(iNums);
i++)
{
/* here's the logic below ... check if the number is in our list already, if so increment the number of times we've encountered it, if not then add it to the list (and and show that we've encountered it one time, of course)
*/
if (indexPresent(iNums[iLoop], vecCount) >= 0)
{
vecCount = incrementCount(iNums[iLoop], vecCount);
)
else
{
vecCount = addNewNum(iNums[iLoop], vecCount);
)
}
}

public int indexPresent
(
int iTest,
Vector vecToChk
)
{
/* implement a binary search ... uhh, I think I'll leave out the explanation of what a binary search is ... just trust me on this one ... it is the most efficient way to search a list (that I'm aware of)
*/
int iMin = 0;
int iMax = vecToChk.size();
boolean isFound = false;
int iRet;

while (!isFound && (iMin < iMax))
{
int iCurr = iMax + iMin / 2
int iTwo[] = new int[2];
iTwo = int[2] vecToChk.elementAt(iCurr);
int iNumFound = iTwo[1];
if (iNumFound = iTest)
{
isFound = true;
)
else
{
if (iNumFound > iTest)
{
iMax = iCurr - 1;
}
else
{
iMin = iCurr + 1;
}
}
}
/* return the index of the matching num or if it is not present, then return the additive inverse index of the number that closest to the number we're looking for minus 1
*/
if (isPresent)
{
iRet = iCurr;
}
else
{
iRet = (-1 * iCurr) - 1;
}
return (iRet);
}

public Vector incrementCount
(
int iToAdd,
Vector vecToChange
)
{
Vector vecTemp = new Vector();
vecTemp = vecToChange
int iToInc = indexPresent(iToAdd, vecTemp);
int iInVec[] = new int[2];

iInVec = (int[2]) vecTemp.elementAt(iToInc);
iInVec[2] = iInVec[2] + 1;
vecTemp.removeElementAt(iToInc);
vecTemp.insertElementAt(iInVec, iToInc);

return (vecTemp);
}

public Vector addNewNum
(
int iNew,
Vector vecToChange
)
{
Vector vecTemp = new Vector();
vecTemp = vecToChange;
int iInVec[] = new int[2];
int iInsert[] = new int[2];

iIndex = indexPresent(iNew, vecTemp);
iIndex = -1 * (iIndex + 1);

iInVec = (int[2]) vecTemp.elementAt(iIndex);
iFound = iInVec[1];
iInsert[0] = iNew;
iInsert[1] = 1;

if (iFound > iNew)
{
vecTemp.insertElementAt(iInsert, iIndex);
}
else
{
if (iIndex >= vecTemp.size() - 1)
{
vecTemp.addElement(iInsert);
}
else
{
vecTemp.insertElementAt(iInsert, iIndex + 1);
}
}
return (vecTemp);
}

that's all of it ... good luck

Mike


9-16-98

Re: Re: arrays

2 mistakes (that I see) in the prev reply ...

There's a typo in the getData method. "IList" should be "iList"

and 1 big mistake ... I left out the part that actually returns the mode (the most common number in a list) ...

before the last right bracket "}" in the "findMode" method ... there should be a line called
"System.out.println(getMode(vecCount));"

and getMode goes like this ...


private String getMode
(
Vector vecList
)
{
/*
declare a vector that will store the values
which have the highest frequency
*/
Vector vecMode
int iHiFreq = 1;
int iTwo[] = new int[2];
int iVal;
int iValFreq;

for (int i = 0; i < vecList.size - 1; i++)
{
/*
get the values from that ordered list we
stored in the Vector with the corresponding
frequencies
*/
iTwo = (int[2]) vecList.elementAt(i);
iVal = iTwo[0];
iValFreq = iTwo[1];
/*
test if the freq of this value is higher
than any we've found so far, if so then
make it the highest freq and clear all of
the elements in our Vector of modal values
and add this value as the 1st (and only
value in the vector of modes)
*/
if (iValFreq > iHiFreq)
{
iHiFreq = iValFreq;
vecMode.removeAllElements();
vecMode.addElement(new Integer(iVal));
}
/*
test if the frequency of this value is
equal to the highest frequency found,
if so, then add it to the list
*/
if (iValFreq = iHiFreq)
{
vecMode.addElement(new Integer(iVal));
}
}
/*
create the string which will display the
values
*/
String strRet = "";
Integer ITemp;
for (int j = 0; j < vecMode.size - 1; j++)
{
ITemp = (Integer) vecMode.elementAt(j);
strRet = strRet + ITemp.toString() + " ";
}
return (strRet);
}

I think that covers it all this time ...

keep in mind that I haven't tried to compile it, so of course, it's not tested ... But the logic seems good and I didn't catch any more typos ... it has been a while since I've done binary searches, but I think I got that part right ...

You should be able to copy-and-paste this to the appropriate places and it should work.

Mike does not take liability for any damages incurred while using this code ... if your computer blows up compiling it and you suffer horrible disfiguration ... it ain't my fault ...

Mike


9-18-98

Re: Re: arrays

1 more typo ... in the "if(indexPresent...)" clause of the "findMode" method ... it looks like I have an end parenthesis instead of a right brace

Mike


Home / Java Q & A Archive Page 1 / Java Q & A Archive / Java Q & A Main Page