1. Originally Posted by R.Stiltskin
Still working on this, I see. I was hoping that by the time you finished implementing the step-counting you would have realized how simply (and efficiently) you could complete the assignment with only a minor modification to your original program. Does this give you a hint?
Code:
```#include <stdio.h>#include <stdlib.h>
#define MAXVALUE 10
int compact (int[], int);
int main()
{
int arrSeries[MAXVALUE], i=0, newArrayLength;
int arrLength = MAXVALUE;
while(i != MAXVALUE)
{
scanf("%d", &arrSeries[i]);
i++;
}
newArrayLength=compact(arrSeries, arrLength);
printf("The new array length is %d.\n", newArrayLength);
getchar();
return 0;
}
int compact(int arrSeries[], int arrLength)
{
int i=0, j= 0, counter=0, newArray[MAXVALUE];
newArray[0]=arrSeries[0];
for(i = 0; i != arrLength; i++)
{
if(newArray[j] != arrSeries[i])
{
newArray[++j] = arrSeries[i];
}
}
j++;
while(counter < j)
{
printf("%d  ", newArray[counter]);
counter++;
}
printf("\nThe number of steps: %d.\n", j);
return j;
}```
Isn't this the solution you intended? Or is there an even more elegant method other than in place compacting ?
Thanks.

2. Since you have newArray, that is not in-place. But as I said, it looks like you're close. Remove newArray. How would you adjust your algorithm to work with just arrSeries?

3. I showed how to solve this kind of problem efficiently in a recent thread. Basicaly you use destination and source "iterators", copying from one to the other (even though they may initially point to the same item), and only advancing the source iterator in some cases.

Your case is slightly different here. The above should just give you the idea, and you'll still have to work out the exact the details yourself.

4. In the current code attempts, the "iterators" are basically the indices. The idea is already there and implemented, just that it isn't yet in-place

5. Oh so it is.

6. Originally Posted by laserlight
Code:
`(i < max_size && scanf("%d", &numbers[i]) == 1)`
Sorry for my so less experience in coding, but I have never seen a while loop number reading like this. Can you please explain the logic so that I can implement loops like this myself? The implementation seems very elegant to me. I know that a while(1) loop is an infinite loop, but I don't' get the ==1 testing part.
Thanks !.

7. Basically, the idea is to keep reading integers until the max_size is reached, or there are no more integers left to read. Read up on scanf to understand its return value.

8. Originally Posted by laserlight
Basically, the idea is to keep reading integers until the max_size is reached, or there are no more integers left to read. Read up on scanf to understand its return value.
Sorry for being late, here's the solution that I did, don't know how efficient it is...

Code:
```#include <stdio.h>#include <stdlib.h>
{
size_t i = 0;
printf("Please enter up to %d integers (trigger EOF to stop early).\n", max_size);
while (i < max_size && scanf("%d", &numbers[i]) == 1)
{
++i;
}
return i;
}

void print_numbers(const int numbers[], size_t size)
{
size_t i;
printf(">>> ");
for (i = 0; i < size; ++i)
{
printf("%d ", numbers[i]);
}
printf("<<<\n");
}

size_t compact(int numbers[], size_t size)
{
size_t i, j=0, k=0;
if (size <= 1)
{
return size;
}
else
{
for(i=(size-1); i>0; i--)
{
if(numbers[i-1]==numbers[i])
{
k=i;
while(k!=size)
{
numbers[k-1]=numbers[k];
k++;
}
j++;
}
}
size=size-j;
return (size);
}
}

int main(void)
{
int numbers[10];
size_t size = read_numbers(numbers, sizeof(numbers) / sizeof(numbers[0]));
size = compact(numbers, size);
print_numbers(numbers, size);
system("pause");
return 0;
}```

9. In the worst case, it's still O(n2).

You would do better to go back to your compact function in post #61 and make it in-place by simplifying it, rather than by complicating it. Essentially all you have to do is eliminate counter and newArray.

10. Originally Posted by R.Stiltskin
In the worst case, it's still O(n2).

You would do better to go back to your compact function in post #61 and make it in-place by simplifying it, rather than by complicating it. Essentially all you have to do is eliminate counter and newArray.
Will try to do the same.