Advertisement
Guest User

Lezione 5 - Funzione media

a guest
Oct 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void media(int v[], int dim);
  4.  
  5. int main()
  6. {
  7.     int dim=10;
  8.     int vect[dim];
  9.    
  10.     for(int i=0; i<dim; i++){
  11.         scanf(" %d", &vect[i]);
  12.     }
  13.     media(vect, dim);
  14.    
  15.     return 0;
  16. }
  17.  
  18.  
  19. void media(int v[], int dim){
  20.     int positivo=1;
  21.     int quantita=0;
  22.     float somma=0;
  23.    
  24.    
  25.     if(v[dim-1]<0){
  26.         positivo=0;
  27.     };
  28.    
  29.     for(int i=0; i<dim; i++){
  30.         if(v[i]!=0){
  31.             if(positivo){
  32.                 if(v[i]>0){
  33.                     somma=somma+v[i];
  34.                     quantita++;
  35.                 }
  36.             }else{
  37.                 if(v[i]<0){
  38.                     somma=somma+v[i];
  39.                     quantita++;
  40.                 }
  41.             }
  42.         }
  43.     }
  44.    
  45.     printf("%.2f",(float)(somma/quantita));
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement