creating an array with different array value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sekundes
    New Member
    • Feb 2008
    • 3

    creating an array with different array value

    hi im new here and still a beginner in C. i need an answer to this problem and hopefully u can help me in this.

    we have this challenge asked by an instructor to create an array program that will input integers and if you input an integer it must have no same input value with other array index and will not accept it. therefore it is unique. example if i input 1 in array[0], array[0] will only have the integer 1 on it and no other.

    this is all what ive come up with

    [CODE=c]void unique (int la[], int n)
    {
    int i;
    for(i=0;i<n;i++ )
    {
    scanf("%d", &la[i]);
    }
    }[/CODE]
    cant seem to solve the unique part T_T. hope u can help me solve it ^_^
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    So at any point in time, if you are inputting number N to array element i, then array[i-1], array[i-2], array[i-3],...,array[1], and array[0] cannot hold the value N. If none of them held N, you're good to go. If you found N somewhere else, you need a new N value.

    Comment

    • sekundes
      New Member
      • Feb 2008
      • 3

      #3
      yup... it will promt the user to enter another value coz the N value has been entered. the thing is i dont know how to program this kind of problem T_T ..

      Hope u can help me

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Take a look at my post again - there's a pretty major hint on how to go about solving this problem. I'll try and repeat:

        Let's say you'll use a function, named isRepeat, that will see if a value passed to it is found in an array, also passed as an argument. If the value is found, it returns true (1) - if the value is not found, it will return false (0). So we have a function like this:

        [CODE=c]int isRepeat(int array[], int array_size, int value)[/CODE]

        Now, array_size doesn't have to be the actual maximum size of the array. It could be the number of elements read in. For instance, when you are reading in the first element (the array is empty), then when you call check, you might say:

        [CODE=c]isRepeat(array, 0, val);[/CODE]

        You know for certain that the value is not a repeat, because there are no other elements in the array that could possibly hold that value.

        Now, during any call of isRepeat, you know that you will return true (1) if you find val in any point in the array before array_size - that is, if array[0], array[1], array[2],..., array[array_size-2], or array[array_size-1] is value. If you finish checking all of these indexes and didn't find value, then there is no repeat, and you return false 0.

        This is way more than enough for you to write the code yourself. All you need to do is think of how you can write isRepeat - using the hints I've provided - and how you will use isRepeat when reading values into your array.

        Comment

        • sekundes
          New Member
          • Feb 2008
          • 3

          #5
          ok thx for the help....

          Comment

          Working...