c++ - Multithreading (integral function) - c++

I am making a program that solves integrals. So far I managed to make a simple function integral (a,b) f(x)dx.
Now I need to make a Multithreading program in parallel programming using MPI.
This is how i see it. Need to make an array of elements for a,b,n(how precise i want my function solved) and then taking each combination of function elements for one thread.
Question : Am I understanding this problem correctly?
This is my code for functions and my epic fail trying to change it to array. Please help!
#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;
}

Related

why is the answer not showing up?

I am doing a code in c++ where I am supposed to be finding the series and I build the function for the series myself yet and I call the function I don't find my answer
here is my code
#include <iostream>
#include <cmath>
using namespace std;
double harmonicSeries(int n);
int main() {
int n;
cout << "Enter n" << endl;
cin >> n;
harmonicSeries(n);
}
double harmonicSeries(int n) {
for (int i = 1; i <= n; i++) {
float s;
float sum = 0.0;
s = 1 / n;
sum += s;
return sum;
}
}
I will be thankful for any help
See I have made the changes in your code,this works fine in this finding numbers and adding to get their sum.You should use return outside the function and basically harmonic series is of form 1/n which can be any float number or double number so I use s as double and i has float(which by this).
s=1/i(double=1/float,gets converted to double)
#include <iostream>
#include <cmath>
using namespace std;
double harmonicSeries(int n);
int main() {
int n;
cout << "Enter n" << endl;
cin >> n;
cout<<harmonicSeries(n);
}
double harmonicSeries(int n) {
double sum=0.00;
double s;
for (float i = 1; i <= n; i++) {
s = 1 / i;
sum += s;
}
return sum;
}
If you find anything wrong do ask for sure:)

Using boost multi precision operator instead of a const double one

