Friday, 31 July 2015

Given an array of integers, find the product of its elements Simple Algorithm Solution.

/*
Given an array of integers, find the product of its elements.
Example
for [1, 3, 2, 10] output should be 60
[input] array.integer inputArray
non-empty array of integers
[output] integer
product of all elements of inputArray

Tag: Algorithm , C++, Code Fight , Array.
*/

Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int arrayProduct(vector<int> inputArray) {

   int result = 1;
   for (int i = 0; i < inputArray.size(); i++)result*= inputArray[i];
   return result;
}
int main() {

   int n,a;
   vector<int>v;
   cin >> n;
   for(int i = 0; i < n; i++){
      cin >> a;
      v.push_back(a);
   }
   cout << arrayProduct(v) << endl;
   return 0;
}

No comments:

Post a Comment