Jin N.
asked 01/21/21Help me with this please
Write a static void method that will take accept an int array as a parameter and will count the number of times the numbers, 0-9, appear in the array, which can just be print statements
3 Answers By Expert Tutors
Dayaan M. answered 29d
Computer Science Graduate with Computer Engineering Coursework
In order to count how many times each of the digits 0 through 9 shows up in an array, the trick that makes this easy is to use a second array of size 10 as a set of counters, where the digit itself is the index. So counts[0] holds how many zeros we saw, counts[7] holds how many sevens we saw, and so on. You could write ten separate if statements instead, but then you would be writing almost the same line ten times, and a counting array handles all ten of them at once.
Here is the method:
public static void countDigits(int[] numbers) {
int[] counts = new int[10];
for (int i = 0; i < numbers.length; i++) {
int n = numbers[i];
if (n >= 0 && n <= 9) {
counts[n]++;
}
}
for (int digit = 0; digit <= 9; digit++) {
System.out.println(digit + " appears " + counts[digit] + " times");
}
}
The first thing worth noticing is that int[] counts = new int[10] already gives you ten slots that all start at 0, since Java fills a new int array with zeros for you, so there is no extra loop needed to clear it out first.
Then the first loop walks through the array one element at a time, and the line counts[n]++ is where the actual work happens. If n is 7, it adds one to counts[7], and if n is 3, it adds one to counts[3], so each number bumps up its own counter. The if check around it is there because the question only asks about 0 through 9, and if the array happened to hold something like 42, then counts[42] would throw an ArrayIndexOutOfBoundsException and crash the program, so anything outside that range gets skipped.
The second loop is just the printing, which the question says is fine. It runs from 0 to 9 and prints each digit next to its count, so every digit shows up in the output even the ones that appeared zero times.
I ran it on this array to make sure it works:
int[] test = {3, 7, 3, 0, 9, 3, 7, 5, 5, 5, 5};
countDigits(test);
and it printed that 0 appears 1 times, 3 appears 3 times, 5 appears 4 times, 7 appears 2 times, 9 appears 1 times, and 0 times for the rest, which matches the array when you count by hand.
One last thing, since the method is static and void, you call it straight from main like above without making an object first and without storing a return value, because it prints instead of returning anything.
Inactive Tutor answered 01/21/21
class DigitStats
{
private int A[];
private long freqTable[];
DigitStats( int a[])
{
//deep copies
int N = a.length;
A = new int[N];
for (int iLoop=0; iLoop<N; iLoop++)
{
A[iLoop]=a[iLoop];
}
//clears the frequencies
freqTable = new long[10];
for (int iLoop=0; iLoop<10; iLoop++)
{
freqTable[iLoop]=0;
}
}
void Set( int a[])
{
//deep copies
int N = a.length;
A = new int[N];
for (int iLoop=0; iLoop<N; iLoop++)
{
A[iLoop]=a[iLoop];
}
//clears the frequencies
freqTable = new long[10];
for (int iLoop=0; iLoop<10; iLoop++)
{
freqTable[iLoop]=0;
}
}
protected void CalculateFrequencies()
{
for (int iLoop=0; iLoop<A.length; iLoop++)
{
String iNumbuff = (new Integer(A[iLoop])).toString();
System.out.println(iNumbuff);
int nStrLen = iNumbuff.length();
for (int jLoop=0; jLoop<nStrLen; jLoop++)
{
char chDigit = iNumbuff.charAt(jLoop);
if ((chDigit>='0') && (chDigit<='9'))
{
freqTable[chDigit-48]++;
}
}
}
}
public long[] GetFreq()
{
long longFreqTableReturn[] = new long[10];
CalculateFrequencies();
for (int iLoop=0; iLoop<10; iLoop++)
{
longFreqTableReturn[iLoop] = freqTable[iLoop];
}
return(longFreqTableReturn);
}
public static void main(String args[])
{
int iNums[] = { 1234,2345,3456,4567,5678,6789,7890,8901,9012};
DigitStats digitStats = new DigitStats(iNums);
long freqTable[] = digitStats.GetFreq();
System.out.println("----------------------------------------");
System.out.println(" DIGIT FREQ ");
System.out.println("-----------------------------------------");
for (int iLoop=0; iLoop<10; iLoop++)
{
System.out.println(" " + iLoop + " " + freqTable[iLoop]);
}
}
public void Clear()
{
A=null;
freqTable=null;
}
}
Inactive Tutor answered 01/21/21
In this static void method, you can create a new array of ints with a length of 10. Let's call that array counter. Each index of the array will represent the number of times each number, 0-9, has been found.
Use a for loop to iterate through each index of the parameter array, and increment the appropriate index of counter. For instance, if you encounter a 5 in the parameter array, increase counter[5] by 1. If you encounter a 1, increase counter[1] by 1.
Once you've gone through the entire parameter array in this fashion, you'll have essentially counted the number of occurrences of each integer; the number of occurrences will be held in counter.
Lastly, print out the value of each index of counter!
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Inactive Tutor
There is confusion on what is expected... the array contains integers which can be 2,3,4 digits long... you are asking to count how many times each digit appears, a frequency table For each integer in the array: converts the integer to string for each character in the string increment the frequency for that digit ----------------------------------------------------------------------------- This will of course work on even single digit integers, so no harm, no foul.... The source code is uploaded to the RESOURCES section under this link.01/21/21