Thread: getting garbage values in the ouput i guess code is correct !!!

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    10

    getting garbage values in the ouput i guess code is correct !!!

    Write a C function with 1-D array and its size as arguments, which is called by main() function and remove the duplicate elements from an array also display the resultant array? ( this is the question )

    Code:
    #include <stdio.h>
    
    #define MAX_SIZE 100 
    int duplicate_elements(int arr[], int n);
    int main()
    {
        int arr[MAX_SIZE]; 
        int n;          
        int i, j, k;     
        printf("Enter size of the array : ");
        scanf("%d", &n);
        printf("Enter elements for the array : ");
        for(i=0; i<n; i++)
        {
            scanf("%d", &arr[i]);
        }
       duplicate_elements(arr,n);
        printf("\nArray elements after deleting duplicates is  : ");
        for(i=0; i<n; i++)
        {
        printf("%d\t", arr[i]);
        }
    
        return 0;
    }
    int duplicate_elements(int arr[], int n)
    {
    	int i,k,j;
      for(i=0; i<n; i++)
        {
            for(j=i+1; j<n; j++)
            {
                if(arr[i] == arr[j])
                {
                    for(k=j; k<n; k++)
                    {
                        arr[k] = arr[k + 1];
                    }
                    n--;
    
                   
                    j--;
                }
            }
        
    	}
    
    	}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to return the new length of the array, after you've removed the duplicates.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    10
    Quote Originally Posted by Salem View Post
    You need to return the new length of the array, after you've removed the duplicates.

    i did not get you can you elaborate please ?? otherwise please do mention the line which needs to be included !!!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Punju
    i did not get you can you elaborate please
    You start off with n elements as the size of the array. If there are any duplicates, after removing them, the size of the array would surely be less than n, but you did not update the value of n, hence you get those "garbage values".

    Quote Originally Posted by Punju
    please do mention the line which needs to be included
    It looks like you declared duplicate_elements as having a return type, but you did not actually return a value. You might as well return the new effective size of the array so that you can update n in main.

    By the way, duplicate_elements doesn't sound like a good name for the function: it sounds like you're duplicating elements rather than removing duplicates. A name like remove_duplicate_elements or even more simply remove_duplicates would be better.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-21-2013, 10:35 PM
  2. Garbage values in File
    By wayne08 in forum C Programming
    Replies: 5
    Last Post: 04-11-2010, 11:01 AM
  3. garbage values
    By j.vick in forum C Programming
    Replies: 2
    Last Post: 04-06-2010, 03:31 PM
  4. problem of garbage values
    By aldajlo in forum C Programming
    Replies: 5
    Last Post: 10-02-2004, 04:41 PM
  5. Garbage values
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-03-2002, 05:17 PM

Tags for this Thread