# Thread: Array Comparison and Modification

1. ## Array Comparison and Modification

This is what my code is intended to do: When called with the following array:
Code:
`{ 1, 1, 1, 2, 2, 1, 2, 3, 3, 1, 1 }`
, your function should modify the array to
Code:
`{1, 2, 1, 2, 3, 1}`
and store it.

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];
newArray[0]=arrSeries[0];
for(i = 1; i != arrLength; i++)
{
if(newArray[j] != arrSeries[i-1]);
{
newArray[++j] = arrSeries[i];
}

}

while(counter !=6)
{
printf("%d  ", newArray[counter]);
counter++;
}

}```
However it is not working as intended. It works as if the IF statement doesn't even exist ! Where am I going wrong?
Thanks

2. Code:
`if(newArray[j] != arrSeries[i-1]);`
That semi-colon at the end of your "if()" statement is making that statement do nothing.

3. Originally Posted by Matticus
Code:
`if(newArray[j] != arrSeries[i-1]);`
That semi-colon at the end of your "if()" statement is making that statement do nothing.
Oops ! That was a typo...edited and recompiled the code. Still No luck Gives erroneous results: 1 2 2 3 1 2686632

Also how should I modify the code to satisfy this problem?

Write a function compact ( ) which takes as parameters an array of integers and its length.The function should modify the array, so that all consecutive
occurrences of the same; integers are replaced by a single occurrence of that integer.
The function should return the length of this new array.

4. Originally Posted by Debojyoti Das
Code:
`        if(newArray[j] != arrSeries[i-1])`
Are you sure you want to compare to element [i-1]?

PS: when you see entries like 2686632 popping up, you should suspect that you might be running out of the bounds of your array somewhere.

5. Originally Posted by R.Stiltskin
Are you sure you want to compare to element [i-1]?

PS: when you see entries like 2686632 popping up, you should suspect that you might be running out of the bounds of your array somewhere.
Yeah I thought that ! Thanks ! I got the error !

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

}```
gives correct output.

But another question, as arrays are static data structures in C, is this question solvable using C?

Write a function compact ( ) which takes as parameters an array of integers and its length.The function should modify the array, so that all consecutive
occurrences of the same; integers are replaced by a single occurrence of that integer.
The function should return the length of this new array.

Thanks Again

6. Of course it's solvable. Think.

7. Originally Posted by R.Stiltskin
Of course it's solvable. Think.
Are you suggesting I use dynamic memory allocation?

8. Many solutions.
One would be to create dynamically a new array and fill it with the elements. But your function returns only the length of the array....
So are you sure you need to create a new array??
The requirement says to modify the array!

9. Originally Posted by Debojyoti Das
Are you suggesting I use dynamic memory allocation?
Say you were, for a moment, the tip of the pen, and you start with index 0 of this array:
Code:
`int arrSeries[11] = {1, 1, 1, 2, 2, 1, 2, 3, 3, 1, 1 };`
You look ahead, and see the next number is the same as your current number.

What would you have to do to get rid of that current number, and move all the higher numbers, down by one index?

Work on it with paper and pen, and see what patterns of logic you need to follow to make this happen. Repeat it a few times, and the patterns will become evident.

And that's the backbone of your programs logic. Don't let someone give you the answer - work it out.

10. Originally Posted by std10093
Many solutions.
One would be to create dynamically a new array and fill it with the elements. But your function returns only the length of the array....
So are you sure you need to create a new array??
The requirement says to modify the array!
I assume that means I should create the first array (that keeps the repeating number dynamically so that I can modify it later). Trying to code it now..

11. Originally Posted by Adak
And that's the backbone of your programs logic. Don't let someone give you the answer - work it out.
Tip to remeber forever!
Originally Posted by Debojyoti Das
I assume that means I should create the first array (that keeps the repeating number dynamically so that I can modify it later). Trying to code it now..
I am not sure what you want to accomplish. I would suggest to follow Adak's advice. But before doing that read again and again the requirement!

12. Originally Posted by std10093
Tip to remeber forever!

I am not sure what you want to accomplish. I would suggest to follow Adak's advice. But before doing that read again and again the requirement!
Sorry I overlooked Adak's post while refreshing the page. I get the logic, I will simply shift the digits and throw out the repeating digits...say

Array s -> 1 1 1 2 2 3 1 4 1
Fisrt Pass -> Check Index 0 and 1 > Index 0 is top -> Pop index 0 -> Set index 1 to s.top.
Next Pass - > 1 1 2 2 3 1 4 1
Next Pass - > 1 2 2 3 1 4 1 g
Next Pass - > 1 2 3 1 4 1 g g

now the indices with g has the previous value or I may set it to 0. Will that be the solution?

13. Nice explanation and nice idea Bravo. Try it out and post back even though it work!! I am eager to see the result

14. Originally Posted by std10093
Nice explanation and nice idea Bravo. Try it out and post back even though it work!! I am eager to see the result
Thanks ! Working on it !

15. Originally Posted by Debojyoti Das
Sorry I overlooked Adak's post while refreshing the page. I get the logic, I will simply shift the digits and throw out the repeating digits...say

Array s -> 1 1 1 2 2 3 1 4 1
Fisrt Pass -> Check Index 0 and 1 > Index 0 is top -> Pop index 0 -> Set index 1 to s.top.
Next Pass - > 1 1 2 2 3 1 4 1
Next Pass - > 1 2 2 3 1 4 1 g
Next Pass - > 1 2 3 1 4 1 g g

now the indices with g has the previous value or I may set it to 0. Will that be the solution?
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.

Popular pages Recent additions