I'm trying to write a C++ program that:
Creates an
array_1
ofsize n
and fills it following the formulax(i) = x(0) + h*i
wheren
,x(0)
andh
are input parameters;Creates an
array_2
ofsize l=n+1
and fills it following the formulax(i) = ((b-a)/n)*i+a
, wherea
,b
are input parameters;Creates an
array_sin
ofsize l=n+1
and fills it with the sine of the elements ofarray_2
.
I decided to do these three steps in three separate void functions. Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
void fillarr1 (double ar_x[], int n, double x_0, double h);
void fillarr2 (double ar_y[], double a, double b, int n, int l);
void Sin (double ar_y[], double sine_[], int l);
int main ()
{
int n, i;
double a, b, h, x_0;
cout << "Number of points \n";
cin >> n;
int l=n+1;
double *x = new double(n); //array_1
double *y = new double(l); //array_2
double *sine = new double(l); //array_3
cout << "Separation between points \n";
cin >> h;
cout << "x(0) = ";
cin >> x_0;
fillarr1 (x, n, x_0, h);
cout << "\na = ";
cin >> a;
cout << "\nb = ";
cin >> b;
fillarr2 (y, a, b, n, l);
cout << "\nsin \n";
Sin(y, sine, l);
cout << "\n\n\n";
for (i=0; i<n; i++)
{
cout << x[i] << "\t";
}
cout << "\n\n\n";
for (i=0; i<l; i++)
{
cout << y[i] << "\t";
}
cout << "\n\n\n";
for (i=0; i<l; i++)
{
cout << sine[i] << "\t";
}
delete [] x;
delete [] y;
delete [] sine;
return 0;
}
void fillarr1 (double ar_x[], int n, double x_0, double h)
{
int i;
for (i=0; i<n; i++)
{
ar_x[i]=x_0+h*i;
cout << ar_x[i] << "\t";
}
}
void fillarr2 (double ar_y[], double a, double b, int n, int l)
{
int i;
double h_;
h_=(b-a)/(n);
for (i=0; i<l; i++)
{
ar_y[i]=a+h_*i;
cout << ar_y[i] << "\t";
}
}
void Sin(double ar_y[], double sine_[], int l)
{
int i;
for(i=0;i<l;i++)
{
sine_[i]=sin(ar_y[i]);
cout << sine_[i] << "\t";
}
}
I'm also trying to print the elements of the arrays both in the void functions and in the main. It appears that, after the function calls, the original dynamic arrays passed to the void functions are filled with random values. Furthermore, the process exits with return value 3221226356. Here is an output example: output
I really don't know how to fix it, any help would be very appreciated.