problem in initializing of array?

in question buy-1 get-1 ,i have used freq[52]={0}(to initialize frequency array) in while loop of test case.While submitting, it gave wrong answer.after reading submitted solutions,i declared freq[52] in global scope and used for(i=0;i<52;i++)freq[i]=0 to initialize array in while loop,and it gave right answer.what’s difference?

As far as i know ,The statement freq[52]={0} (in general any declaration like my_array[SIZE] = {0}) can only be used once when initializing the array.It does not work any other time.

u mean this is wrong… ?
We cannot do this?

while(test–)
{
int arr[52]={0};
// any statement
}

@codeluv @damn_me

Any declaration like my_array[SIZE] = {0}) can only be used once when initializing the array. For proof here is a simple code (in C++):

t=6;

int arr[6]={0};

while(t--)
{
arr[t]=t;
arr[6]={0};   //This should re-initialize array to zero but it does not!!
for(i=0 ; i<6 ;i++)
    printf("%d ",arr[i]);
printf("\n");
}

Ans here is the output :

 0 0 0 0 0 5
 0 0 0 0 4 5
 0 0 0 3 4 5
 0 0 2 3 4 5
 0 0 2 3 4 5
 0 1 2 3 4 5

Moral of the story : Always re-initialize array manually (using a for loop) and no shortcuts!!

2 Likes

This would work fine but if you are defining the array in the global scope and then initializing the array as arr[52]={0} won’t work as told by @kcahdog in the answer.

1 Like

@kcahdog @vermashubhang thanks very much :slight_smile:

//