How do I print the contents of a struct in C? -


resolved issues on own, using different methods suggested below! :)

thanks viewing question! :)

i've been learning structs , working on practice lab in c, , code not seem compiling correctly change make it. currently not receiving output , program crashes. i'm still confused on how correctly utilize '*' , '&' symbols when passing them functions well. goals practice to:

  • print contents of array in same format data file
  • print full name of student best gpa
  • calculate , print average gpa
  • print names of students gpas above average
  • print name of youngest student has gpa below average
  • sort structures in array order lowest highest gpa
  • print array again (will in different order last time)

how call , print these items student struct? , how access gpa values pass function calculate average?

#include <stdio.h> #include <stdlib.h>  // define constants #define arr 100 #define first 7 #define midinit 1 #define last 9 #define street 16 #define city 11 #define state 2 #define zip 5 #define age 3 #define gpa 4 #define start 0 #define firstid 8 #define initid 10 #define streetid 20 #define cityid 37 #define stateid 49 #define zipid 52 #define ageid 57 #define gpaid 64  // defined structs  typedef struct {     char street[street + 1];     char city[city + 1];     char state[state + 1];     char zip[zip + 1];   } address;  typedef struct {     char firstname[first + 1];     char initial[midinit + 1];     char lastname[last + 1];     address ofstudent;     int age;     double gpa; } student;  // function prototype void strsub(char buf[], char s[], int start, int size);  void processstudent(int *id, student students[]); void sortstudentgpa(student *students, int id);  void maxgpa(student *students, int id);  /* lab6student.c: creates array of student structures , outputs reports */ int main(void) {     student students[arr]; // creates array of student structures     int id = 0; // counter student      processstudent(&id, students);     maxgpa(students, id); } void strsub(char buf[], char s[], int start, int size) {     int i;      (i = 0; < size && buf[start + i] != '\0'; i++) {          // loops long iterator less size          // , while string has not run out of characters         s[i] = buf[i + start];     }     s[i] = '\0'; } /* void sort(student *students, int id) {     int j, i;      for(i = 1; < n; i++) {         for(j = 0; j < id - i; j++) {             if(students[j].gpa > students[j + 1].gpa) {                 student temp = students[j];                 students[j] = students[j + 1];                 students[j + 1] = temp;             }         }     } } */ void processstudent(int *id, student students[]) {     file *data;     char line[arr];     *id = 0; // counter student      data = fopen("students.dat", "r");     if (data == null) {         printf("students.dat file not found!\n");         exit(1);     }      // process file     while (!feof(data)) {         // organize student info separate arrays         fgets(line, arr, data);         strsub(line, students[*id].firstname, start, first);          strsub(line, students[*id].initial, firstid, midinit);         strsub(line, students[*id].lastname, initid, last);         strsub(line, students[*id].ofstudent.street, streetid, street);         strsub(line, students[*id].ofstudent.city, cityid, city);         strsub(line, students[*id].ofstudent.state, stateid, state);         strsub(line, students[*id].ofstudent.zip, zipid, zip);         students[*id].age = atoi(&line[ageid]);         students[*id].gpa = atoi(&line[gpaid]);         (*id)++;     }         fclose(data); } //sorts struct student array containing num (gpa) elements //ascending order void sortstudentgpa(student *students, int id) {     int i, j; // indexes unsorted , sorted partitions     student temp; // temporarily holds element array      (i = 1; < id; ++i) {         temp = students[i];         j = - 1;         while (j >= 0 && temp.gpa < students[j].gpa) {             students[j + 1] = students[j];             j = j - 1;         }         students[j + 1] = temp;     } }  void maxgpa(student *students, int id) {     int iwithmax, i;     float max = 0;      ( = 0 ; < id ; i++) {         if (students -> gpa > max) {             max = students -> gpa;             iwithmax = i;         }     }      printf("\n\nhighest gpa done student %d gpa = %f", iwithmax, max); } 

in maxgpa function change definition as

        void maxgpa(student *students, int id); 

then in maxgpa function following changes

   void maxgpa(student *students, int id) {     int iwithmax, i;     float max = 0;      ( = 0 ; < id ; i++) {             if (students -> gpa > max) {                     max = students -> gpa;                     iwithmax = i;             }     } 

try this.....


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -