返回时更改double的C++值
C++ value of double is changed when returned
C++和学习的新手。此程序返回正确的输出。我将函数原型更改为void,以隔离并确保函数提供正确的输出。
#include <iostream>
#include <fstream>
void ArraySortToMedian(int x[], int numElem);
using namespace std;
int main()
{
ifstream infile;
infile.open("numbers.txt");
const int SIZE = 6;
int array[SIZE];
int i;
if(!infile)
{
cout << "couldn't find 'numbers.txt'";
return 1;
}
while(i < SIZE && infile >> array[i])
i++;
infile.close();
for(i = 0; i < SIZE; i++)
cout << array[i] << "n";
ArraySortToMedian(array, SIZE);
return 0;
}
void ArraySortToMedian(int x[], int numElem)
{
bool swap;
int temp, i;
double m;
do
{
swap = false;
for(i = 0;i < (numElem - 1); i++)
{
if( x[i] > x[i + 1] )
{
temp = x[i];
x[i] = x[i + 1];
x[i + 1] = temp;
swap = true;
}
}
}
while (swap);
cout << "n";
for(i = 0; i < numElem; i++)
cout << x[i] << "n";
m = (x[numElem/2] + x[numElem/2]-1)/(double)2;
cout << "n" << m;
}
输出:
6
5
3
1
2
4
1
2
3
4
5
6
3.5
当我删除void并将其替换为double以返回main()时,中值如下。
#include <iostream>
#include <fstream>
double ArraySortToMedian(int x[], int numElem);
using namespace std;
int main()
{
ifstream infile;
infile.open("numbers.txt");
const int SIZE = 6;
int array[SIZE];
int i;
double median;
if(!infile)
{
cout << "couldn't find 'numbers.txt'";
return 1;
}
while(i < SIZE && infile >> array[i])
i++;
infile.close();
for(i = 0; i < SIZE; i++)
cout << array[i] << "n";
median=ArraySortToMedian(array, SIZE);
cout<< "n" << median << "n";
return 0;
}
double ArraySortToMedian(int x[], int numElem)
{
bool swap;
int temp, i;
double m;
do
{
swap = false;
for(i = 0;i < (numElem - 1); i++)
{
if( x[i] > x[i + 1] )
{
temp = x[i];
x[i] = x[i + 1];
x[i + 1] = temp;
swap = true;
}
}
}
while (swap);
cout << "n";
for(i = 0; i < numElem; i++)
cout << x[i] << "n";
m = (x[numElem/2] + x[numElem/2]-1)/(double)2;
return(m);
}
失真输出:
1
6
5
3
1
2
1
1
2
3
5
6
2.5
当返回时,它移动在main()中生成的数组元素,而之前我只是从ArraySortToMedian()输出。我认为这与我引用数组第一个元素的起始地址有关。可能很简单,但由于我的经验有限,我对这种行为感到不知所措。任何帮助我了解自己做错了什么的人都将不胜感激。非常感谢。
问题在于您的输入循环:
int i;
// ... snip ...
while(i < SIZE && infile >> array[i])
i++;
您从未初始化过i
,所以这是未定义的行为。也许它有效,也许它不起作用。
如果使用std::vector
而不是数组,这会更容易:
std::vector<int> values;
int next;
while (infile >> next) {
values.push_back(next);
}
现在,您既不受大小限制,也不必担心跟踪索引。
相关文章:
- C++在数学计算中将double转换为int
- 在c++中为double类型的数组创建一个unique_ptr
- vector<vector<double>> to mxArray using memcpy
- (double) 和 double() 之间的区别
- C++标准是否允许<double>在没有开销的情况下实现 std::可选
- C++ Version Of Double.longBitsToDouble
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- 为什么 nlohmann/json 序列化 "null" 而不是在 double 上"0"?
- 使用 std::vector<double> 访问由 std::unique_ptr<double[2] 管理的数据>
- 将 std::vector<double> 从 C++ 包装到 C 以在 Swift 中使用
- 错误:无法将"<lambda(double)>"转换为"double(*)(double)"
- 如何正确将"strings"转换为"double"?
- 如何在<double>矢量中创建复杂类型的模板<T>?
- "double* grade"、"double *grade"和"double* fn()"有什么区别?
- 在不损失精度的情况下将double从C++传输到python
- 重复使用预分配的向量<复杂<double>>作为<double>长度两倍的向量
- 编译器给出错误:format 指定类型 'float *',但参数的类型'double' [-Wformat]
- Zedboard zynq-7000 Opencl 浮点数从类型 'double*' 强制转换为类型 'double' 无效
- test1.cpp:9:77:错误:对"(const std::normal_distribution) <double>(std::mt19937&)"的调用不匹配