Write a method called findNums that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is
10 45 3 8
2 42
3 21 44
And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array)
public class Question2 {
public static void main(String args[]){
int arr[][] = {{10, 45, 3, 8}, {2, 42}, {3, 21, 44}};
System.out.println(“The number time 3 appears is “+findNums(arr,3));
} //main
public class Question2 {
public static int findNums(int[][] arr,int n)
{
int count=0;
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[i].length;j++)
{
if(arr[i][j]==n)
count++;
}
}
return count;
}
public static void main(String args[]){
int arr[][] = {{10, 45, 3, 8}, {2, 42}, {3, 21, 44}};
System.out.println("The number time 3 appears is "+findNums(arr,3));
} //main
}
Get Answers For Free
Most questions answered within 1 hours.