Thread: Need help with Searching within Structs

  1. #1
    Registered User
    Join Date
    Nov 2022
    Posts
    6

    Question Need help with Searching within Structs

    Hello! I am looking for help with the below code. This is for a class and I would like help figuring out how to complete the missing code. I don't want someone to finish it for me because I won't learn anything. I missed 2 weeks of class due to a death in the family and family illnesses, so I really need to learn how to write this.

    I have to complete two remaining sections of the code below. I'm not sure what to write in the YOUR CODE GOES HERE parts. First part is a lookup function. The second is adding them to an array. I've tried googling, but just end up getting more and more confused.

    This is my first coding course, so any and all help is appreciated!

    Code:
    #include "stdio.h"
    #include "stdlib.h"
    
    typedef unsigned int uint;
    
    struct Student
    {
      // add the definition of the struct
      char name[30];
      uint id;
      char department;
      char course;
      uint admit_year;
    };
    
    /* This function accepts an array of struct Student structs and an integer
    cohort_year and returns an array of students with a admit_year that matches.*/
    void cohort_finder(struct Student *, uint, uint);
    
    void student_lookup(struct Student *, uint, uint);
    
    void print_student_record(struct Student);
    
    int main(int argc, char const *argv[]) {
    
      struct Student student_roster[100];
    
      struct Student s;
    
      s.name = "Test Student";
      s.department = "Engineering";
      s.course = "N/A";
      s.admit_year = 2009;
      s.id = 0001;
    
      student_roster[0] = s;
    
      printf("Performing Cohort Finder for Year: 2009\n");
      cohort_finder(student_roster,1, 2009);
    
      printf("Performing Student Lookup for id: 0001\n");
      student_lookup(student_roster, 1, 0001);
    
      return 0;
    }
    
    void student_lookup(struct Student students[], uint n, uint id)
    {
      struct Student matching_student;
    
      /* YOUR CODE GOES HERE!
        Complete the student ID lookup function such that it prints the matching
        student's record. The printing code already exists.
      */
    
    
      printf("Matching Student Is:\n");
      print_student_record(matching_student);
    }
    
    void cohort_finder(struct Student students[], uint n, uint cohort_year)
    {
    
     // create an oversized array on the heap to contain the matching students
      struct Student * cohort_students = (struct Student*)malloc(sizeof(struct Student)*n);
    
     // j is the counter for the matching students
      uint j = 0;
    
      /* YOUR CODE GOES HERE!
        Your code should find the matching students in the struct Student array and
        add them to a new array (cohort_students).
      */
      for (j = 0; j < 5; j++){ /*I did this part but got confused about what to put next. 
    If I'm not mistaken, this should advance j through the array, correct?
    Not sure how to add to the array here*/
    
        
      }
    
      // resize the heap allocation so the size can be determined using sizeof
      cohort_students = (struct Student*)realloc(cohort_students, j*sizeof(struct Student));
    
      // check if the pointer is null
      if(cohort_students == NULL)
      {
        printf("Something Went Wrong During Allocation of cohort_students\n");
      }
    
      printf("The Students from %d Are:\n", cohort_year);
      for(uint i=0; i<j; i++)
      {
        print_student_record(cohort_students[i]);
      }
    }
    
    void print_student_record(struct Student student)
    {
      printf("\n");
      printf("Student Name: %s\n", student.name);
      printf("Student department: %s\n", student.department);
      printf("Student course: %s\n", student.course);
      printf("Student admit year: %d\n", student.admit_year);
      printf("Student id: %d\n", student.id);
      printf("\n");
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void cohort_finder(struct Student *, uint, uint);
    You can have names along with the types in a prototype.
    So you have less to describe when describing the interface.
    Code:
    void cohort_finder(struct Student students[], uint n, uint cohort_year);
    > s.name = "Test Student";
    You can't assign an array, you have to use strcpy.

    > s.department = "Engineering";
    > s.course = "N/A";
    You can't assign a string to a single char.
    Were these fields meant to be arrays?

    > s.id = 0001;
    Be aware that numbers beginning with 0 are interpreted as octal (base 8) numbers.
    Fine for the moment, but not so good when you've counted your way up to 0008.

    > Complete the student ID lookup function such that it prints the matching
    for ( uint i = 0 ; i < n ; i++ ) // loop over all students
    if ( students[i].id == id // test for a match against a field within the struct
    I'm sure you can figure the rest out.


    > struct Student * cohort_students = (struct Student*)malloc(sizeof(struct Student)*n);
    You don't need the cast in a C program.
    struct Student * cohort_students = malloc(sizeof(struct Student)*n);
    If you get a message about not being able to convert void*, then you're compiling your C program with a C++ compiler. Use a C compiler.

    > Your code should find the matching students in the struct Student array and
    It's the same basic loop used above.

    The only other line of code you need is this, when you've found a cohort member.
    cohort_students[j] = students[i];
    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.

  3. #3
    Registered User
    Join Date
    Nov 2022
    Posts
    6
    Quote Originally Posted by Salem View Post
    > void cohort_finder(struct Student *, uint, uint);
    You can have names along with the types in a prototype.
    So you have less to describe when describing the interface.
    Code:
    void cohort_finder(struct Student students[], uint n, uint cohort_year);
    > s.name = "Test Student";
    You can't assign an array, you have to use strcpy.

    > s.department = "Engineering";
    > s.course = "N/A";
    You can't assign a string to a single char.
    Were these fields meant to be arrays?

    > s.id = 0001;
    Be aware that numbers beginning with 0 are interpreted as octal (base 8) numbers.
    Fine for the moment, but not so good when you've counted your way up to 0008.

    > Complete the student ID lookup function such that it prints the matching
    for ( uint i = 0 ; i < n ; i++ ) // loop over all students
    if ( students[i].id == id // test for a match against a field within the struct
    I'm sure you can figure the rest out.


    > struct Student * cohort_students = (struct Student*)malloc(sizeof(struct Student)*n);
    You don't need the cast in a C program.
    struct Student * cohort_students = malloc(sizeof(struct Student)*n);
    If you get a message about not being able to convert void*, then you're compiling your C program with a C++ compiler. Use a C compiler.

    > Your code should find the matching students in the struct Student array and
    It's the same basic loop used above.

    The only other line of code you need is this, when you've found a cohort member.
    cohort_students[j] = students[i];

    Thank you so much! It turns out that the code provided for us to complete had some errors, which you identified. So there was much more to this than just inserting missing code, we had to fix the original code! (correcting the code was not in the assignment instructions. lol.) I had to look up "strcpy" as we hadn't covered that yet in class, but wouldn't compile without it. So thank you for your help and explanations!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching array of structs within interval
    By knowing in forum C Programming
    Replies: 4
    Last Post: 10-06-2016, 06:51 AM
  2. Replies: 2
    Last Post: 01-08-2013, 07:55 AM
  3. Typedef Structs inside Typdef structs
    By gremory in forum C Programming
    Replies: 21
    Last Post: 12-30-2011, 07:48 PM
  4. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

Tags for this Thread