samedi 23 janvier 2016

Why is the value in the loop never greater than 2?

I have the loop below, the value of unique is never greater than 2. n is the total number of integers in the array (I read integers from a file line by line).

I am completely stumped and have been for a long time. Why is the value of unique never greater than 2?

int last = array[0];
int unique = 1;
int i;

for (i = 1; i < n; i++) {
    if (array[n] != last) {
        last = array[n];
        unique++;
        printf("i:%d\n", i);
        printf("n:%d\n", n);
        printf("last:%d\n:", last);
    }
}
printf("unique:%d\n", unique);

OUTPUT:

 i:1
n:9
last:0
:unique:2
 i:1
n:4
last:4
:unique:2
 i:1
n:5
last:4
:unique:2
 i:1
n:5
last:4
:unique:2
 i:1
n:5
last:4
:unique:2
 unique:1
 i:1
n:20
last:0
:unique:2

Aucun commentaire:

Enregistrer un commentaire