1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
//Header for use of pow();
#include <cmath>
//Headers: for an example with rand():
//Including srand() and time();
#include <cstdlib>
#include <ctime>
//For trailing decimal places of 2, Precision();
#include <iomanip>
using namespace std;
double F(double *x, double *g, int j)
{
double result=1;//Multiplication by zero would result with 0.
for(int i=0;i<j;i++)
{
result*=pow(x[i],g[i]);
}
return result;
}
int main()
{
srand(time(0));
//Declaring an integer j, which counts array size.
int j=rand()%10+1;
//Declaring arrays of already known size j; j must be known.
double x[j], g[j];
//Initializing dummy data:
for(int i=0;i<j;i++)
{
x[i] =rand()%10+1;
g[i] =rand()%10+1;
cout<<"(x"<<i<<",g"<<i<<"): "<<"("<<x[i]<<","<<g[i]<<")"<<endl;
}
//Running function and displaying the result:
double result = F(x,g,j);
cout<<"The resulting value from F function: "<<result<<endl;
cout<<"OR: "<<fixed<<setprecision(2)<<result<<endl;
//Supporting command line operation, without IDE:
cout<<"Custom Exit: Press 'enter': ";
cin.get();
return 0;
}
|
(x0,g0): (1,8)
(x1,g1): (8,4)
(x2,g2): (3,1)
(x3,g3): (6,1)
(x4,g4): (1,3)
(x5,g5): (9,6)
The resulting value from F function: 3.91821e+010
OR: 39182082048.00
Custom Exit: Press 'enter': |