Create and print two source arrays of real numbers X(N) , Y(M) (S decimal places). Define an array with fewer negative elements. Use functions: initialization with random numbers, output, determining the number of negative elements in arbitrary arrays of integers. C++. Compilation is fine, but displays an error "Segmentation fault (core dumped)"
enter code here
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <math.h>
#include <time.h>
using namespace std;
const int S=2;
void init(double a[], int n, double b[], int m) {
int c=0, k=0;
srand(time(0));
cout<<"Enter the number of elements in the array X:";
cin>>n;
cout<<"Array X is:";
for(int i=0; i<n;i++) {
a[i]=rand()%50-100/3.;
cout.precision(S);
cout<<setw(8)<<fixed<<a[i]<<" ";
}
for (int i=0; i<n; i++)
if (a[i]<0) c++;
cout<<endl;
cout<<"Enter the number of elements in the array Y:";
cin>>m;
cout<<"Array Y is:";
for(int i=0; i<m;i++) {
b[i]=rand()%50-100/7.;
cout.precision(S);
cout<<setw(8)<<fixed<<b[i]<<" ";
}
for (int i=0; i<m; i++)
if (b[i]<0) k++;
cout<<endl;
cout<<"Total negative elements in the array X: "<<c<<"."<<endl;
cout<<"Total negative elements in the array Y: "<<k<<"."<<endl;
if(c>k)
cout<<"Array X has more negative elements than array Y."<<endl;
else if (c<k)
cout<<"Array Y has more negative elements than array X."<<endl;
else
cout<<"Array X and array Y have the same number of negative elements."<<endl;
}
int main() {
int N, M;
double X[N],Y[M];
init(X,N,Y,M);
return 0;
}