我有两个数组:
数组1:
<dependency>
数组2:
e[0] = 21.145;
e[1] = 17.152;
e[2] = 15.541;
e[3] = 0.095;
e[4] = -10.515;
我需要合并并订购这些数组。这是我的代码,但它正在为逆序(作为升序)工作。我希望有关降序数组合并的帮助。我尝试更换操作员,但它没有工作。我错在哪里?
double *f = new double[5];
f[0] = 18.458;
f[1] = 11.542;
f[2] = 7.152;
f[3] = 1.243;
f[4] = -12.758;
答案 0 :(得分:1)
使用正确的符号,代码可以正常工作:
#include <iostream>
void mergeArrays(double * arr1, double * arr2, int n1, int n2, double * arr3)
{
int i = 0, j = 0, k = 0;
while (i<n1 && j <n2)
{
if (arr1[i] > arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}
while (i < n1)
arr3[k++] = arr1[i++];
while (j < n2)
arr3[k++] = arr2[j++];
}
int main()
{
double *e = new double[5];
e[0] = 21.145;
e[1] = 17.152;
e[2] = 15.541;
e[3] = 0.095;
e[4] = -10.515;
double *f = new double[5];
f[0] = 18.458;
f[1] = 11.542;
f[2] = 7.152;
f[3] = 1.243;
f[4] = -12.758;
double *g = new double[10];
mergeArrays(e,f,5,5,g);
for(int i=0;i<10;i++)
std::cout<<g[i]<<std::endl;
delete e;
delete f;
delete g;
return 0;
}
结果:
21.145
18.458
17.152
15.541
11.542
7.152
1.243
0.095
-10.515
-12.758