In class Recursion problems bool anyNegative(int al , int size) This function re
ID: 3747187 • Letter: I
Question
In class Recursion problems bool anyNegative(int al , int size) This function returns true if there are any negative numbers in the array, grity false otherwise. int firstNegative(int al I, int size) This function returns the position of the first negative number in the array & EAO If there are no negative numbers it returns int countNegative(int al I, int size) This function returns the number of negative numbers in the array a. 12 bst Stringsint indexO bout Strings nt indexOfMin(int al l. int size) This function returns the index of minimum value in the array M Chapter IszsExplanation / Answer
Hi Student,
I had written code in Java if you wanted in other language then use the implementation of method provided in code
public class Chapter_8_3026Chegg {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Test implemented methods
int[] arr={3,6,8,9,-1,6,3,-12};
System.out.println("Array Contains negative number ? "+anyNegative(arr,arr.length));
System.out.println("Array Contains first negative number at index :"+firstNegative(arr,arr.length));
System.out.println("Array negative number Count :"+countNegative(arr,arr.length));
System.out.println("Array Contains the minimal number at index :"+indexOfMin(arr,arr.length));
}
//Method to check if array contains any negative number
public static boolean anyNegative(int a[],int size){
for(int a1:a){
//If any negative number found method returns true
if(a1<0)
return true;
}
//Otherwise returns false
return false;
}
//Method to find index of first negative number
public static int firstNegative(int a[],int size){
int index=0;
for(int a1:a){
//If any negative number found method returns index of that number
if(a1<0)
return index;
index++;
}
//Otherwise returns -1
return -1;
}
//Method to find total countof negative number
public static int countNegative(int a[],int size){
int count=0;
for(int a1:a){
//If any negative number found increment the count
if(a1<0)
count++;
}
//Return total count of negative number
return count;
}
public static int indexOfMin(int a[],int size){
int min=0;
for(int i=1;i<size;i++){
//Find the index of minimum number
if(a[min]>a[i])
min=i;
}
return min;
}
}
Output:
Array Contains negative number ? true
Array Contains first negative number at index :4
Array negative number Count :2
Array Contains the minimal number at index :7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.