C++ I';我是个初学者,我不明白错误是什么。你能帮助那个女孩吗?
创建并打印两个实数X(N)、Y(M)(S小数位)的源数组。定义具有较少负元素的数组。使用函数:初始化随机数,输出,确定任意整数数组中负元素的数量。C++。编译正常,但显示错误“分段错误(核心转储)” 在这里输入代码C++ I';我是个初学者,我不明白错误是什么。你能帮助那个女孩吗?,c++,C++,创建并打印两个实数X(N)、Y(M)(S小数位)的源数组。定义具有较少负元素的数组。使用函数:初始化随机数,输出,确定任意整数数组中负元素的数量。C++。编译正常,但显示错误“分段错误(核心转储)” 在这里输入代码 #include <iostream> #include <iomanip> #include <stdio.h> #include <math.h> #include <time.h> using namespace st
#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;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
常数int S=2;
void init(双a[],整数n,双b[],整数m){
int c=0,k=0;
srand(时间(0));
coutn;
C++中的CUT,不能声明从输入中读取的大小数组。尤其是不能用稍后将从输入中读取的大小声明它。
如果需要动态调整大小的数组,请使用
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;
}
#包括
#包括
#包括
#包括
void init(){
srand(时间(0));
标准::cout n;
std::载体a(n);
std::coutN
和M
在您使用它们创建数组时未初始化。您尝试创建具有一些随机未知大小的数组。1)int N,M;double X[N],Y[M],更不用说,VLAs是非标准C++,但这是未定义的行为,即使是在有扩展的编译器上,也允许VLAs。在代码声明的时候,<代码> N<代码>或“代码> M<代码>的值是什么?2)如果你是初学者,考虑从C++学习C++。