Thread: Array Problem!

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    11
    Rep Power
    0

    Array Problem!


    Hi

    How would you be able to determine whether a certain index in an array contains a value?

    Thanks
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Every single memory location contains a value. So then you can tell without even looking that a certain index in an array contains a value.

    Now, if your program had not placed a value there, then the value it contains is whatever was last written to that memory location by whatever process had last had possession of that memory location; AKA "garbage".

    So what are you really trying to ask here? Please be specific this time.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    11
    Rep Power
    0
    The program that I need to code involves a two dimensional array , I just want to know how to test whether there is an integer entered into one of the index positions in the array ,so that I can tell the program not to overwrite what is currently inside that index and instead right the next integer into the next available index according to an "if" statement that I will use. :cool:
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Whether the data is int, float, or pointer depends on how you (that is, c in this forum) views it. If you started with an array of zeros and you want to know if the value is still zero you could use

    if (a[i][j]) printf("ok to modify a[%d][%d]\n",i,j);
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    A number of methods are possible.

    A generalization on b49P23TIvg's method would be to initialize the array by filling it with an invalid value. Could be zero if all valid values would be greater than zero. -1 is a popular choice if a valid value is greater than or equal to 0. Or INT_MAX or INT_MIN if valid values would be reasonable positive and negative values. In an array of pointers, NULL is a good default invalid value. What you choose depends on your requirements.

    If you start filling the array from the beginning and insert each new value in the next available slot, then simply keep a count of how many values you've already input. That count would then be the index for the next value to be stored and then you increment the count. You'd have to think of how to apply that to a 2-D array.

    Offered just for completeness: If it's an array of structs, then you could add a field in the struct that would flag whether this struct has already been written to.

    A variation of the last two just occurred to me. Have two arrays, one that contains the values and the other that flags whether the indexed element in the values array is free or has been written to: a flag of one means it's been written to, a flag of zero means it's free to be written to. For example, when you write to values[2][3], you also write a 1 to flags[2][3]. Then to write to an element:
    Code:
        if (!flags[i][j])
            values[i][j] = new_value;
    I'm sure we could come up with more ideas.

    PS
    Another thought, a variation on that last one. Add a dimension to the array that would hold the flags; eg:
    Code:
        //  Plane 0 of the array contains the data values,
        // Plane 1 of the array contains the flags
        if (!array[i][j][1])
            array[i][j][0] = new_value;
    Last edited by dwise1_aol; April 4th, 2013 at 06:50 PM.

IMN logo majestic logo threadwatch logo seochat tools logo