CS 110 Lab Assignment 11 NOTE: S points will be given for well-constructed comme
ID: 3701095 • Letter: C
Question
CS 110 Lab Assignment 11 NOTE: S points will be given for well-constructed comments in submitted codes. Problem 1 (10 points): Write a program printFrequency java that will take an integer array and number of elements that it stores as user input and will print frequency of each element in the given array Sample Input: Enter length of array: 8 Enter element 0: 2 Enter element 1: 5 Enter element 2: 6 Enter element 3: 5 Enter element 4: 2 Enter element 5:8 Enter element 6:8 Enter element 7: 5 Enter element 8: 5 Sample Output: Element 2 has frequency: 2 Element 5 has frequency: 4 Element 6 has frequency: 1 Element 8 has frequency: 2 Problem 2 (10 points): Write a program findMaxima.java that will take an integer array and number of elements that it stores as user input and will print all the Maximas. (NOTE: An element is a Maxima if it is greater than all the elements to its right side) Sample Input: Enter length of array: 7 Enter element 0: 10 Enter element 1: 9 Enter element 2: 14 Enter element 3:23 Enter element 4: 15 Enter element S: 0 Enter element 6: 9 Sample Output: Maximas in the given array are: 23, 15, 9Explanation / Answer
Problem 1:
//packages
import java.util.*;
import java.lang.*;
import java.io.*;
class GetFreq {
public static void main(String args[ ]) {
int t, i, j, n; //declaring variables
int count=0;
System.out.println("Enter length of array: ");
Scanner in = new Scanner(System.in); //Getting input from Keyboard with Scanner class
n = in.nextInt(); //storing input to n
n++; //since array index starts with index zero
int[ ] arr = new int[n]; //declaring an integer array
for(i=0;i<n;i++) { //loop for getting inputs
System.out.println("Enter element " + i + ": ");
t = in.nextInt();
arr[i] = t; // storing input in array
}
System.out.println(" ");
for(i=0;i<n;i++) { //loop for counting frequency of numbers
count=1;
for(j=i+1;j<n-1;j++) { //loop for checking same elements
if(arr[i]==arr[j] && arr[i]!='') {
count++;
arr[j] = '';
}
}
if(arr[i]!='') { //printing frequencies
System.out.println("Element " + arr[i] + " has frequency: " + count + " ");
}
}
}
}
thank you!!
please do upvote :)