You are not logged in. Please login at www.codechef.com to post your questions!

×

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

#include<iostream>
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;

}

asked 30 Jan '15, 23:19

shubham99's gravatar image

2★shubham99
2403932
accept rate: 5%

edited 30 Jan '15, 23:20


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.

link

answered 30 Jan '15, 23:26

tijoforyou's gravatar image

2★tijoforyou
4.2k52364
accept rate: 15%

I am trying to initialize all elements with 0.

(30 Jan '15, 23:32) shubham992★
1

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.

(30 Jan '15, 23:41) damn_me3★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,718
×1,911
×1,477
×1,302
×29

question asked: 30 Jan '15, 23:19

question was seen: 1,107 times

last updated: 30 Jan '15, 23:41