pastebin

Paste Search Dynamic
Recent pastes
Submission
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define maxL 1000
  5. typedef struct Submission
  6. {
  7.     int subID;
  8.     int studentID;
  9.     char outputs[10][maxL];    
  10. }Submission;
  11. typedef struct Grade {
  12.     int subID;
  13.     int studentID;
  14.     int grade;  
  15. } Grade;
  16. int cmp(const void *a, const void *b){
  17.     Grade A = *((Grade *)a), B = *((Grade *)b);
  18.     if(A.studentID > B.studentID)
  19.         return 1;
  20.     else
  21.         return -1;
  22.     return 0;
  23. }
  24. void grading(char answer[10][maxL], Submission* sub, int num_of_submission){
  25.     Grade G[num_of_submission];
  26.     for(int i = 0; i < num_of_submission; i++){
  27.         G[i].studentID = sub[i].studentID, G[i].subID = sub[i].subID, G[i].grade = 0;
  28.         for(int j = 0; j < 10; j++)
  29.             if(strcmp(answer[j], sub[i].outputs[j]) == 0)
  30.                 G[i].grade += 10;
  31.     }
  32.     qsort(G, num_of_submission, sizeof(Grade), cmp);
  33.     int max[1000] = {}, count = 0;
  34.     for(int i = 0; i < num_of_submission; i++){
  35.         printf("Student %d gets %d with Submission %d.n", G[i].studentID, G[i].grade, G[i].subID);
  36.         if(G[i].grade > max[count])
  37.             max[count] = G[i].grade;
  38.         if(i == num_of_submission-1 || G[i].studentID != G[i+1].studentID){
  39.             printf("Student %d gets %d in this exam.n", G[i].studentID, max[count]);
  40.             count++;
  41.         }
  42.     }
  43. }
  44. int main(){
  45.     char answer[10][maxL];
  46.     int  num_of_submission;
  47.     Submission* sub;
  48.  
  49.     for(int i=0;i<10;i++){
  50.         scanf("%s",answer[i]);
  51.     }
  52.  
  53.     scanf("%d",&num_of_submission);
  54.  
  55.     sub=(Submission*)malloc(num_of_submission*sizeof(Submission));
  56.     for(int i=0;i<num_of_submission;i++){
  57.         scanf("%d %d",&sub[i].subID,&sub[i].studentID);
  58.         for(int j=0;j<10;j++){
  59.             scanf("%s",sub[i].outputs[j]);
  60.         }
  61.     }
  62.     grading(answer,sub,num_of_submission);
  63. }
Parsed in 0.204 seconds