Computer Applications
Suppose x is an array of type int[] with 50 elements. Write a code segment that will count and print the frequency of number 42 in the array.
Java Arrays
8 Likes
Answer
int c = 0;
for (int i = 0; i < 50; i++) {
if (x[i] == 42) {
c++;
}
}
System.out.println("Frequency of 42 = " + c);
Answered By
6 Likes
Related Questions
A student wrote the following code segment, intending to print 11 22 33 44:
int arr[] = {11, 22, 33, 44}; for (int i = 1; i <= 4; i++) System.out.println(arr[i]);
However, the program crashed with a run-time error. Can you explain the reason for this?
Declare and instantiate a one dimensional int array named evenNums with five elements. Use an initialiser list that contains the first five even integers, starting with 11.
Write a program to input integer elements into an array of size 20 and perform the following operations:
- Display largest number from the array
- Display smallest number from the array
- Display sum of all the elements of the array
Write a code segment to compute the sum of all positive real numbers stored in the following array.
double numb[] = new double[50];