Thread: Working with arrays of structures, need advice...

  1. #1
    Unregistered
    Guest

    Working with arrays of structures, need advice...

    Brand new C programmer here.

    I'm working on a program, and am about 75% done...stumped on 3 things though.

    1. One of the fields in my structure is student GPA. I need to calculate all of the GPA's to return a class average. What would be the best way to determine the number of students, if the number to be divided by is not known when the program is started (works off user input, and there is not a predetermined number of inputs...could be 5, could be 50).

    2. What is the best way to remove a record from memory, based on "Last Name" as the key field.

    3. One of the inputs I'm receiving from the user is "Number of children". I've got a get_student_info function that I'm using to get input from the user...I'm not exactly sure of the syntax I would need to set up an "if" structure to check the number of children field to also prompt the user for child name & age if they respond 1 or more to the number.

    Any help would be appreciated.

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Working with arrays of structures, need advice...

    Originally posted by Unregistered
    Brand new C programmer here.

    I'm working on a program, and am about 75% done...stumped on 3 things though.

    1. One of the fields in my structure is student GPA. I need to calculate all of the GPA's to return a class average. What would be the best way to determine the number of students, if the number to be divided by is not known when the program is started (works off user input, and there is not a predetermined number of inputs...could be 5, could be 50).

    2. What is the best way to remove a record from memory, based on "Last Name" as the key field.

    3. One of the inputs I'm receiving from the user is "Number of children". I've got a get_student_info function that I'm using to get input from the user...I'm not exactly sure of the syntax I would need to set up an "if" structure to check the number of children field to also prompt the user for child name & age if they respond 1 or more to the number.

    Any help would be appreciated.
    1. int numberofstudents = 0;
    // after each successful input
    numberofstudents++;
    2. post some code
    3. if (numberofkids > 0)
    { // take more input
    }

    i'm sorry i can't be more specific, try posting some code
    hello, internet!

  3. #3
    Unregistered
    Guest

    Re: Working with arrays of structures, need advice...

    Originally posted by moi


    1. int numberofstudents = 0;
    // after each successful input
    numberofstudents++;
    2. post some code
    3. if (numberofkids > 0)
    { // take more input
    }

    i'm sorry i can't be more specific, try posting some code
    1. Can I declare numberofstudents globally, increment the counter each time through my while loop (in main), and use that variable in my calc_avg_gpa function?

    2. Don't have any code for #2 yet. This is a requirement that I'm not sure where to start with what I have so far...I've found reference to the "free" function, but I'm not using pointers on this particular project (just arrays of a structure).

    3. I tried something comparable to that, but got errors at compile time. "numberofkids" is a field within my structure, so when I use student.numberofkids in my comparison the compiler doesn't like it...something like "comparison cannot be made using type structure" is the error that was returned.

    Apologize for not providing code...it's on my PC at home, and I'm not there right now.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What would be the best way to determine the number of students
    One way would be to ask the user to input how many records will be processed on that particular run of the program, if this isn't possible you may have to wait until all records are input and then count them before processing.

    >What is the best way to remove a record from memory, based on "Last Name" as the key field.
    The best way is to use a linked data structure that is easy to delete from. Arrays are difficult and inefficient when it comes to deletion because you have to shift a lot of data around in memory to remove just one record. It sounds to me like a linked list is what you really need.

    >I'm not exactly sure of the syntax I would need to set up an "if"
    >structure to check the number of children field to also prompt
    >the user for child name & age if they respond 1 or more to the number
    Code:
    /*
    ** No need to test num_of_children, the loop will
    ** do that for you. :)
    */
    for ( i = 0; i < student.num_of_children; i++ ) {
      /*
      ** Get the name and age of child[i] until
      ** all children have been processed.
      */
    }
    >Can I declare numberofstudents globally, increment the counter each time through my while loop
    You can, but if you design your programs to avoid global variables when possible you'll be better off.

    >This is a requirement that I'm not sure where to start with what I have so far...
    You could also mark the record as removed and just ignore it.

    >I tried something comparable to that, but got errors at compile time.
    We'd have to see code for this one.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays of Structures called in a function
    By Fadibasma in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 09:30 PM
  2. Evaluation of structure's element against input not working!
    By laczfinador in forum C Programming
    Replies: 6
    Last Post: 05-11-2009, 01:19 PM
  3. more help...2d arrays in structures - sscanf
    By mapunk in forum C Programming
    Replies: 6
    Last Post: 12-01-2005, 07:00 PM
  4. outputting a report arrays of structures
    By elusive in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 11:15 PM
  5. Replies: 7
    Last Post: 12-29-2001, 11:25 PM