fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. long n, a, b;
  6. cout << "Cit de mare va fi vectorul? ";
  7. cin >> n;
  8.  
  9. long double vector[n];
  10.  
  11. cout << "Introdu numerele:\n";
  12. for (int i = 0; i < n; i++) {
  13. cin >> vector[i];
  14. }
  15.  
  16. cout << "De unde pina unde vrei sa afli media? (indexuri a si b): ";
  17. cin >> a >> b;
  18. if (a < 0 || b >= n || a > b) {
  19. cout << "Interval invalid!";
  20. return 0;
  21. }
  22.  
  23.  
  24. long double suma = 0;
  25. long contor = 0;
  26.  
  27. for (int i = a; i <= b; i++) {
  28. suma += vector[i];
  29. contor++;
  30. }
  31.  
  32. cout << "Media numerelor este: " << (suma / contor);
  33.  
  34. return 0;
  35. }
  36.  
  37.  
Success #stdin #stdout 0.01s 5316KB
stdin
45
stdout
Cit de mare va fi vectorul? Introdu numerele:
De unde pina unde vrei sa afli media? (indexuri a si b): Interval invalid!