1

I'm trying to write a C++ program that:

  1. Creates an array_1 of size n and fills it following the formula x(i) = x(0) + h*i where n, x(0) and h are input parameters;

  2. Creates an array_2 of size l=n+1 and fills it following the formula x(i) = ((b-a)/n)*i+a , where a, b are input parameters;

  3. Creates an array_sin of size l=n+1 and fills it with the sine of the elements of array_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.

Community
  • 1
  • 1
riccardo
  • 13
  • 4
  • 2
    `double *x = new double(n);` does not allocate an array of doubles. You need `double *x = new double[n];`. Voting to close for typo. – R Sahu Nov 20 '19 at 23:40
  • Note: When you see a weird result code like 3221226356, convert it to hexadecimal and see if it is more recognizable. In this case C0000374 isn't immediately recognizable to me, but google has pages and pages of hits. – user4581301 Nov 20 '19 at 23:49
  • thank you @RSahu it was a typo... now it works – riccardo Nov 21 '19 at 00:02

1 Answers1

1

You're not creating arrays of double here:

double *x = new double(n); //array_1
double *y = new double(l);  //array_2
double *sine = new double(l);   //array_3

but single values, you want something like:

double *x = new double[n]; //array_1
double *y = new double[l];  //array_2
double *sine = new double[l];   //array_3
Paul Evans
  • 27,315
  • 3
  • 37
  • 54