Newbie need help

This is a discussion on Newbie need help within the C Programming forums, part of the General Programming Boards category; Hi, I kinda confuse to understand the code for my lab assignment below: #define NOT_FOUND -1; #define MAX 100 struct ...

  1. #1
    Sunset
    Guest

    Newbie need help

    Hi,
    I kinda confuse to understand the code for my lab assignment
    below:
    #define NOT_FOUND -1;
    #define MAX 100

    struct student
    { int id;
    char name[20];
    float q1, q2, q3, exam;
    };
    struct student stuArr[MAX];

    int main()
    {
    /*call search function here*/

    }
    int search(int studentID)
    {
    int i;
    for (i=0, i <MAX; i++)
    if (stuArr[i].id == ID)
    return i;

    return NOT_FOUND;
    }
    ===============================
    Does the FOR loop in search() function will run from 0 to 99 regardless even a student ID has found at index, say, 45 or
    it returns immediately the index of array where a student ID match and terminate the search() function right away and
    go back to main() where it get called?

    Thank you in advance for help explaining.

    Sunset

  2. #2
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    The for loop iterates from 0 - 99 but if the item being searched for is found somewhere in there, the index is returned immediately and the loop stops. So if the item is at index 1 the loop won't go all the way to 99 before quitting. The only time that happens is if the item isn't there, then the function returns -1.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  3. #3
    Sunset
    Guest
    Than you very much for your explaining.
    So the RETURN in the For loop serves as a BREAK statement.
    Right?

    Sunset

  4. #4
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    The call to return exits the function completely, so it does break from the loop.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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