简体   繁体   English

我的代码中的垃圾结果

[英]Garbage Result in my code

Hey guys so I was working on this code that finds the same numbers and display them and so far the result is just random numbers I need help. 大家好,所以我正在研究可找到相同数字并显示它们的代码,到目前为止,结果只是我需要帮助的随机数。

int main(void){
int arr[10] = {1, 2, 3, 4, 5, 4, 8 ,8, 9, 10};
int i;
int j;
int same[10];
int ctr = 0;

for(i = 0; i < 10; i++){
    for(j = 10; j > 0;j--){
        if(arr[i] == arr[j]){
            same[ctr++] = arr[i];//store the similar numbers
        }
    }
}

for(i = 0; i < 10; i++){
    printf("%d", same[i]);
}
getch();
return 0;}

You're contradicting yourself. 你自相矛盾。 For the same array, you're using two different indexing scheme!! 对于同一数组,您正在使用两个不同的索引方案!

  • In outer loop ( ascending index ), you're indexing from 0 to 9 在外循环( 升序索引 )中,您的索引从09
  • In inner loop ( decending index ), you're indexing 10 from 1 . 在内部循环( 递减索引 )中,您从1开始索引10

The inner loop indexing is off-by-one , thereby accessing out of bound memory, which is invalid . 内部循环索引是一对一的 ,从而访问了无效的绑定内存。 This causes undefined behavior . 这会导致不确定的行为

You need to make it 你需要做到

for(j = 9; j >= 0;j--)

After that, you're attempting to print the values of all elements in the same array, whereas all ( or none ) the element values may not be assigned a value, actually. 之后,您尝试打印same数组中所有元素的值,而实际上( 可能没有 )为所有( 或无 )元素值分配值。 You left the automatic variable uninitialized, thereby containing indeterminate values. 您未对自动变量进行初始化,从而包含了不确定的值。 Since the variable has never its address taken, attempting to use the value will again cause UB. 由于该变量从未使用过其地址,因此尝试使用该值将再次导致UB。

That said, once a match is found, you can use continue to jump to the outer loop. 就是说,一旦找到匹配项,就可以使用continue跳转到外部循环。

从9开始第二个循环( j循环),然后转到0。

I ran your code on codechef compiler, it was giving a run time error. 我在codechef编译器上运行了您的代码,它给出了运行时错误。 Because in your ctr++ step , value of ctr exceeded the size of the array same[]. 因为在您的ctr ++步骤中,ctr的值超出了数组same []的大小。

Moreover, in your code you are comparing one element with itself so every number will be printed in the list(since i==j may be a possible case) 而且,在您的代码中,您正在将一个元素与其自身进行比较,因此每个数字都将被打印在列表中(因为i == j可能是这种情况)

It will be better if you include a condition i!=j with it or iterate j from i+1 to 9 so that no self comparisons are done. 如果包含条件i!=j或将j from i+1 to 9迭代j from i+1 to 9这样最好不进行自我比较,那会更好。 The following is a working snippet of the same code(your for loop) :- 以下是相同代码的工作片段(您的for循环):-

for(i = 0; i < 10; i++){
    for(j = 10; j > 0;j--){
        if(arr[i] == arr[j] && i!=j){
            same[ctr] = arr[i];//store the similar numbers
            ctr++;
            cout<<ctr<<" ";
        }
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的指针和结构代码显示垃圾值 - My pointers and struct code is displaying garbage values 为什么我的代码在 C 中返回垃圾值 - Why my code is returning garbage value in C 使用文件实现复制命令我的代码正在复制垃圾? - implementing copy command using file my code is copying garbage? 为什么我的代码给我乱码,有时出现分段错误? - Why is my code giving me garbage values and sometimes segmentation fault? 我的C代码中有一个奇怪的结果 - A strange result in my C code 为什么我的代码会从数组中打印出一些垃圾? - Why does my code print out some garbage from my array? 对于我的 C 代码,我正在读取文件并对其进行排序给我垃圾输出。 为什么? - For my C code, where I am reading a file and sorting it is giving me garbage outputs. Why? 为什么我的程序在提供足够的输入后却以o / p给出垃圾值? 代码如下: - why my program is giving garbage value in o/p after providing sufficient inputs? code is as follows: 为什么每次我输入 10 进行评级输入时,我的 C 代码 output 垃圾值? - Why my C code output garbage value each time i enter 10 for rating input? 为什么我的 c 中的矩阵乘法代码总是给出垃圾值? (使用共享 memory 和 fork) - Why my Matrix Multipication code in c always give the garbage value? (using shared memory and fork)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM