blooming8

Vettore di pari + max

Jun 10th, 2020 (edited)
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. /* Caricato un vettore di numeri pari ricercarne il valore massimo */
  2.  
  3. #include <stdio.h>
  4.  
  5. int Cerca_max(int *vett, int dim);
  6.  
  7. int main()
  8. {
  9.     int dim = 4;
  10.    
  11.     int vett[dim], num;
  12.    
  13.     // caricamento vettore
  14.    
  15.     for (int i = 0; i < dim; i++)
  16.     {
  17.         printf("Elemento pari (%d) : ", (i + 1));
  18.         scanf("%d", &num);
  19.  
  20.         while ((num % 2) != 0)
  21.         {
  22.             puts("Devi inserire un elemento PARI!\n");
  23.             printf("Elemento pari (%d) : ", (i + 1));
  24.             scanf("%d", &num);
  25.         }
  26.        
  27.         vett[i] = num;
  28.     }
  29.    
  30.    
  31.     puts("Vettore: \n");
  32.     for (int i = 0; i < dim; i++)
  33.         printf(" %d ", vett[i]);
  34.    
  35.     printf("\nValore massimo: %d", Cerca_max(vett, dim));
  36.    
  37.     return 0;
  38. }
  39.    
  40. // ricerca del massimo
  41.  
  42. int Cerca_max(int *vett, int dim)
  43. {
  44.     int max;
  45.    
  46.     for (int i = 0; i < dim; i++)
  47.         if (i == 0)
  48.             max = vett[i];
  49.         else
  50.             if (max < vett[i])
  51.                 max = vett[i];
  52.    
  53.     return max;
  54. }
Add Comment
Please, Sign In to add comment