Advertisement
harrisonmk

MediaRecursivaComVetor

May 16th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. double media (int *vetor, int n, int tam);
  6.  
  7. int main(int argc, char** argv)
  8. {
  9.     int tamanho;
  10.  
  11.     cout << "\nDigite o tamanho do vetor: ";
  12.     cin >> tamanho;
  13.     int vetor[tamanho];
  14.  
  15.     cout << endl;
  16.  
  17.     for (int i = 0; i < tamanho; i++)
  18.     {
  19.         cin >> vetor[i];
  20.     }
  21.  
  22.     cout << "\nMedia: " << media(vetor, tamanho, tamanho) << endl;;
  23.  
  24.  
  25.     return 0;
  26. }
  27.  
  28. double media (int *vetor, int n, int tam)
  29. {
  30.  
  31.     if (n <= 0)
  32.     {
  33.         return 0;
  34.     }
  35.     else
  36.     {
  37.         return  vetor[n - 1] / (float)tam + media(vetor, n - 1, tam);
  38.     }
  39.  
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement