By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Help me in developing my print function possible sort function

P: 17
I have been tasked with finishing the following C program. I need to develop the sort by birthday function and the print grade function. I have been working on the print function for the last week and it seems like everything I try doesn't work. I guess I am not understanding how to deal with structures. Please if someone could please help me out I would appreciate it.
Thank you.

Expand|Select|Wrap|Line Numbers
  1.  #include <stdio.h> 
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define CLASS_SIZE 2
  7. // maximum class size
  8.  
  9. struct test{
  10. //declare structures
  11.     float quiz1;
  12.     float quiz2;
  13.     float quiz3;
  14.     float average;
  15.     float GP;
  16. };
  17.  
  18. struct date{
  19.  
  20.     int month;
  21.     int day;
  22.     int year;
  23. };
  24.  
  25. struct student_data{
  26.  
  27.     char *last_name;
  28.     int student_id;
  29.     char grade;
  30. };
  31.  
  32.  
  33. void getGrade();
  34. // function prototypes
  35. void sort();
  36. void swapd (struct date *s1, struct date *s2);
  37. void swaps (struct student_data *s1, struct student_data *s2);
  38. void swapt (struct test *s1, struct test *s2);
  39. void prn_student_data();
  40.  
  41. struct student_data *students[CLASS_SIZE];
  42. // structure declarations
  43. struct date dob[CLASS_SIZE];
  44. struct test tests[CLASS_SIZE];
  45.  
  46.  
  47. int main(void)
  48. // begin execution
  49. {
  50.     char temp[30];
  51.     int i;
  52.     float total=0;
  53.  
  54.     printf("\n\n This program will compute, sort by birthday, and
  55. display a\n"    // user prompt
  56.         " students average letter grade, quiz average? and grade
  57. point obtained\n"
  58.         " from 3 quizzes.\n\n");
  59.  
  60.     for(i=0; i<(CLASS_SIZE); i++)
  61. //get student info
  62.     {
  63.         printf("\n\nEnter Student #%d's Last Name: ", i+1);
  64. // user prompt
  65.         scanf("%s", temp);
  66. // user input
  67.         students[i]=(struct student_data*)malloc(sizeof(struct
  68. student_data));    // allocate memory for structure
  69.         students[i]->last_name=(char*)malloc(strlen(temp));
  70. // allocate memory for pointer last name in structure
  71.         strcpy(students[i]->last_name, temp);
  72. // copy from temp to struct member -last name
  73.  
  74.         printf("Enter Students ID Number: ");
  75. //input student ID
  76.         scanf("%d", &students[i]->student_id);
  77.  
  78.         printf("\nEnter Students Birthday (mm/dd/yyyy): \n");
  79. //input birthday info
  80.         printf(" ** Dont forget the forward slashes **: ");
  81.         scanf("%d/%d/%d", &dob[i].month, &dob[i].day, &dob[i].year);
  82.  
  83.         printf("\n\nEnter quiz scores from 0 to 100\n");
  84.         printf("\nEnter score obtained for quiz #1: ");
  85. //input scores from quizzes
  86.         scanf("%f", &tests[i].quiz1);
  87.         printf("\nEnter score obtained for quiz #2: ");
  88.         scanf("%f", &tests[i].quiz2);
  89.         printf("\nEnter score obtained for quiz #3: ");
  90.         scanf("%f", &tests[i].quiz3);
  91.  
  92.         total = (tests[i].quiz1)+(tests[i].quiz2)+(tests[i].quiz3);
  93. //compute quiz total and average
  94.         tests[i].average = (total/3);
  95.         system("cls");
  96.     }
  97.  
  98.     getGrade();
  99. //call functions
  100.     sort();
  101.     prn_student_data();
  102.  
  103.     return 0;
  104. }
  105.  
  106.  
  107. void getGrade()
  108. //grade conversion function
  109. {
  110.     int i;
  111.  
  112.     for(i=0; i<(CLASS_SIZE); i++)
  113.  
  114.     {
  115.         if (tests[i].average >= 90.0)
  116.             students[i]->grade = 'A', tests[i].GP = 4;
  117.         else if (tests[i].average >= 80.0)
  118.             students[i]->grade = 'B', tests[i].GP = 3;
  119.         else if (tests[i].average >= 70.0)
  120.             students[i]->grade = 'C', tests[i].GP = 2;
  121.         else if (tests[i].average >= 60.0)
  122.             students[i]->grade = 'D', tests[i].GP = 1;
  123.         else
  124.             students[i]->grade = 'F', tests[i].GP = 0;
  125.     }
  126. }
  127.  
Sep 29 '06 #1
Share this Question
Share on Google+
3 Replies


100+
P: 293
D_C
You do not pass any data to the print student function. Since the array that holds all student data is not static, your function has absolutely no idea of knowing where the data is to print. Either you could make the array of student structs static, pass the array (or each entry) by address (and as const too), or you could change the student struct to a class and make a print() method.

Also, in order sort by year, then month, then day, first sort by day, then by month, then by year. Make sure to use a stable sort.
Sep 30 '06 #2

P: 17
You do not pass any data to the print student function. Since the array that holds all student data is not static, your function has absolutely no idea of knowing where the data is to print. Either you could make the array of student structs static, pass the array (or each entry) by address (and as const too), or you could change the student struct to a class and make a print() method.

Also, in order sort by year, then month, then day, first sort by day, then by month, then by year. Make sure to use a stable sort.
Do you suggest that I use typedef to do this? I have been reviewing my notes and text book, but seems like I should really get involed with this program.
Thank you and take care.
Oct 6 '06 #3

100+
P: 293
D_C
Look at things in terms of priorities. Using typedef *should* simplify the declaration of data/structs. However, it is not necessary. All the data in your array of student structs is held somewhere in memory. You have to deliver your data to your print function somehow. That is absolutely necessary. There are several things you could do:
  • declare the array as static
  • pass by value each Student struct
  • pass by address each Student struct
  • pass by address the array of Student structs
  • change Student from a struct to a class, and implement a print function
Oct 6 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.