# Thread: Array Comparison and Modification

1. Originally Posted by R.Stiltskin
That's a viable approach and certainly worth doing, if only for the benefit of the programming practice it gives you. However, think about how many operations the computer will be performing as compared to your original program. To me, it seems a horribly inefficient way to solve an extremely simple problem. So after you finish that, you should go back to the original version and think about it some more. When you come to your `Eureka' moment you'll see what I mean.
While I do not fully disagree, tell me about the time complexity of the operation that Debojyoti is going to make. Of course it's not optimum, but he is probably a beginner in C.

By the way I didn't know that you say Eureka too

2. We shouldn't turn this into a debate about complexity, but doesn't repeatedly shifting the entire array change it from O(n) to O(n2)?

But even ignoring the time complexity, it makes the program itself much more complicated than is necessary.

Oh, and some of us have actually heard of a fellow named Archimedes as well!

3. Exactly

Of course you do

4. Originally Posted by std10093
Exactly

Of course you do
Here's my solution (it work's ) but it became quite complex and due to the shifting the garbage value also shows.
Now will think on R.Stiltskin's idea.

Code:
```#include <stdio.h>#include <stdlib.h>
#define MAXSIZE 10
int compact(int[]);
int main()
{
int num=0, numarray[MAXSIZE]={0,0,0,0,0,0,0,0,0,0}, i=0;
int top = -1;
printf("Please enter some numbers. Enter -1 to stop.\n");
while(num != -1)
{
scanf("%d", &num);
if(top==(MAXSIZE-1) || num == -1)
{
printf("Done !\n");
break;
}
else
{
top+=1;
numarray[top] = num;
}
}

while(i<MAXSIZE)
{
printf("%d ", numarray[i]);
i++;
}
printf("\nPrinting compact numvers.\n");
compact(numarray);
getchar();
return 0;
}
int compact(int changeArray[MAXSIZE])
{
int i=0, j=0, k=0, l=0;
while(l<MAXSIZE)
{
i=0, j=0;
while(i < MAXSIZE)
{
if(changeArray[i]==changeArray[i+1])
{
j=i;
while(j < MAXSIZE)
{
changeArray[j]=changeArray[j+1];
j++;
}
}

i++;
}
l++;
}

while(k<MAXSIZE)
{
printf("%d ", changeArray[k]);
k++;
}
getchar();
return 0;
}```
I did not implement the size returning function as I am not very satisfied with the garbage values creeping in. Maybe I will modify to store 0/-1 instead of garbage and check conflict if user inputs the same.

5. Spelling
Code:
`printf("\nPrinting compact numvers.\n");`
Of course take a shot in R.Stiltskin's idea.

6. Nice job. Now why don't you modify the program so it counts (and prints) the number of assignments performed (i.e., how many times a number is moved from one place to another) while processing a given input. Then do the same thing with your previous program and compare the number of operations for the same input array.

7. Originally Posted by R.Stiltskin
Nice job. Now why don't you modify the program so it counts (and prints) the number of assignments performed (i.e., how many times a number is moved from one place to another) while processing a given input. Then do the same thing with your previous program and compare the number of operations for the same input array.
You are recommending me to find out it's time complexity. Sure thing doing it now .

8. Originally Posted by std10093
Spelling
Code:
`printf("\nPrinting compact numvers.\n");`
Of course take a shot in R.Stiltskin's idea.
Why did they keep v and b adjacently on the keyboard ??

9. Originally Posted by Debojyoti Das
Why did they keep v and b adjacently on the keyboard ??
Shh... this is the C programming forum, not VB

10. Code:
```while(l<MAXSIZE)	{
i=0, j=0;
while(i < MAXSIZE)
{
if(changeArray[i]==changeArray[i+1])
{
j=i;
while(j < MAXSIZE)
{
changeArray[j]=changeArray[j+1];
j++;
}

stepcounter++;
}

i++;
}
l++;
}```
So, I added a step-counter here and also here:

Code:
```for(i = 0; i != arrLength; i++)    {        if(newArray[j] != arrSeries[i])
{
newArray[++j] = arrSeries[i];
stepcounter++;
}```
Attachment 12372

Attachment 12373

No doubt my previous code was much more efficient than this one. Trying to modify the previous code to make it work dynamically .

12. Originally Posted by std10093
It got deleted for reasons I don't know

1 2 1 2 3 1
The number of steps: 5

1 1 1 2 2 1 3 3 1 1
Printing compact numbers.
1 2 1 3 1 1 1 1 1 1
Steps Performed: 63
Well it's the number of steps my first and second piece of code does for similar set of numbers. In fact the second piece of code was provided a number less yet it took substantially more steps to compute than the first step, which did the computation with one number more in the series in so less steps. Sorry for being ambiguous in my previous post .

13. No problem

So, can you post the code that you think is the best you have until now?

14. Originally Posted by std10093
No problem

So, can you post the code that you think is the best you have until now?
Code:
```#include <stdio.h>#include <stdlib.h>
void compact (int[], int);
int main()
{
int arrSeries[11] = {1, 1, 1, 2, 2, 1, 2, 3, 3, 1, 1 };
int arrLength = 11;
compact(arrSeries, arrLength);
getchar();
return 0;
}

void compact(int arrSeries[], int arrLength)
{
int i, j= 0, counter=0, newArray[11], stepcounter = 0;
newArray[0]=arrSeries[0];
for(i = 0; i != arrLength; i++)    {
if(newArray[j] != arrSeries[i])
{
newArray[++j] = arrSeries[i];
stepcounter++;
}
}

while(counter !=6)
{
printf("%d  ", newArray[counter]);
counter++;
}
printf("\nThe number of steps: %d", stepcounter);

}```
This one, but it is highly restricted as it works only with a static data set.

15. Only one for loop in the function which is good. Well do you really need stepcounter? I would say that j has the same value with stepcounter when you reach line 31.