c++ - 我是初学者,我不明白错误是什么。你能帮助女孩吗?
问题描述
创建并打印两个实数源数组 X(N) , Y(M)(S 个小数位)。定义一个负元素较少的数组。使用功能:用随机数初始化,输出,确定任意整数数组中负元素的数量。C++。编译正常,但显示错误“Segmentation fault (core dumped)”
在此处输入代码
#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;
}
解决方案
在 C++ 中,您不能声明具有从输入读取的大小的数组。您尤其不能使用稍后将从输入中读取的大小来声明它。
如果您想要一个动态大小的数组,请使用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
是个坏习惯。如果您的教练告诉您使用它,那他们就错了。
std::uniform_real_distribution
与生成器一起查看<random>
用于在两个限制之间生成随机双精度数。
用std::count_if
一个函数bool isNegative(double x) { return x < 0 }
来计算有多少是负数。
推荐阅读
- html - Angular 模板:防止伪元素换行
- javascript - 为什么将 gl.texImage2D 中的格式设置为 gl.LUMINANCE 而不是 gl.RGB 会使画布上的 blob 文件大小仅小约 5%?
- log4net - 是否有 Microsoft.Extensions.Logging.ILogger 支持的 log4net.ILog 实现?
- javascript - 如何在 Angular 中使用 zone.js?
- html - 如何仅隐藏移动设备或移动视图的 div 类文本?
- rgb - 旋转时得到黑白图像?
- apache-spark - 这个 Hive 查询有问题吗?
- azure - 如何删除文本文件中引号内而不是行尾的 \r\n 换行符
- spring-boot - HttpMediaTypeNotAcceptableException:在异步模式下而不是在同步模式下找不到可接受的表示
- loops - 去掉PDF密码保护,知道密码WINDOWS