I was told I\'m missing an array that stores the frequency of the numbers and ch
ID: 3762396 • Letter: I
Question
I was told I'm missing an array that stores the frequency of the numbers and characters from my instructions below? Am I over looking anything in my code? I think it's okay, need a 2nd opinion.
( Given an array of 200 numbers in the range 1-100, you will count the frequency of occurrence of each number and store these tallies in an array frequency. The array frequency will be of size 100 + 1, since index [0] will not be used if the numbers range from 1-100. A similar argument can be made for characters, since characters are integers internally (ASCII) and thus the usable range would then be [65]-[90].)
import java.util.*;
public class Main {
public static void main(String[] args) {
//Declares the required variables
int[] intList=new int[200] ;
char[] charList=new char[200];
//Initiates random function
Random r = new Random();
// Loop to iterate the code 200 times
for(int i=0; i<200; i++)
{
// Code to generate random value between 1 to 100;
intList[i] = r.nextInt(100)+1;
}
System.out.println(" Original Integer array:");
// loop to iterate the code 200 times
for(int i=0; i<200; i++)
{
// Condition to print the value in new line after each 20 values
if(i%20 == 0)
System.out.println();
// Print the array values
System.out.print(intList[i] + " ");
}
System.out.println(" Sorted Integer array:");
// Generic method to sort the integer array
Arrays.sort(intList);
// loop to iterate the code 200 times
for(int i=0; i<200; i++)
{
// Condition to print the value in new line after each 20 values
if(i%20 == 0)
System.out.println();
// Print the array values
System.out.print(intList[i] + " ");
}
// loop to iterate the code 200 times
for(int c=0; c<200; c++)
{
charList[c] = (char)(r.nextInt(26) + 'A');
}
System.out.println(" Original character array:");
// loop to iterate the code 200 times
for(int c=0; c<200; c++)
{
// Condition to print the value in new line after each 20 values
if(c%20 == 0)
System.out.println();
// Print the array values
System.out.print(charList[c] + " ");
}
System.out.println(" Sorted character array:");
// Generic method to sort the character array
Arrays.sort(charList);
// loop to iterate the code 200 times
for(int c=0; c<200; c++)
{
// Condition to print the value in new line after each 20 values
if(c%20==0)
System.out.println();
// Print the array values
System.out.print(charList[c] + " ");
}
}
}
Explanation / Answer
package Labs;
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] frequencyNumbers = new int[101];
int[] frequencyCharacters = new int[101];
for (int i = 0; i < 101; i++) {
frequencyNumbers[i]=frequencyCharacters[i]=0;
}
// Declares the required variables
int[] intList = new int[200];
char[] charList = new char[200];
// Initiates random function
Random r = new Random();
// Loop to iterate the code 200 times
for (int i = 0; i < 200; i++) {
// Code to generate random value between 1 to 100;
intList[i] = r.nextInt(100) + 1;
}
System.out.println(" Original Integer array:");
// loop to iterate the code 200 times
for (int i = 0; i < 200; i++) {
// Condition to print the value in new line after each 20 values
if (i % 20 == 0)
System.out.println();
// Print the array values
System.out.print(intList[i] + " ");
frequencyNumbers[intList[i]]++;
}
System.out.println(" Sorted Integer array:");
// Generic method to sort the integer array
Arrays.sort(intList);
// loop to iterate the code 200 times
for (int i = 0; i < 200; i++) {
// Condition to print the value in new line after each 20 values
if (i % 20 == 0)
System.out.println();
// Print the array values
System.out.print(intList[i] + " ");
}
// loop to iterate the code 200 times
for (int c = 0; c < 200; c++) {
charList[c] = (char) (r.nextInt(26) + 'A');
}
System.out.println(" Original character array:");
// loop to iterate the code 200 times
for (int c = 0; c < 200; c++) {
// Condition to print the value in new line after each 20 values
if (c % 20 == 0)
System.out.println();
// Print the array values
System.out.print(charList[c] + " ");
frequencyCharacters[charList[c]]++;
}
System.out.println(" Sorted character array:");
// Generic method to sort the character array
Arrays.sort(charList);
// loop to iterate the code 200 times
for (int c = 0; c < 200; c++) {
// Condition to print the value in new line after each 20 values
if (c % 20 == 0)
System.out.println();
// Print the array values
System.out.print(charList[c] + " ");
}
System.out.println("Frequenct list of numbers: ");
for (int i = 1; i <= 100; i++) {
System.out.println(i+":"+frequencyNumbers[i]);
}
System.out.println("Frequenct list of characters: ");
for (int i = 65; i <= 90; i++) {
System.out.println((char)i+":"+frequencyCharacters[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.