使用 STL 在 C++ 中進行陣列相乘


這是一個用 C++ 程式設計找出陣列乘積的示例。

演算法

Begin
   Initialize the values of array.
   Call used defined function accumulate to return the product of array.
   Print the solution.
End.

示例程式碼

 現場演示

#include <iostream>
#include <numeric>
using namespace std;
int ProductOfArray(int p[], int n) {
   return accumulate(p, p + n, 1, multiplies<int>());
}
int main() {
   int m[] = {6,7 };
   int n = sizeof(m) / sizeof(m[0]);
   cout <<"Product of the Array is:" <<ProductOfArray(m, n);
}

輸出

Product of the Array is:42

更新於:2019 年 7 月 30 日

185 次瀏覽

啟動你的 事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.