0

Today I created a program which have 3 Functions:

sortArray(array,length);
removeDuplicateInArray(array, length);
max = findMax(array, length);

The program works fine but, if I run it more times let say, three times, the output is only one OK, other two is different and i think that somehow has to do with the length of the array in findMax function, because i remove duplicates and the array does not have the same size . I'm not sure if there is the problem.

The program is this:

#include<stdio.h>

void sortArray(int *array, int length){
    int i, j, k, temp;

    for(i=0;i<length-1;i++){
        for(k=0;k<length-i-1;k++){
            if(array[k]<array[k+1]){
                temp=array[k];
                array[k]=array[k+1];
                array[k+1]=temp;
            }
        }
    }

    for(j=0;j<length;j++){
        printf("%d ",array[j]);
    }
    printf("\n\n");
}

void removeDuplicateInArray(int *array, int length){
    int i,j,k;

    for (i = 0; i < length; i++) {
      for (j = i + 1; j < length;j++) {
         if (array[j] == array[i]) {
            for (k = j; k < length; k++) {
               array[k] = array[k + 1];
            }
            length--;
         }else{
            j++;
         }
      }
   }

   for (i = 0; i < length; i++) {
      printf("%d ", array[i]);
   }
   printf("\n\n");
}

int findMax(int *array, int length){
    int i;
    int max = array[0];

    for(i=1;i<length;i++){
        if(max == array[i]){
            continue;
        }

        if(max<array[i]){
            max = array[i];
        }
    }
    return max;
}

int main(void){
    int array[] = {-9,-7,-3,-1,9,7,3,1,-8,-6,-4,-10,-2,8,6,4,2,5,-5,-10};
    int length = sizeof array / sizeof array[0];
    int max;

    sortArray(array,length);
    removeDuplicateInArray(array, length);
    max = findMax(array, length);

    printf("Max = %d\n", max);
    return 0;
}

and the output is:

michi@michi-laptop:~$ ./program 
9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -10 
9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 

Max = 9
michi@michi-laptop:~$ ./program 
9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -10 

9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 

Max = 9
michi@michi-laptop:~$ ./program 
9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -10 

9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 

Max = 2034093120
michi@michi-laptop:~$ ./program 
9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -10 

9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 

Max = 912874208
michi@michi-laptop:~$ ./program 
9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -10 

9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 

Max = 1269451840
michi@michi-laptop:~$ ./program 
9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -10 

9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 

Max = 1946221408
michi@michi-laptop:~$ ./program 
9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -10 

9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 

Max = 9

The output should be 9, but the output is not always 9

7
  • instead of passing the array length as a parameter, why not get the length of the array in each function? Commented Aug 25, 2015 at 17:44
  • 2
    @devlin carnate and how do you get the length of that array if i pass it as pointer ? Commented Aug 25, 2015 at 17:45
  • @cad i did that already, or you mean something else ? Commented Aug 25, 2015 at 17:51
  • in function: remove....array() this line: 'array[k] = array[k + 1];'will access beyond the upper boundary of the array. This is undefined behaviour and can lead to a seg fault event. Commented Aug 25, 2015 at 20:01
  • this line: 'if(array[k]<array[k+1]){' in the sortarray() function is not correct. It should be: 'if(array[k] > array[k+1]){' to sort the array ascending Commented Aug 25, 2015 at 20:02

2 Answers 2

7

The removeDuplicateInArray function changes the length of the array, but the calling function, main in your case, doesn't know about the new length.

You can either return the new length from the function:

int removeDuplicateInArray(int *array, int length)
{
    // code as above

    return length;
}

and call it like this:

length = removeDuplicateInArray(array, length);

or you can pass the length as a pointer, which will reflect the canges:

void removeDuplicateInArray(int *array, int *plength) ...
{
    int length = *plength;

    // use and modify length as above

    *plength = length;
}

and call it like this:

removeDuplicateInArray(array, &length);

I prefer the second variant, because it doesn't allow you to accidentally forget about the return value.

The garbage value you see is shifted in from beyond the array's bounds, because you loop up to k < length and access athe element at index k + 1, which might be length, which is one element beyond the array.

Sign up to request clarification or add additional context in comments.

9 Comments

I don't think you need those voids before the function name when you call the function.
@M Oehm this means that if i use a pointer (int *lenght) i will have to use cast "error: comparison between pointer and integer [-Werror]|"
@TheParamagneticCroissant: Oh, I've left them there for the compiler to find them. (Fixed, thanks!)
you could return the new length
@Michi: No, you won't need casts. But you must use *length inside the function where you usually would use just length.
|
3

In removeDuplicateInArray pass the length by pointer so it will have the proper value later in the main void removeDuplicateInArray(int *array, int *length).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.