我正在制作一个解决积分的程序。到目前为止,我设法制作了一个简单的函数积分(a,b)f(x)dx。 现在我需要使用MPI在并行编程中制作多线程程序。 这就是我的看法。需要为a,b,n创建一个元素数组(我希望我的函数解析得多么精确),然后为一个线程获取每个函数元素的组合。 问题:我是否正确理解了这个问题? 这是我的函数代码,我的史诗失败试图将其更改为数组。请帮忙!
#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include <stdio.h>
using namespace std;
void WaitForEnter(void)
{
printf("Press Enter to continue: ");
fflush(stdout);
while ( _getch() != '\n' ) ;
}
double fun (double x){return x;}//changing f(x)dx
double sumIntegral(double lowBound, int n, double dx)
{
double rectangleArea;
double cumSum=0;
for (int i=0; i<n; i++)
{
double xi = lowBound+i*dx;//x1,x2...
double funValue = fun(xi);
double rectangleArea = dx*funValue;
cumSum += rectangleArea;
}
return cumSum;
}
int main()
{
double lowBound = 4;//a
double upBound = 7 ;//b
int n = 5;//how precise is solution
double dx;
dx = (upBound-lowBound)/n;
double result = sumIntegral(lowBound , n, dx);
cout<<"rez je = "<<result;
WaitForEnter();
return 0;
}
//epic fail
#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include <stdio.h>
using namespace std;
void WaitForEnter(void)
{
printf("Press Enter to continue: ");
fflush(stdout);
while ( _getch() != '\n' ) ;
}
double fun (double x){return x;}//menja funkciju
double sumIntegral(double lowBound[], int n, double dx[],int numBound)
{
double rectangleArea;
double cumSum;
double sumIntegralAll[] = {NULL};
for(int j=0;j<numBound;j++)
{
cumSum=0;
for (int i=0; i<n; i++)
{
double xi = lowBound[j]+i*dx[j];//x1,x2...
double funValue = fun(xi);
rectangleArea = dx[j]*funValue;
cumSum+=rectangleArea;
cout<<cumSum;
}
//sumIntegralAll[j]=cumSum;
}
for(int i=0;i<numBound;i++){cout<<sumIntegralAll;}//pise
return cumSum;
}
int main()
{
int numBound = 2;//broj do
double lowBound[] = {4,4};//a
double upBound[] = {7,7} ;//b
int n = 5;//broj intervala, preciznost
double dx[]={NULL};
for(int i=0;i<=numBound;i++){
dx[i] = (upBound[i]-lowBound[i])/n;
}
double result = sumIntegral(lowBound , n, dx,numBound);
//cout<<"rez je = "<<result;
WaitForEnter();
return 0;
}