C++ 为什么我的代码在int数组中放入假值?
由于某种原因,我从这段代码中得到了错误的输出结果 在这里,我给代码一个最高的数字和一个除法器数字,我从0到最高的数字检查所有的数字,看看是否有任何数字可以与除法器数字除法,如果是,它会存储它。我认为问题在于存储数据,我就是不明白为什么C++ 为什么我的代码在int数组中放入假值?,c++,C++,由于某种原因,我从这段代码中得到了错误的输出结果 在这里,我给代码一个最高的数字和一个除法器数字,我从0到最高的数字检查所有的数字,看看是否有任何数字可以与除法器数字除法,如果是,它会存储它。我认为问题在于存储数据,我就是不明白为什么 #include <iostream> #include <cmath> int HN = 200; //Number of which we look for the dividable numbers int DN = 3; //
#include <iostream>
#include <cmath>
int HN = 200; //Number of which we look for the dividable numbers
int DN = 3; //The divider number
int STOR[3]; //[(int)floor(HN/DN)]; //Storage of dividable numbers
int VAR = 0; //Array index
void multiples(int HNt, int DNt){
for(int i=0;i<HNt;i++){
if (i%DNt==0){
STOR[VAR]=i;
VAR++;
};
};
};
int main(){
std::cout<<floor(HN/DN)<<"\n";
multiples(HN,DN);
for(int i=0; i<=VAR-1;i++){
std::cout<<STOR[i]<<"\n";
};
return 0;
};
我排除了剩下的代码,因为它在198之前实际上是正确的
任何帮助都将提前感谢:)欢迎
我曾经尝试过一些整理,但我自己还是一个初学者,所以请把这当作一些友好的建议
伊戈尔·坦德尼克所说的是正确的。存储系统中没有分配足够的空间来记录所有结果
想象一下,你将第一个数字写入插槽0,然后写入插槽1,然后写入插槽2。。。现在怎么办?在尺寸过小的数组结束后,“堆栈”上可能还有其他数据。程序正在使用的其他数据。现在你写下这些,创建未定义的结果。然后经过几次写操作,由于这是一个小程序,您可能会在堆栈上找到“空闲”的未使用内存,这就是为什么在继续进行时,它会给出正确的结果
为什么不为您的存储使用一个动态大小的容器,例如std::vector
如果不是,至少将数组大小与最大可能结果(即红利)联系起来。。。见下文
#include <iostream>
#include <cmath>
//Moved your variables out of the global scope. Lots of reasons for this.
//Take in your parameters as referecnes and a pointer to your storage facility.
static void multiples(const int& HNt, const int& DNt, int* STOR, int& VAR)
{
for (int i = 0; i < HNt; i++) {
if (i % DNt == 0) {
STOR[VAR] = i;
VAR++;
};
};
};
int main() {
const int HN = 200; //Made first two const as they do not change
const int DN = 2;
int STOR[HN]; //Const variables can be used to size your array
//This is great because now your array size is linked to the dividend(HN)
int VAR = 0;
std::cout <<"Floor : "<< floor(HN / DN) << "\n";
multiples(HN, DN, STOR, VAR);
for (int i = 0; i <= VAR - 1; i++) {
std::cout << STOR[i] << "\n";
};
std::cin.get(); //Stop immediate return to allow displayed results.
return 0;
}
#包括
#包括
//将变量移出全局范围。原因很多。
//将参数作为参考和指向存储设备的指针。
静态无效倍数(常数int&HNt、常数int&DNt、int*STOR、int&VAR)
{
对于(int i=0;i std::您的程序是否通过访问超出边界的索引而表现出未定义的行为。intstor[3]
数组只有足够的空间来存储三个元素;您试图在其中存储三个以上的元素。您不需要地板。一个int
除以一个int
就是一个int
而不是一个float
或double
小数部分在除法中丢失。小调:“dividable”是更常见的“division”对于(inti=0;iso而不是floor(HN/DN),它应该只是HN/DN?同样在intstor[3]中,我可以实现这一点吗?
#include <iostream>
#include <cmath>
//Moved your variables out of the global scope. Lots of reasons for this.
//Take in your parameters as referecnes and a pointer to your storage facility.
static void multiples(const int& HNt, const int& DNt, int* STOR, int& VAR)
{
for (int i = 0; i < HNt; i++) {
if (i % DNt == 0) {
STOR[VAR] = i;
VAR++;
};
};
};
int main() {
const int HN = 200; //Made first two const as they do not change
const int DN = 2;
int STOR[HN]; //Const variables can be used to size your array
//This is great because now your array size is linked to the dividend(HN)
int VAR = 0;
std::cout <<"Floor : "<< floor(HN / DN) << "\n";
multiples(HN, DN, STOR, VAR);
for (int i = 0; i <= VAR - 1; i++) {
std::cout << STOR[i] << "\n";
};
std::cin.get(); //Stop immediate return to allow displayed results.
return 0;
}