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;
printf("Please enter %d numbers.\n", 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.