0

Write an interactive C program to remove the duplicates in an array from the list of "N" numbers given. I am bit confused because it's not giving me the correct output and I'm not able to debug the code. please help because program not giving the required output. gives incorrect values for case 2, code runs well if I just don't do it without switch case

#include<stdio.h>
int main()
{
    int arr[10], size, i , j=0, temp, choice, k=0;

    printf("Enter the size of array:\n");
    scanf("%d", &size);
    printf("Enter the numbers in array:\n");
    for(i=0; i<size; i++)
    {
        scanf("%d", &arr[i]);
    }

    /*sorting the array numbers */

    for(i=0; i<size; i++)
    {
        for(j=i+1; j<size; j++)
        {
            if(arr[i]>arr[j])
            {
                temp = arr[i];
                arr[i]  = arr[j];
                arr[j] = temp;
            }
        }
    }
    /*sorting completed*/
    printf("---------------------------------------------------\n");
    printf("---------------------------------------------------\n");
    printf("\tEnter 1 print sorted array.\n");
    printf("\tEnter 2 print array without duplicats.\n");
    printf("---------------------------------------------------\n");
    printf("---------------------------------------------------\n");
    scanf("%d", &choice);
    switch(choice)
    {
        case 1: for(i=0; i<size; i++)
                {
                    printf("%d- %d\n", i, arr[i]);
                }
                break;
        case 2: for(i=0; i<size; i++)
                {
                    if(arr[k]!=arr[i+1])
                        {
                            arr[k] = arr[i];
                            k++;
                        }
                }   
                printf("\nk=%d\n",k);
                for(i=0; i<k; i++)
                {
                    printf("%d\n", arr[k]);
                }
                break;
        default: printf("Invalid Choice made!");
    }

}

please help

Pradnya Sinalkar
  • 1,116
  • 4
  • 12
  • 26
  • 2
    https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb – Yunnosch Jun 29 '18 at 05:36
  • 1
    You print arr[k] without changing k. But in any case, you are actually adjusting the array, which is probably bad practice. Once sorted, you can just keep track of the last value and don't print the current value if it's the same – LoztInSpace Jun 29 '18 at 05:36
  • @LoztInSpace Your comment has done the debugging service asked for by the question. Please turn it into an answer, in order to get this out of the list of unanswered questions. – Yunnosch Jun 29 '18 at 05:39
  • This is a very simple `remove duplicates from array in C` program, just google it, and you will get answer from numorous C tutorial website. – Abhishek Jaiswal Jun 29 '18 at 05:44
  • Possible duplicate of [sort and remove duplicates from int array in c](https://stackoverflow.com/questions/18924792/sort-and-remove-duplicates-from-int-array-in-c) – Abhishek Jaiswal Jun 29 '18 at 05:46
  • "I'm not able to debug the code" -- why? – schaiba Jun 29 '18 at 08:33

4 Answers4

0

You print arr[k] without changing k.

        for(i=0; i<k; i++)
        {
            printf("%d\n", arr[k]);  // <--- Should be i not k
        }
LoztInSpace
  • 5,584
  • 1
  • 15
  • 27
0

Change your switch case 2 code into this :->

for (i = 0; i < size; i++) {
  for (j = i + 1; j < size;) {
     if (arr[j] == arr[i]) {
        for (p = j; p < size; p++) {
           arr[p] = arr[p + 1];
        }
        size--;
     } else
        j++;
  }
}

for (i = 0; i < size; i++) {
  printf("%d ", arr[i]);
}
Abhishek Jaiswal
  • 243
  • 1
  • 18
0

You have a logical error and silly mistake in printing values. You should change the code of case 2: like this...

for(i=0; i<size; i++) {
    if (i==0) continue;

    if (arr[i]!=arr[k-1])
    {
        arr[k++] = arr[i];
    }
}

printf("\Number of unique value = %d\n",k);
for(i=0; i<k; i++) printf("%d\n", arr[i]);
fgamess
  • 1,276
  • 2
  • 13
  • 25
0

It's very simple. Just record the last value.

#include<stdio.h>

int main(int argc, char *argv[])
{
    int arr[10], size, i , j = 0, temp, choice, k = 0;
    int last;
    int uni_index;

    printf("Enter the size of array:\n");
    scanf("%d", &size);
    printf("Enter the numbers in array:\n");
    for(i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }

    /*sorting the array numbers */

    for(i = 0; i < size; i++)
    {
        for(j = i + 1; j < size; j++)
        {
            if(arr[i] > arr[j])
            {
                temp = arr[i];
                arr[i]  = arr[j];
                arr[j] = temp;
            }
        }
    }
    /*sorting completed*/
    printf("---------------------------------------------------\n");
    printf("---------------------------------------------------\n");
    printf("\tEnter 1 print sorted array.\n");
    printf("\tEnter 2 print array without duplicats.\n");
    printf("---------------------------------------------------\n");
    printf("---------------------------------------------------\n");
    scanf("%d", &choice);
    switch(choice)
    {
        case 1: 
                for( i= 0; i < size; i++)
                {
                    printf("%d- %d\n", i, arr[i]);
                }
                break;
        case 2: 
                uni_index = 0;
                printf("%d - %d\n", uni_index++, arr[0]);
                last = arr[0];
                for(i = 1; i < size; i++)
                {
                    if (arr[i] != last)
                    {
                        printf("%d - %d\n", uni_index++, arr[i]);
                        last = arr[i];
                    }  
                }
                break;
        default: 
                printf("Invalid Choice made!");
    }

    return 0;
}

By the way, you need take care of code format about usingspace.

Joy Allen
  • 402
  • 3
  • 8