Can anybody please provide a solution to the following question in C?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharathkumar
    New Member
    • Nov 2006
    • 1

    Can anybody please provide a solution to the following question in C?

    Try to find out the duplicate element while inserting an integer element into an array of size 100. The inter numbers should 1 to 99. If i start inserting the number from 1 to 99 randomly without repeating the previous inserted element into array. At the instance if the number repeats we have find the index of the array and we have to find out the duplicated number and return.

    Example:
    array[0]=3;
    array[1]=7;
    array[2]=88;
    array[3]=45;
    array[4]=33;
    array[5]=99;
    array[6]=7;
    At this stage the C program should exit and display the information as duplicated element is 7 and index = 6;
    Write a C Program using only one "for loop".
    Only one for loop to be used is the constraint.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sharathkumar
    Try to find out the duplicate element while inserting an integer element into an array of size 100. The inter numbers should 1 to 99. If i start inserting the number from 1 to 99 randomly without repeating the previous inserted element into array. At the instance if the number repeats we have find the index of the array and we have to find out the duplicated number and return.

    Example:
    array[0]=3;
    array[1]=7;
    array[2]=88;
    array[3]=45;
    array[4]=33;
    array[5]=99;
    array[6]=7;
    At this stage the C program should exit and display the information as duplicated element is 7 and index = 6;
    Write a C Program using only one "for loop".
    Only one for loop to be used is the constraint.
    Write some code that you think solves the problem and post it.

    Comment

    • Manjiri
      New Member
      • Nov 2006
      • 40

      #3
      Originally posted by sharathkumar
      Try to find out the duplicate element while inserting an integer element into an array of size 100. The inter numbers should 1 to 99. If i start inserting the number from 1 to 99 randomly without repeating the previous inserted element into array. At the instance if the number repeats we have find the index of the array and we have to find out the duplicated number and return.

      Example:
      array[0]=3;
      array[1]=7;
      array[2]=88;
      array[3]=45;
      array[4]=33;
      array[5]=99;
      array[6]=7;
      At this stage the C program should exit and display the information as duplicated element is 7 and index = 6;
      Write a C Program using only one "for loop".
      Only one for loop to be used is the constraint.


      Here i have the solution for it.... I have used for loop only once.. But i dont know this code will satisfy you or not... But you are going to get your required output...


      Code:
      #include<stdio.h>
      int main()
      {
      int array[100],i,x,c;
      printf("Enter the elements\n");
      for(i=1; i<=100; i++)
      {
      c=1;
      printf("\narray[%d]=",i);
      scanf("%d",&x);
      while(1)
      {
      if(array[c]==x)
      {
      printf("Duplicated element is %d  and index = %d\n",x,c);
      exit(0);
      }
      if(c<i)
      c++; 
      else
      break;
      }
      array[i]=x;
      }
      return 0;
      }

      You wanted this only..?
      And 1 more thing you have mentioned as 1 to 99... and array size as 100...
      if so then i think you are somewhere confused with the thing what you require..
      See 1 to 99 means you need a array of size just 99 not 100..
      am i right..?
      And plz tell me is my code is exactly the same what you wanted...?
      Thank you...

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        Why don't you consider it the other way round? set up an int array of 100, and as a number is entered, check the array is empty, if it is put the index of the number you are upto, otherwise print the inputed(?input) number and the index stored in the array. (eg if they enter 5,8,6,99,86 then a[5]=1, a[8]=2, a[6]=3, a[99]=4 etc)

        You can only use 1 for loop (I think you said), so use that to set every value in the array to 0 (or -1 or 101 or something that can't possibly be an input index. Then set a counter for each input and with each input test:

        Code:
        while(counter < 100)
        {
          if(a[input_variable]=default_variable)
          { 
            a[input_variable]=counter;
          }
          else
          {
            printf("The value %d, entered is the same as the %d th value entered", input_variable, a[input_variable]):
          }
        counter++;
        }
        This should work if i have understood/remembered the constraints on the program, that is, you will never have more than 100 elements (all unique except the exit case) and the elements are constrained to be less than 100 and greater than 1 (so if you wanted to <and let me say don't bother> you could set an array of 99 and store in a[n-1], which would save you an int (16 bits I think, which let's face it is nothing, given that it's a one-off) but set you up for potential logic errors when you forget the index is n-1)

        Hope it helps!

        Comment

        Working...