hello friends sorry for posting this basic question here but i can't figure it out why my variable "tasse" is getting 8 or (the value of variabale i) in this code below.

``````#include <stdio.h>
int main(){
int leght = 15;
int tasse =0;
int random;
int array[15]={1,5,4,7,8,5,1,0,9,7,0,0,5,4,10};
int i,j;
for(i=0; i<leght; ++i){
if(array[i]==0){
//increase variable tasse by 1 BUT IT IS GETTING VALUE 8 CAN'T UNDESTAND WHY//
tasse++;
/*FOR J FROM I WE ARE FINDING A VALUE >0 TO EXCHANGE WITH I THIS WORKS WELL*/
for(j=i; j<leght; ++j){
if((array[i]==0)&&(array[j]>0)){
array[i] = array[j];
array[j]=0;
}
}
}
}
leght -= tasse;
printf("Tasse: %d\n", tasse);//here i can see tasse is 8 i can't undestand why
printf("leght: %d\n", leght);
for(i=0; i<leght; ++i){
printf("%d\n", array[i]);
}
return 0;
}
``````
3
Contributors
4
Replies
35
Views
2 Years
Discussion Span
Last Post by toxicandy

I am pretty sure what is happening is that your swaping area is not working too well, since you replace array[j] with 0 your next array[i] will AGAIN equal 0, think of it this way, the first 7 variables are none zeros, as soon as you hit a zero you finish out with all 0's. I ran your code with a bunch of print statements and here is your arrea at the end: [1,5,4,7,8,5,1,9,7,5,4,10,0,0,0] which works but look at the following:

``````for(j=i; j<leght; ++j){
if((array[i]==0)&&(array[j]>0)){
array[i] = array[j];
array[j]=0;
}
}
``````

The if does work but what happens when your array hits that first 0 is that 0 gets 9 which is right BUT that 9 spot gets a 0 which means at your next pass of i (array[i] == 0) = trues so that means tasse++ which means tasse = 2 at this point. Guess what happens next, the next two get switched which means again you're pushing 0 towards the next open spot which again will get counted. That is why tasse is 8. Here is a sample fix, though there may be better fixes:

``````  for(i=0; i<leght; i++){

if(array[i] == 0){
//increase variable tasse by 1 BUT IT IS GETTING VALUE 8 CAN'T UNDESTAND WHY//
/*FOR J FROM I WE ARE FINDING A VALUE >0 TO EXCHANGE WITH I THIS WORKS WELL*/
for(j=i; j<leght; j++){
if((array[i]==0)&&(array[j]>0)){
array[i] = array[j];
array[j]=0;
}
}
}
if(array[i] == 0){
tasse++;
}
}
``````
since i replace my next array[j] my next array[i] will not be zero because `if((array[i]==i) && (array[j]>0))` will be false until we get out from the j loop, and when my array hits the first 0 why tasse++ is equal to 2 ?? and not 1?? and i as i have 3 zeros in total why tasse is not equal to 6 and it is equal to 8 ??.
your fixes works prety fine but i can't undestand why because in that point `array[i] > 0` because it get value `array[j]` but writing `array[j]=0` after `array[i] = array[j];`doesn't change while i'm not using pointer.
`if((array[i]==i) && (array[j]>0))` This is correct, it will exit that statement HOWEVER that statement is just for switching variables, for instance, if you had an array `1,2,3,0,4,5` once it got to zero Tasse would go up to 1 and what would happen is your array would be transformed to `1,2,3,4,0,5` when i gets increased by 1 it sees a 0 again because it was moved so tasse goes up one and it enters that loop and your new and final array becomes `1,2,3,4,5,0` but tasse becomes 3. In your array the first 0 occurs at the 8th index which means that first 0 gets moved over 8 times making tasse equal to 8 at the end of execution and not 6. The only real changes I made to your code was creating a new if statement at the end after you transform your array values, this means array[i] will only equal 0 if all the characters after it are also 0's which means it is located at the end of the array, that is where it counts up tasse, your swapping works fine and so does most everything else, it was just the location of the counter.