I have the following code for integration by boost library. Iam tried to change the double operator to cpp_dec_float_50 operator.
#include <iostream>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature = boost::numeric::quadrature;
double polynomial(int k , int n);
struct f
{
vector<double> poly;
double polysum(double x) const {
double s = 0;
double p = 1;
for (int i = 1; i < poly.size(); i++) {
p = p * x;
s += p * poly[i];
}
return s;
}
double operator()(double x)const {
return polysum(x) * log(x) / (1 + x);
}
};
int main()
{
int n = 2;
f fun;
double p = 0;
for (int i = 0; i <= n; i++)
{
p = polynomial(i, n);
fun.poly.push_back(p);
}
double answer, error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(fun, 0., 1., answer, error_estimate);
cout << "ans" << answer << endl;
return 0;
}
double polynomial(int k , int n)
{
return k;
}
if it changed to :
cpp_dec_float_50 operator()(cpp_dec_float_50 x) const {
and change all the related thing to cpp_dec_float_50, then a list of errors appear see them here
can any one fix that ?
for users who does not have Boost Quadrature library, u can download it from here https://github.com/coolfluid/coolfluid3/tree/master/include/boost/numeric

C++, function doesn't return double [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 7 years ago.
my code doesn't return double value z, instead returns only 1, why?
#include <iostream>
#include <fstream>
using namespace std;
double road(int s, int v, int max )
{
double t;
t = (s/v);
return t;
}
int main()
{
int s[2]={0};
int v[2]={0};
int max;
double z; // result of function
ifstream fd;
fd.open("u1.txt");
fd >> max;
for (int i = 0; i < 2; i++)
{
fd >> s[i] >> v[i];
z = road( s[i], v[i], max );
cout << z << " ";
}
fd.close();
return 0;
}
Try to change your method like
double road(int s, int v, int max )
{
double t;
t = (s/(double)v);
return t;
}
int/int will result in a integer. So you need to cast the numerator or denominator as double.

Getting error message "Not declared in scope" [duplicate]

This question already has answers here:
Why is my HelloWorld function not declared in this scope?
(11 answers)
Closed 8 years ago.
I am working on a C++ project and while compiling, I am receiving the error messages:
error: mean was not declared in this scope
error: standard_dev was not declared in this scope
My code is
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
int N(0);
char filename[100];
double m, stdev;
string temp;
double next;
int count = 0;
cout << "Enter name of file: ";
cin >> filename;
ifstream myfile;
myfile.open(filename);
while (myfile >> next)
{
count++;
}
N = count;
double* mydata;
mydata = new double[N];
for (int i = 0; i < N; i++)
{
myfile >> mydata[i];
}
m = mean(mydata, N);
stdev = standard_dev(mydata, m, N);
cout << "The standard deviation is:" << stdev << endl;
myfile.close();
delete[] mydata;
return 0;
}
double mean(double* mydata, double N)
{
double sum(0), m;
for (int i = 0; i < N; i++)
{
sum += mydata[i];
}
m = (sum / (double) N);
return (m);
}
double standard_dev(double* mydata, double m, int N)
{
double* mydata2 = new double[N];
for (int i = 0; i < N; i++)
{
mydata2[i] = pow((mydata[i] - m), 2);
}
double sum(0), S, X;
for (int i = 0; i < N; i++)
{
sum += mydata2[i];
}
X = sum / N;
S = sqrt(X);
return (S);
}
Those functions have not been seen yet when you try to use them; the compiler doesn't know what they are which leads to the error. Either move them before main() or prototype them, such as:
double mean(double * mydata, double N);
double standard_dev(double * mydata, double m, int N);
int main()
{
...
This will give the compiler its expectations for those symbols, so when it sees them in use it knows what to do with them.
Any name must be defined before its using (with rare exceptions relative to template). You use name mean in statement
m = mean(mydata, N);
but name mean is not yet defined.
Place function declarations before main
double mean(double *mydata, double N);
double standard_dev(double *mydata, double m, int N);
int main()
{
//...
Or before their usage.
int main()
{
double mean(double *mydata, double N);
double standard_dev(double *mydata, double m, int N);
//...
m = mean(mydata, N);
//...
You need to have prototypes for your functions mean() and std_dev() at the top of your file. Otherwise, they don't 'exist' in the main function. Either copy the entire functions to the space above your main function, or make prototypes for them. Good luck!

Program triggers breakpoint; doesn't run

Hi I'm a beginner level programmer and I have an issue with a program I've been working on. The point is to create a an individual class that has an array consisting of random generated numbers as one private variable, a size for the array, and a number representing the fitness of the individual through the use of the array.
It utilizes a header file, the source file for the header, and source file to test the header. For some reason, whenever I try to compile I reach a breakpoint, and Visual Studio doesn't tell me what the error is. I suspect it has something to do with the private pointer in my class but I don't know why or how to fix the error.
Header
#ifndef INDIVIDUAL_H
#define INDIVIDUAL_H
class individual
{
int size;
double fitness;
double* genotype;
public:
individual(int pSize = 10);
individual(const individual& copy);
~individual();
double* getGenotype();
double getFitness();
int getSize();
void mutation();
void crossover(individual a);
};
#endif
Header source
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#define M_PI 3.14159265358979323846
#define M_E 2.71828182845904523536
#include <cmath>
#include "individual.h"
using namespace std;
double RandomFloat(double min = -32.768, double max = 32.768)
{
min = min;
max = max;
unsigned int seed;
seed = (unsigned int) time(0) + rand();
srand(seed);
double r = (double)rand() / (double)RAND_MAX;
return min + r * (max - min);
}
double Fitness(double a[], int size)
{
double fitness;
double firstSum, secondSum;
firstSum = 0;
for(int i = 0; i<size; i++)
{
firstSum += a[i]*a[i];
}
firstSum /= size;
secondSum = 0;
for(int i = 0; i<size; i++)
{
secondSum += cos(2*M_PI*a[i]);
}
secondSum /= size;
fitness = -20*exp(-0.2*sqrt(firstSum) - exp(secondSum) + 20 + M_E);
return fitness;
}
individual::individual(int pSize)
{
size = pSize;
genotype = nullptr;
genotype = new double(size);
for(int i = 0; i<size; i++)
{
genotype[i] = RandomFloat();
}
fitness = Fitness(genotype,size);
}
individual::individual(const individual& copy)
:size(copy.size),genotype(new double[copy.size])
{
std::copy(copy.genotype, copy.genotype + copy.size, genotype);
}
individual::~individual()
{
delete[] genotype;
}
double* individual::getGenotype()//returns a pointer
{
return genotype;
}
double individual::getFitness()
{
return fitness;
}
int individual::getSize()
{
return size;
}
void individual::mutation()
{
int first, second;
double temp;
first = (int)RandomFloat();
second = (int)RandomFloat();
temp = genotype[first];
genotype[first] = genotype[second];
genotype[second] = temp;
}
void individual::crossover(individual a)
{
int crossPoint = size/3 - 1;
for(int i = crossPoint; i<size; i++)
{
double temp1;
temp1 = 0;
temp1 = genotype[i];
genotype[i] = a.genotype[i];
a.genotype[i] = temp1;
}
}
Driver source
#include <iostream>
#include <string>
#include <stdlib.h>
#include <cmath>
#include <vector>
#include "individual.h"
#define M_PI 3.14159265358979323846
#define M_E 2.71828182845904523536
using namespace std;
int main()
{
individual test;
int size = test.getSize();
cout << size << endl;
for(int i = 0; i<size; i++)
{
cout << test.getGenotype()[i] << endl;
}
return 0;
}
I've tried searching for possible solutions (added the copy constructor and destructor) and nothing seems to resolve the problem.
Any help would be much appreciated.
To allocate an array of double
Change:
genotype = new double(size); // this initialize one double and initialize value to size
TO
genotype = new double[size]; // this creates an array which size is 'size'
Your code overruns memory when you only allocate one double and write data to memory
for(int i = 0; i<size; i++)
{
genotype[i] = RandomFloat();
}