ahmed0saber

Sum and Average of unspecified number of values in C++

Nov 11th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std ;
  4. int main()
  5. {
  6.     int n,i,n_p,n_n,total=0;
  7.     float av;
  8.     vector<int>n1;
  9.     cout<<"Enter integers , The input ends if it is 0 : ";
  10.     cin>>n;
  11.     if (n==0)
  12.     {
  13.         cout<<"You haven't entered any number just 0";
  14.     }
  15.     else
  16.     {
  17.         for(i=0;n!=0;i++)
  18.         {
  19.             n1.push_back(n);
  20.             cin>>n;
  21.         }
  22.         for (auto i = n1.begin(); i != n1.end(); ++i)
  23.         {
  24.             if (*i>0)
  25.             {
  26.                 n_p++;
  27.             }
  28.             else if (*i<0)
  29.             {
  30.                 n_n++;
  31.             }
  32.             total=total+*i;
  33.         }
  34.         av=(float)total/i;
  35.         cout<<"Number of positive integers : "<<n_p<<endl<<"Number of negative integers : "<<n_n<<endl<<"Total : "<<total<<endl<<"Average : "<<av;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment