complete count and split methods We need to know the number of elements in an array with
Question:
complete count and split methods
We need to know the number of elements in an array with a value less than a given given value. To do this, we will create the method count, with two parameters, the reference of an array of values tab, and the given value val : public static int count(int[] tab, int val); This method returns an integer (int) which represents the number of elements of the array designated by tab whose value is strictly lower than a given value val. We will apply this method afterwards to the current parameters tab2 given above and its average value. We will thus have the number of elements that must be saved in the array designated by the reference variable smallTab, so we can create the array dynamically. Finally, we will have to create the split method which will have three parameters: public static void split(int[] tab, int[] petitTab, int val); The first two parameters are references to arrays of type int, the last parameter is of type int. parameter is of type int. The goal of the method is to save in the array designated by smallTab all the values of the array designated by tab whose value is lower than its mean value (will be copied in the (will be copied in the val parameter). Modify the attached Tab program to include the count and split methods described above in the described above in the places indicated. The main method main is provided here for testing and should not be test and should not be modified. It allows you to calculate the average, count the number elements below the average, create the necessary array smallTab, and call the method split method to fill smallTab, and finally print the values of these two arrays. Example output: The sorted elements of tab1 and tab2 are: -10 , 1 , 3 , 4 , 7 , 9 , 55 , 88 7 , 10 , 34 , 56 , 62 , 82 , 95 The average value of tab2 is 49 The elements of smallTab are: 7, 10, 34
Code :
class Tab {
public static void trier( int [ ] tab ){
int i , j , min , tmp ;
for ( i = 0 ; i < tab.length - 1 ; i++) {
min = i;
for ( j = i + 1 ; j < tab.length ; j++) {
if ( tab[j]
min = j ;
}
}
tmp = tab[min] ;
tab[min] = tab[ i ] ;
tab [ i ] = tmp ;
}
}
public static int moyenne(int[] tab){
int somme, moyenne;
int i;
somme = 0;
for (i = 0; i < tab.length; i++) {
somme += tab[i];
}
moyenne = somme/tab.length;
return moyenne;
}
public static int count(int[] tab, int val){
}
public static void split(int[] tab,int[] petit, int val){
}
public static void main (String args [ ] )
{
int tab1 [ ] = { 3,55,7, 1, 88, 9 , 4, -10 };
int i,moyenne, size;
int[] petitTab,tab2;
tab2 = new int[]{10,34,62,56,82,7,95};
trier(tab1 ) ;
trier(tab2 ) ;
moyenne=moyenne(tab2);
size = count(tab2, moyenne);
petitTab = new int[size];
System.out.println (" " ) ;
for ( i =0; i
if ( i >0) {
System.out.print ( " , " ) ;
}
System.out.print (tab1 [ i ] ) ;
}
System.out.println ( ) ;
for ( i =0; i
if ( i >0) {
System.out.print ( " , " ) ;
}
System.out.print (tab2 [ i ] ) ;
}
System.out.println ( ) ;
System.out.print ("" + moyenne(tab2)) ;
System.out.println ( ) ;
moyenne=moyenne(tab2);
split(tab2,petitTab,moyenne);
System.out.print(" ");
for(i=0; i
if (i>0) {
System.out.print(", ");
}
System.out.print(petitTab[i]);
}
System.out.println ( ) ;
}
}
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest