# logica question

This is a discussion on logica question within the C Programming forums, part of the General Programming Boards category; Hi guys! I got a simple problem but I dont know whats wrong in my code. The situation is: I ...

1. ## logica question

Hi guys! I got a simple problem but I dont know whats wrong in my code.
The situation is:
I have an array of integers:
int rptds[] { 12, 32, 40, 12, 35, 95, 32, 12}; (any else)
Then I want to overwrite the repeated numbers with the next:
12, 32, 40, 12, 35, 95, 32, 12
12, 32, 40, 35, 95

look the code:

Code:
```int a, b, c, d;
a = b = c = d = 0;

while (rptds[a++]);
a--;

for (b=0; b<=a; b++)
{
for (c=b+1; c<=a; c++)
{
if (rptds[b]==rptds[c])
{
for (d=c; d<a; d++)
rptds[d]=rptds[d+1];
}
}
}```
thanks for any help

2. The code looks fine. The rptds[] array should be "terminated" with a 0, which you didn't post. Other than that, you will have to be more specific about the problem you are having.

gg

3. the problem is that in some cases it doesn't overwrite repeated numbers and in others it overwrite non-repeated numbers with repeated ones. this code isn't working properly.

4. Post an rptds[] array that doesn't work.

gg

5. When calculating the size of an array, its safer to use
Code:
`a = sizeof( rptds ) / sizeof( int ) - 1;`
Also
Code:
```for (d=c; d<a; d++)
rptds[d]=rptds[d+1];
a--;```

6. >while (rptds[a++]);

If you want to use such like this, you need to put a terminating symbol in your array. The while-loop ends when the condition is false, in this case the while-loop will end if rptds[i] == 0 for a certain i.

Also note that in your for-loops, the conditions should be < a, instead of <= a. This is because you start counting at 0. In your code the loop runs from 0 to a, which implies an array length of a+1, but it the array has length a.

If I understand you correctly, you want to remove the double values and shift the array to the left when found. I have not tested the following:

Code:
```for (i = 0; i < length; i++)
{
// check for duplicates
for (j = i; j < length; j++)
{
if (array [i] == array [j])
{
// duplicate found, shift array to left
for (k = j; j < length; k++)
{
array [k] = array [k+1];
}
}
}
}```
You could make the algorithm more faster by realising that shifting is only needed until the terminating symbol is found.

7. well, my way to do it is stupid but...
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void) {
int arr[7] = {11,25,55,33,11,27,25};
int i,j;

for (i=0; i<7; ++i)
for (j=i+1; j<7; ++j) //I think here I explode the buffer
if (arr[i] == arr[j])
arr[j] = 0;

for (i=0; i<7; ++i) {
if (arr[i] != 0)
printf("%d ",arr[i]);
}
}```

8. Also note that in your for-loops, the conditions should be < a, instead of <= a.
Wrong! "while (rptds[a++]);a--;" will put 'a' on the index of rptds[] that contains 0, and this assumption is used in the remainder of the code.

Like I said, I don't see anything wrong with the code you originally posted, ipe (aside from a few optimizations like the one Xsquared posted).

gg

9. I thought a was the length of the array. My mistake. Anyway, this is wrong:while (rptds[a++]);, because the array has no termination symbol. It goes outside the array range and then behaviour is undefined.

10. thank you guys!!

XSqaured:

thank you. was missing a--!

a = sizeof( rptds ) / sizeof( int ) - 1;
pretty one! it may be faster

11. a-- will optimize the code, but it won't fix it.
Try this array, it should clear up why things aren't working as you (and I originally) expected:
Code:
`int rptds[] = {12, 12, 12, 12, 12, 12, 12, 12, 12, 0};`
Give another post if you are still stuck.

gg

12. still failing when a sequential of 2 repeated numbers is in array.
look: ...28,28....

Code:
```#include <stdio.h>

int main()
{
int rptds[] = { 31,17,22,25,28,13,20,20,25,28,28,32 };
int a,b,c,d,e;
a=b=c=d=e=0;

a = sizeof(rptds)/sizeof(int)-1;
printf("0 -> %d\n",a);
for (b=0;b<=a; b++) printf("%d|",rptds[b]);

for (b=0; b<=a; b++)
{
for (c=b+1; c<=a; c++)
{
if (rptds[b]==rptds[c])
{
for (d=c; d<a; d++)
rptds[d]=rptds[d+1];
a--;
}
}
}
puts("\n--------------------\n");
printf("0 -> %d\n",a);
for (b=0;b<=a; b++) printf("%d|",rptds[b]);
return(0);

}```
just compile it. you will see easily.

13. finish!

where is a--; must be a--; c--;

14. Just for fun, here's a more generic way of find the number of elements in an array:
Code:
```#include <stdio.h>

#define ASIZE(a) (sizeof(a) / sizeof(*a))

int main(void)
{
int myarray[10];
printf ("There are %d elements in the array\n", ASIZE(myarray));
return 0;
}

/*
* Output:
There are 10 elements in the array
*
*/```