tuxmartin

Vypis cisel vetsich nez prumer v poli

Oct 24th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. /*
  2. * Implementujte telo metody *nadprumer*, ktera vrati pocet prvku pole *pole*,
  3. * ktere jsou vetsi, nez aritmeticky prumer vsech prvku v poli.
  4. *    int nadprumer(int[] pole) {
  5. */
  6.  
  7. import java.util.Random;
  8.  
  9. public class PrumerCiselVPoli {
  10.    
  11.     private static int[] poleCisel = new int[10];
  12.    
  13.     public static void main(String[] args) {
  14.         Random rnd = new Random();
  15.        
  16.         for (int i = 0; i < 10; i++) {
  17.             poleCisel[i] = rnd.nextInt();      
  18.             System.out.println("Cislo c. " + i + " " + poleCisel[i]);
  19.         }
  20.        
  21.         System.out.println( nadprumer(poleCisel) + " je vetsich, nez prumer." );       
  22.     }
  23.    
  24.     private static int nadprumer(int[] pole) {
  25.         int soucet = 0;
  26.         float prumer = 0;
  27.        
  28.         for (int i = 0; i < pole.length; i++) {
  29.             soucet += pole[i];
  30.         }
  31.        
  32.         prumer = soucet / pole.length;
  33.         System.out.println("Soucet = " + soucet);
  34.         System.out.println("Prumer = " + prumer);
  35.        
  36.         int pocetVetsichNezPrumer = 0;
  37.         for (int i = 0; i < pole.length; i++) {
  38.             if (pole[i] > prumer) {
  39.                 pocetVetsichNezPrumer++;               
  40.             }                      
  41.         }
  42.        
  43.         return pocetVetsichNezPrumer;  
  44.     }  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment