linear search for structure (record) array

This is a discussion on linear search for structure (record) array within the C Programming forums, part of the General Programming Boards category; ok guys, im looking to use a linear search to search a structure for a certain account number. the structure ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    Question linear search for structure (record) array

    ok guys, im looking to use a linear search to search a structure for a certain account number. the structure is as follows.

    Code:
    typedef struct { 
       int acctNum;
       char lastName[ 15 ];
       char firstName[ 10 ];
       double balance;
    } clientData;
    i have called the function containing the linear search however my actual search algorithm for this wrong. a linear search on a normal array is much easier and im finding troubles determining how to complete the search. this is what i have.

    calling funtion using a previously found search key.

    Code:
    element = linearSearch (customer, searchKey, SIZE)
    then this is my function for the linear search.

    [code]

    int linearSearch ( clientData client [], int Key, int arrsize){

    int n;

    for ( n = 0; n <= arrsize - 1; n++ )
    if (strcmp (client[n] .acctNum, Key) == 0)
    return n;

    return -1;
    }

    can anyone advise me of how to alter this linear search algorithm so that is searches for an account number that is requested?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    strcmp() is for comparing C-strings.
    acctNum and Key are int's, so simply use "==".

    gg

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    13
    ok well thats stopped the program crashing however now it just ends
    should i have acctNum. acctNum in there? or doesn't that make sense

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >for ( n = 0; n <= arrsize - 1; n++ )
    The customary idiom is
    Code:
    for ( n = 0; n < arrsize; n++ )
    >if (strcmp (client[n] .acctNum, Key) == 0)
    Perhaps
    Code:
    if (client[n] .acctNum == Key)
    >ok well thats stopped the program crashing however now it just ends
    Then show us your new code.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Linear Search: the recursive way.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 01-15-2002, 02:15 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