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.