开发者

Why am i getting this kind of output since i know that the default values of non-initialized integer elements of an array is 0?

#include <iostream>

using namespace std;

void RemoveZeroElements(int arr1[],int arr2[],int &i){
    int n=0;
    int m=0;
    while(n<14) {
        switch(arr1[n]) {
            case 0:
                n+=1;
            break;
            default:
                arr2[m]=arr1[n];
                m+=1;
                n+=1;
                i+=1;
             break;

         }
      }
}


int main()
{
    int ar1[14]={2,4,5,0,7,-9,0,0,11,23,44,0,13,999};
    int ar2[14];
    int efsize=0;

    RemoveZeroElements(ar1,ar2,efsize);

    cout<<"the new array without the zeros has an effective size of "<< efsize << endl;

    for (int i=0;i<14;i++) {

        if(ar2[i]!=0) {
            cout << "the new array has its " << (i+1)<< "th element set to " << 
            ar2[i]<< endl;
        } 
     }

}

The output i get is the following:

the new array without the zeros has an effective size of 10
the new array has its 1th element set to 2
the new array has its 2th element set to 4
the new array has its 3th element set to 5
the new array has its 4th element set to 7
the new array has its 5th element set to -9
the new array has its 6th element set to 11
the new array has its 7th element set to 23
the new array has its 8th element set to 44
the new array has its 9th element set to 13
the new array has its开发者_JS百科 10th element set to 999
the new array has its 11th element set to 1
the new array has its 12th element set to 65535
the new array has its 13th element set to 4308980
the new array has its 14th element set to -1079890440

The problem as you see is in the 12th,13th and 14th elements


What you know is wrong. The initial values of array elements of POD type (such as int or void * or struct some_standard_c_structure) with function scope is undefined. ar2 is full of garbage.

The initial values of array elements with static or global scope is 0 (not taking into account multithreading issues.)

You must make sure to clear the contents of ar2 before you use it. The simplest way to do so is with std::fill():

std::fill(ar2, ar2 + (sizeof(ar2) / sizeof(int)), 0);

In C, the equivalent is memset():

memset(ar2, 0, sizeof(ar2));


Because you are wrong. Those are uninitialized values, could be anything. If they were static storage duration objects, instead of auto storage duration objects, they would be initialized to zero. It's best not to think about it and always initialize your variables.


What you're doing here is setting the first ten elements of ar2 to the numbers in ar1 that aren't zero. Your problem elements aren't just the 12th, 13th, and 14th, it also includes the 11th as well. You'll notice that the number 1 is nowhere in your original array also.

The last four elements aren't zero, they're completely undeclared, because of what he said ^^ up there. If you want the ar2 to be ar1 without the zeros, just do a count in a for loop that counts how many elements are NOT zero, and initialize ar2 to that number.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