1-D array question

This is a discussion on 1-D array question within the C Programming forums, part of the General Programming Boards category; hey guys , i was wondering if u could tell me a few ways to find if a certain element(no,char,or ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    90

    1-D array question

    hey guys , i was wondering if u could tell me a few ways to find if a certain element(no,char,or whatever) exists in an array or not.
    ex: i enter 10 elements in an array through a for loop then how do i check if an element is found in the array or not ?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you know what you are looking for before entering the elements in the array you don't even need to do another iteration. Just test if the element you are inserting is the one you are looking for and then set a flag variable.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    any other ways than using a flag variable ?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    There is a lot of research done on searching algorithms (Search algorithm - Wikipedia, the free encyclopedia), but many of them require your data to be sorted to work efficiently (Sorting algorithm - Wikipedia, the free encyclopedia). I strongly suggest that you peruse the above articles just for your edification, but given that you seem to have a small array, I would just do a simple linear search.

    Simply iterate through your array, and compare each element against your search term. A common return value for an array search function is the index of the item if found, or -1 if not found. Here is some pseudo-code:
    Code:
    // look for item in array, returning the index if found, or -1
    int search(item, array, array_length)
        for i from 0 to array_length {
            if array[i] equals item
                return i
        }
        return -1
    Last edited by anduril462; 11-15-2010 at 11:25 AM.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    mm thanks for the help

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by everyone0 View Post
    any other ways than using a flag variable ?
    Well, unless there is some pattern to the data that you know of in advance searching through an unsorted array is pretty much going to be O(n).

    If the data was sorted, you could use a divide and conquer algorithm for less comparisons.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Question
    By Alecroy in forum C++ Programming
    Replies: 4
    Last Post: 09-26-2010, 03:22 PM
  2. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  3. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21