This code works fine on my system . Why is judge showing compilation error on line 8 ?

#include
using namespace std;

int main(){
int t,in,tmp,a[100005],i,j=0,n,c,count;
cin>>t;
while(t--){
	a[100005]={0}; //this is line 8
	cin>>in;
	n=0;
	while(in!=0){
	 tmp=in%10;
		a[j]=tmp;
		j++;n++;
		in/=10;
		
}

count=0;c=0;
for(i=0;i<n;i++){

	if(a[i]!=c){
		count++;
		c=a[i];
	}
	//if(a[i]==1)
	//count++;
}
cout<<count+1<<endl;}
return 0;

}

What are you trying to do in line 8?

If you are trying to initialize all array elements to 0, you should use a loop (or some other way), as the syntax you are using is wrong.

For all assignments, a[index] refers to a single element.

a[100005] = 0;

will get you past syntax error. But, that is accessing memory outside of allocated locations.

I am trying to initialize all elements with 0.

That is not the correct syntax then, do int a[10000]={0}, doing a[1000]=0 refers to the 999th element of array, not the whole array. Such initialization which you are doing is only valid at the time of declaration.

1 Like