-2

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;
}
Alivia
  • 1
  • 4
    `N` and `M` are uninitialized when you use them to create arrays. You try to create arrays with some random, unknown sizes. – Yksisarvinen Oct 22 '20 at 09:22
  • 1) `int N, M; double X[N],Y[M];` Not to mention, that VLAs are non-standard C++, but this is undefined behavior, even on compilers, that have extensions, that allow VLAs. What is the value of `N`, or `M`, at the point of array declarations? 2) If you are a beginner, consider learning C++ from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Oct 22 '20 at 09:23

1 Answers1

1

In C++, you can't declare an array with a size read from input. You especially can't declare it with a size that will be read from input later.

If you want a dynamically sized array, use std::vector.

#include <iostream>
#include <vector>
#include <math.h>
#include <time.h>

void init() {

    srand(time(0));
    std::cout<<"Enter the number of elements in the array X:";
    std::size_t n
    std::cin >> n;
    std::vector<double> a(n);
    std::cout<<"Array X is:";
    for(int i=0; i<n;i++) {
        a[i]=rand()%50-100/3.;
        cout.precision(S);  
        std::cout<<setw(8)<<fixed<<a[i]<<" ";
    }
    
    int c = 0;
    for (int i=0; i<n; i++)
        if (a[i]<0) c++;
        
    std::cout<<std::endl;
    std::cout<<"Enter the number of elements in the array Y:";
    std::size_t m;
    std::cin>>m;
    std::vector<double> b(m);
    std::cout<<"Array Y is:";
    for(int i=0; i<m;i++) {
        b[i]=rand()%50-100/7.;
        cout.precision(S);
        std::cout<<setw(8)<<fixed<<b[i]<<" ";
    }

    int k = 0;        
    for (int i=0; i<m; i++)
        if (b[i]<0) k++;

    std::cout<<endl;
    
    std::cout<<"Total negative elements in the array X: "<<c<<"."<<std::endl;
    std::cout<<"Total negative elements in the array Y: "<<k<<"."<<std::endl;
    
    if(c>k)
        std::cout<<"Array X has more negative elements than array Y."<<std::endl;
    else if (c<k)
        std::cout<<"Array Y has more negative elements than array X."<<std::endl;
    else 
        std::cout<<"Array X and array Y have the same number of negative elements."<<std::endl;
    
}

int main() {
    init();
    return 0;
}

using namespace std is a bad habit. If your instructor tells you to use it, they are wrong.

Look at std::uniform_real_distribution along with a generator from <random> for generating random doubles between two limits.

Look at std::count_if with a function bool isNegative(double x) { return x < 0 } for counting how many numbers are negative.

Caleth
  • 52,200
  • 2
  • 44
  • 75