Originally Posted by
std10093
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.
Yes j has the same value, thank you for making me notice . Changed the code and also added data acceptance from user:
Code:
#include <stdio.h>#include <stdlib.h>
#define MAXVALUE 10
void compact (int[], int);
int main()
{
int arrSeries[MAXVALUE], i=0;
int arrLength = MAXVALUE;
printf("Please enter %d numbers.\n", MAXVALUE);
while(i != MAXVALUE)
{
scanf("%d", &arrSeries[i]);
i++;
}
compact(arrSeries, arrLength);
getchar();
return 0;
}
void compact(int arrSeries[], int arrLength)
{
int i, j= 0, counter=0, newArray[MAXVALUE]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
newArray[0]=arrSeries[0];
for(i = 0; i != arrLength; i++) {
if(newArray[j] != arrSeries[i])
{
newArray[++j] = arrSeries[i];
}
}
while(counter !=MAXVALUE)
{
printf("%d ", newArray[counter]);
counter++;
}
printf("\nThe number of steps: %d", j);
getchar();
}