SHARE
TWEET

Tarea Vectores 1

a guest Oct 16th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. void imprimir(vector<int>&v)
  8. {
  9.     cout << "Vector: " << "\n";
  10.     for(int & i : v)
  11.         cout << i << setw(4);
  12.  
  13.     cout << "\n";
  14. }
  15.  
  16. void promedio(vector<int>&v)
  17. {
  18.     int sum=0, cont=0;
  19.  
  20.     for(int i : v)
  21.     {
  22.         sum = sum + i;
  23.         cont++;
  24.     }
  25.  
  26.     cout << "Promedio de todos los numeros del vector: " << "\n";
  27.     cout << sum / cont;
  28.  
  29.     cout << "\n";
  30. }
  31.  
  32. void suma(vector<int>&v)
  33. {
  34.     int tope=0, sum=0;
  35.  
  36.     cout << "Ingresar el tope de la suma: " << "\n";
  37.     cin >> tope;
  38.  
  39.     for(int & i : v)
  40.     {
  41.          if(i < tope)
  42.              sum = sum + i;
  43.     }
  44.  
  45.     cout << sum;
  46.  
  47.     cout << "\n";
  48. }
  49.  
  50. void diferencia(vector<int>&v, int n)
  51. {
  52.     int sum=0, cont=0;
  53.  
  54.     for(int i : v)
  55.     {
  56.         sum = sum + i;
  57.         cont++;
  58.     }
  59.  
  60.     vector<int> v2(n);
  61.  
  62.     for(int i=0; i<n; i++)
  63.     {
  64.         v2[i] = (sum/cont) - v[i];
  65.     }
  66.  
  67.     cout << "Diferencia de numero y promedio: " << "\n";
  68.     for(int & i : v2)
  69.         cout << i << setw(4);
  70.  
  71. }
  72.  
  73. int main()
  74. {
  75.     int n;
  76.     cout << "Introducir tamano del vector: " << "\n";
  77.     cin >> n;
  78.  
  79.     srand(time(nullptr));
  80.  
  81.     vector<int> v(n);
  82.  
  83.     for(int & i : v)
  84.         i = rand()%100;
  85.  
  86.     imprimir(v);
  87.     promedio(v);
  88.     suma(v);
  89.     diferencia(v, n);
  90.  
  91.     return 0;
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top