Advertisement
informaticage

Vectors come parametri a funzioni

Feb 5th, 2021
1,942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6. int somma(vector<int> V) {
  7.     int s = 0;
  8.     for(int i = 0; i < V.size(); i++) {
  9.         s += V[i];
  10.     }
  11.     return s;
  12. }
  13.  
  14. int main ( ) {
  15.     int N;
  16.     cout << "N: ";
  17.     cin >> N;
  18.  
  19.     vector<int> V(N, 9);
  20.  
  21.     cout << "V size: " << V.size() << endl;
  22.  
  23.     for(int i = 0; i < V.size(); i++) {
  24.         cout << V[i] << " ";
  25.     }
  26.  
  27.     cout << "E' la somma che fa il totale: " << somma(V) << endl;
  28.     return 0;
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement