Thread: c programming search a structure array

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    4

    c programming search a structure array

    I have to search the structure by letter grade but am having trouble figuring out how to search.

    Here is my code. everything should be working. I only need to search

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>
        
    typedef struct {
      char name[26];
      char id[9];
      int project[2];
      int exam[2];
      char grade;
      int average;
      int idNum;
    } STUDENT;
                    
    void printUnsorted(int size, STUDENT stuAry[]);
    void sortAry(int size, STUDENT stuAry[]); 
                    
                    
    int main(void)  
    {
      STUDENT stuAry[51];
       
      FILE* f = fopen("records.ssv", "r");
      if (f== NULL) {
        printf("Error opening file %s.\n", "records.ssv");
        return 1;
      }
    
      char line[100];
      int ind = 0;
      while (fgets(line, sizeof(line), f) != NULL) {
        sscanf(line, "%25[^;]; %[^;]; %d; %d; %d; %d; %c ",
                    &stuAry[ind].name,
                    &stuAry[ind].id,
                    &stuAry[ind].exam[0],
                    &stuAry[ind].exam[1],
                    &stuAry[ind].project[0],  
                    &stuAry[ind].project[1],
                    &stuAry[ind].grade);
                    
         ind++;
      }
       
      for (int i=0; i<ind; i++) {
                    stuAry[i].average = (stuAry[i].exam[0] + stuAry[i].exam[1] + stuAry[i].project[0] +
                    stuAry[i].project[1]) / 4;
      }
    
      printUnsorted(ind, stuAry);
      sortAry(ind, stuAry);
      
      char gradeChoice;
      printf("Enter grade letter to search (q to quit):");
      scanf("%c", &gradeChoice);
      while(gradeChoice != 'q'){
            if(gradeChoice != 'A' || gradeChoice != 'B' || gradeChoice != 'C' || gradeChoice != 'D' || gradeChoice != 'F'){
                    printf("ERROR and enter again (A, B, C, D, or F (q to quit)):");
                    scanf("%c", &gradeChoice);
                        break;
            }else{
                    //Binary Search
            }
      }
               
      if (fclose(f) == EOF) {
        printf("Error closing file %s.\n", "records.ssv");
        return 1;
      }
    
    
    
    return 0;
    }
      
    void printUnsorted(int size, STUDENT stuAry[])
    {
      printf("\nOriginal: \n              Student Name    Identification\t  Exam 1   Exam 2   Project 1   Project 2   Average   Grade\n");
      printf("=============================================================================================================\n");
      for (int i=0; i<size; i++) {
        printf("%26s\t   %s\t  %d\t   %d\t    %d\t        %d\t    %d\t      %c\n",
               stuAry[i].name,
               stuAry[i].id,
               stuAry[i].exam[0],
               stuAry[i].exam[1],
               stuAry[i].project[0],
               stuAry[i].project[1],
               stuAry[i].average,
               stuAry[i].grade);
      }
    } 
      
    void sortAry(int size, STUDENT stuAry[])
    {
            for (int i=0; i<size; i++) {
                    stuAry[i].average = (stuAry[i].exam[0] + stuAry[i].exam[1] + stuAry[i].project[0] +
                                    stuAry[i].project[1]) / 4;
            }
      
            int largest, largestExam1, largestExam2, largestProject1, largestProject2;
            char largestName[26]; 
            char largestId[9];
            char largestGrade;
               
            for(int current = 0; current < size; ++current){
                    for(int walk = current + 1; walk < size; ++walk){
                            if(stuAry[current].average < stuAry[walk].average){
                                    //store in largest
                                    largest = stuAry[current].average;
                                    largestExam1 = stuAry[current].exam[0];
                                    largestExam2 = stuAry[current].exam[1];
                                    largestProject1 = stuAry[current].project[0];
                                    largestProject2 = stuAry[current].project[1];
                                    strcpy(largestName, stuAry[current].name);
                                    strcpy(largestId, stuAry[current].id);
                                    largestGrade = stuAry[current].grade;
                    
                                    //change order
                                    stuAry[current].average = stuAry[walk].average;
                                    stuAry[current].exam[0] = stuAry[walk].exam[0];
                                    stuAry[current].exam[1] = stuAry[walk].exam[1];   
                                    stuAry[current].project[0] = stuAry[walk].project[0];
                                    stuAry[current].project[1] = stuAry[walk].project[1];
                                    strcpy(stuAry[current].name, stuAry[walk].name);
                                    strcpy(stuAry[current].id, stuAry[walk].id);
                                    stuAry[current].grade = stuAry[walk].grade;
                    
                                    //put into other spot
                                    stuAry[walk].average = largest;
                                    stuAry[walk].exam[0] = largestExam1;
                                    stuAry[walk].exam[1] = largestExam2;   
                                    stuAry[walk].project[0] = largestProject1;
                                    stuAry[walk].project[1] = largestProject2;   
                                    strcpy(stuAry[walk].name, largestName);
                                    strcpy(stuAry[walk].id, largestId);
                                    stuAry[walk].grade = largestGrade;
                            }
                    }
            }
            printf("\nSorted:\n");
            printf("Index\t            Student Name     Identification\t Exam 1   Exam 2   Project 1   Project 2   Average   Grade\n");
            printf("==================================================================================================================\n");
            for(int i = 0; i < size; i++){
                    stuAry[i].idNum = i+1;
                    printf("%5d %26s\t  %s\t %d\t  %d\t   %d\t       %d\t   %d\t     %c\n",
                            stuAry[i].idNum, stuAry[i].name, stuAry[i].id, stuAry[i].exam[0],
                            stuAry[i].exam[1], stuAry[i].project[0], stuAry[i].project[1],
                            stuAry[i].average, stuAry[i].grade);
            }
            return;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    In order to perform a binary search, you need to keep dividing the area you're looking in for the element in half, until you find it. Think of it this way:
    Someone has thought of a number from one to 100. Each time you make a guess, you'll either have found it or be informed you were higher or lower. With those in mind, find the number, whichever it is, with fewer than 8 guesses.

    For example, someone is thinking of 13( but you don't know it ). You would guess:
    1) 50 ( > 13 ), middle of [1, 100]
    2) 25 ( > 13 ), middle of [1, 49]
    3) 12 ( < 13 ), middle of [1, 24]
    4) 18 ( > 13 ), middle of [13, 24]
    5) 15 ( > 13 ), middle of [13, 18]
    6) 14 ( > 13 ), middle of [13, 15]
    7) 13 ( found it! ), middle of [13, 14]
    For this example, I'm always truncating the decimal part when finding the middle value.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    4
    Not to be rude or anything, i'm in a bit of a time crunch and this needs to be done by midnight today and would really appreciate it if someone could type the code up for me.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Nope, nobody's going to write your homework for you. We are here to help you learn how to solve problems, not solve them ourselves.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    On line 94, you cycle through the entire array.
    On line 56, you check a "grade" variable.

    If you really wrote this code, those are the two concepts you need to implement the search you're after. You've already done those things, so adding the search should be quite easy.

    And as GReaper said, we don't do code hand-outs - see the homework policy.

  6. #6
    Registered User
    Join Date
    Dec 2016
    Posts
    4
    Ok so I added this to main

    Code:
    binSearch(ind, gradeChoice, stuAry);
                    printf("Enter grade letter to search (q to quit):");
                    scanf("%c", &gradeChoice);
    and also added

    Code:
    void binSearch(int size, char grade, STUDENT stuAry[]){
            printf("Index\t Student Name\t Identification\t Exam 1\t Exam 2\t Project 1\t Project 2\t Average\t Grade\n");
            for(int i = 0; i < size; i++){
                    if(stuAry[i].grade == grade){
                            printf("%5d %26s\t %s\t %d\t %d\t %d\t %d\t %d\t, %c\n",
                            stuAry[i].idNum, stuAry[i].name, stuAry[i].id, stuAry[i].exam[0],
                            stuAry[i].exam[1], stuAry[i].project[0], stuAry[i].project[1],
                            stuAry[i].average, stuAry[i].grade);
                    }
            }
            printf("==================================================================================================================\n");
    }
    but when i search for the grade it saying "error and enter again".

  7. #7
    Registered User
    Join Date
    Dec 2016
    Posts
    4
    So I re worked the code like so

    Code:
    char gradeChoice;
      printf("Enter grade letter to search (q to quit):");
      scanf("%c", &gradeChoice);
      while(gradeChoice != 'q'){    
            if(gradeChoice != 'A' || gradeChoice != 'B' || gradeChoice != 'C' || gradeChoice != 'D' || gradeChoice != 'F'){
                    printf("ERROR and enter again (A, B, C, D, or F (q to quit)):");
                    scanf("%c", &gradeChoice);
            }       
    
    
                    //Binary Search
            if(binSearch(int size, char grade, STUDENT stuAry[]) == true){
                            printf("%5d %26s\t %s\t %d\t %d\t %d\t %d\t %d\t, %c\n",
                            stuAry[i].idNum, stuAry[i].name, stuAry[i].id, stuAry[i].exam[0],
                            stuAry[i].exam[1], stuAry[i].project[0], stuAry[i].project[1],
                            stuAry[i].average, stuAry[i].grade);
               
            }
      }
    and redid the binSearch as such

    Code:
    bool binSearch(int size, char grade, STUDENT stuAry[], int* locn)
    {
        int first = 0;
            int last;
            int mid;
            bool found = false;
            while (first <= last) {
                    mid = (first + last) / 2;
                    if (grade > stuAry[mid].grade) {
                            first = mid + 1;
                    } else if (grade < stuAry[mid].grade) {
                            last = mid - 1;
                    } else {
                            first = last + 1;
                            found = true;
                            break;
                    }
            }
            *locn = mid;
            return found == stuAry[mid].grade;
    }
    however i get these errors:
    error: expected expression before âintâ
    error: too few arguments to function âbinSearchâ

    the errors have marked the line that says
    Code:
    if(binSearch(int size, char grade, STUDENT stuAry[]) == true){
    what am i doing wrong?

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    > Expected expression
    Generally means something is missing before the line that follows it (usually a syntax error).

    >To few arguments
    Check the amount of parameters the function has requested (prototype), then check the actual arguments that were presented when you invoked the function.
    Double Helix STL

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your sort function is unnecessarily long, if you know that structs can be assigned.

    Code:
    STUDENT temp = stuAry[current];
    stuAry[current] = stuAry[walk];
    stuAry[walk] = temp;
    Your binary search needs to return an index into the array, which would be the index of the found record.
    Return -1 if no record can be found.
    Code:
    int found = binSearch(size, grade, stuAry );
    if ( found != -1 ) {
      // do stuff with stuAry[found]
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Search and edit structure in binary file
    By ghost00359 in forum C Programming
    Replies: 5
    Last Post: 05-28-2016, 07:59 AM
  2. Replies: 4
    Last Post: 05-21-2016, 07:34 PM
  3. Structure Programming ---- Please Help
    By justaregularmic in forum C Programming
    Replies: 3
    Last Post: 08-14-2008, 10:44 AM
  4. Replies: 6
    Last Post: 06-09-2006, 12:44 AM
  5. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM

Tags for this Thread