কম্পিউটার

কিভাবে একটি C++ ভেক্টর উপাদান যোগফল?


একটি C++ ভেক্টরের সমস্ত উপাদানের যোগফল খুব সহজে std::acmulate পদ্ধতিতে করা যায়। এটি <সাংখ্যিক> হেডারে সংজ্ঞায়িত করা হয়েছে। এটি ভেক্টরে উল্লিখিত সমস্ত মানকে নির্দিষ্ট সমষ্টিতে জমা করে।

অ্যালগরিদম

Begin
   Declare v of vector type.
      Initialize some values into v vector in array pattern.
      Print “Sum of all the elements are:”.
      Call accumulate(v.begin(),v.end(),0) to calculate the sum of all
      values of v vector.
      Print the result of sum.
End.

উদাহরণ কোড

#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
int main() {
   vector<int> v = {2,7,6,10};
   cout<<"Sum of all the elements are:"<<endl;
   cout<<accumulate(v.begin(),v.end(),0);
}

আউটপুট

Sum of all the elements are:
25

  1. কিভাবে C++ এ একটি ভেক্টরে একটি ভেক্টর যুক্ত করবেন?

  2. কিভাবে একটি ভেক্টর C++ এ কাজ করে?

  3. কিভাবে C++ এ কম্বিনেশন এবং পারমুটেশন গণনা করা যায়?

  4. কিভাবে C++ এ একটি ভেক্টরের বিষয়বস্তু মুদ্রণ করবেন?