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

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Simple Adjustment


    /**
    * Searches List[] for the next occurrence of Target.
    * Pre:
    * Target is set to value to be found
    * List[] holds at least lSize values
    * Post:
    * List[] is unchanged
    * Returns:
    * On i-th call with same parameters, returns
    * the index of the i-th occurrence of Target
    * within List[]; returns out-of-bounds index
    * if there is no such occurrence.
    */
    int Find(int Target, int List[], int lSize) {

    int idx = 0;

    while ( idx < lSize && Target != List[idx] ) {
    idx++;
    }
    return idx++;
    }

    So, if called as shown below:

    #define LSIZE 7

    int main() {

    int List[7] = {3, 5, 7, 11, 13, 5, 3};

    int Target = 5;
    int location;
    while ( ( location = Find(Target, List, LSIZE) ) < LSIZE ) {
    printf("%d was found at %d\n", Target, location);
    }

    return 0;
    }

    We would want to see the following output:

    5 was found at 1
    5 was found at 5

    Alas, what we see is actually this:

    5 was found at 1
    5 was found at 1

    Explain how the function can be modified so that it will perform as desired. You are not allowed to make any changes
    to the function interface (return value or parameter list), nor are you allowed to rely on any variables declared outside
    the body of the function.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    Also, in the future, please a) indent your code better, and b) use the [CODE] or [HIGHLIGHT] tags to retain formatting on the forum.

    C Code:
    /**
     * Searches List[] for the next occurrence of Target.
     * Pre:
     * Target is set to value to be found
     * List[] holds at least lSize values
     * Post:
     * List[] is unchanged
     * Returns:
     * On i-th call with same parameters, returns
     * the index of the i-th occurrence of Target
     * within List[]; returns out-of-bounds index
     * if there is no such occurrence.
     */
    int Find(int Target, int List[], int lSize) {
     
        int idx = 0;
     
        while ( idx < lSize && Target != List[idx] ) {
            idx++;
        }
        return idx++;
    }


    C Code:
    #define LSIZE 7
     
    int main() {
     
        int List[7] = {3, 5, 7, 11, 13, 5, 3};
     
        int Target = 5;
        int location;
        while ( ( location = Find(Target, List, LSIZE) ) < LSIZE ) {
            printf("%d was found at %d\n", Target, location);
        }
     
        return 0;
    }
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo