finding 100!

there is some compilation error in it..not able to find..
http://ideone.com/Pvped
plzz..hv a look
closed account (o3hC5Di1)
Hi there,

You can't do this:

1
2
//line 9
arr[160]={0};


Because you have already initialised the array on line 4.
Remove that line and it should compile: http://ideone.com/46p21

All the best,
NwN
Last edited on
yup..m waiting
Remove the underlined one:
arr[160]={0};
actually i want to loop arr[160]={0}; so that array gets initialised to 0 every time loop runs..so..i cant delete this line

P.S. see ur own code..output is wrong..
for input
2
100
100
output shud be same for both 100's.
leave everything said above..tell me whats wrong with this code
http://ideone.com/Pvped
closed account (o3hC5Di1)
You can't "re-initialise" a variable - that's the point of it - it only happens initially on creation of it.

If you want to fill it with zero values, you need a separate for loop:

1
2
for (int i=0;i<160;i++)
    arr[i] = 0;


You asked me about compilation errors, I didn't check what the program does and if it's output is correct if all you ask is "how can i make this compile".

If you have any further questions, please be a little bit more specific on what it is you're trying to do with the program.

All the best,
NwN
okay..so u mean to say i cant do the following??

int arr[160]=0;
//somewhere later in program
arr[160]=0;

closed account (o3hC5Di1)
No - you can perfectly well do that.

You are misunderstanding the difference between initialisation and altering values.
Initialising the array:

int arr[160] = {0};

This means "create an array of 160 values of type int and give the first element a value of zero".

Altering a value in the array:
arr[160] = 0;

This means "set the value at position 160 in array arr to 0"

Hope that clears it up for you.

All the best,
NwN
Last edited on
sry ...i meant that can i do the following?

int arr[160]={0};
//somewhere later in program
arr[160]={0};
closed account (o3hC5Di1)
No, that's not possible - again: curly braces are only valid whilst initialising.

If you want to reset the first value of arr to 0:
arr[160] = 0;

If you want to reset all the values of arr to zero:

1
2
for (int i=0;i<160;i++)
    arr[i] = 0;


All the best,
NwN
ok..thnk u so much..so i conclude curly brackets cn be used only while initialising and not in middle of code.

thnk u so much
closed account (o3hC5Di1)
Correct.

Curly braces allow you to set multiple values at once, which can only be done at initialisation:

 
int arr[3] = {0,1,2}; //create array of 3 ints and fill it with values 1,2,3 


After that, you have to access all the values individually as such:

arr[0] //=value 0 in array above

All the best,
NwN
Topic archived. No new replies allowed.