222k views
0 votes
Given the integer array dailymiles with the size of arr vals, assign nummatches with the number of integers in uservalues that are equal to 2. ex: if the input is 57 2 95 2 2 42 98 2 2 60, then the output is: number of 2s in array: 5

import java.util.Scanner;
public class ArrayComparison {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int ARR_VALS = 6;
int[] averageSalaries = new int[ARR_VALS];
int i;
int numMatches;

for (i = 0; i < averageSalaries.length; ++i) {
averageSalaries[i] = scnr.nextInt();
}
// here the code
System.out.println("Number of 0s in array: " + numMatches);
}
}

1 Answer

3 votes

The occurrences of the integer 2 in the array `averageSalaries`, initialize `numMatches` to 0. Increment `numMatches` when the array element is 2. Output the result after the loop.

To find the number of integers equal to 2 in the array `averageSalaries`, you can use a loop to iterate through the array and check each element. Initialize `numMatches` to 0 before the loop, and increment it whenever you find an element equal to 2. Here's the modified code:

java

import java.util.Scanner;

public class ArrayComparison {

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

final int ARR_VALS = 6;

int[] averageSalaries = new int[ARR_VALS];

int i;

int numMatches = 0; // Initialize numMatches

for (i = 0; i < averageSalaries.length; ++i) {

averageSalaries[i] = scnr.nextInt();

// Check if the current element is equal to 2

if (averageSalaries[i] == 2) {

numMatches++; // Increment numMatches

}

}

System.out.println("Number of 2s in array: " + numMatches);

}

}

This code will count the occurrences of the integer 2 in the array `averageSalaries` and output the result after the loop completes.

User Andrew Boes
by
8.2k points