Advertisement
Guest User

simpleGradebook.c

a guest
Oct 24th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5.  
  6. /* define a more readable value for how many grades we want */
  7. #define NUM_GRADES 10
  8.  
  9. /* weird way of making swap in C */
  10. #define swap(type, i, j) {type t = i; i = j; j = t;}
  11.  
  12. int main(int argc, char** argv){
  13.     int i,j,ret;
  14.     size_t read_b,size; // C is weird, ignore
  15.     int grades[10];
  16.     char buf[10]; // ignore
  17.    
  18.     /* this is a String in C, logistics arent really relevant */
  19.     char *s_name;
  20.     s_name = (char*) malloc( sizeof(char) * 100 );
  21.  
  22.     fprintf(stdout, "Please enter the student's name.\n"); // print message to user
  23.     /* read entire line from terminal and store it somewhere */
  24.     read_b = getline(&s_name, &size, stdin);
  25.    
  26.     fprintf(stdout, "Please enter 10 grades.\n"); // print message to user
  27.     /* read 10 integers from user input to terminal */
  28.     for( i = 0; i < NUM_GRADES; ++i ) grades[i] = 0;
  29.     for( i = 0; i < NUM_GRADES; ++i ){
  30.         /*
  31.          * reads an input in terminal
  32.          * then converts it to an integer and saves it
  33.          * NOTE: This is a poor example of how to do this right
  34.          *      it's just simple/fast.
  35.          */
  36.         ret = scanf( "%s", buf );
  37.         grades[i] = atoi( buf );
  38.         if( grades[i] < 0 || grades[i] > 100 ) exit(1); // error
  39.         if( ret == 0 || ret == EOF ) exit(1); // error
  40.     } // end for
  41.    
  42.     for( i = 2; i < NUM_GRADES; ++i ){
  43.     //  sum += grades[i];
  44.         printf("Grades[%d] = %d\n", i, grades[i]);
  45.     }
  46.    
  47.     /* bubble sort */
  48.     int flag;
  49.     for( ;; ){ // infinite loop
  50.         flag = 0;
  51.         for( j = 1; j < NUM_GRADES; ++j ){
  52.             /* "bubbleup" */
  53.             if( grades[j-1] > grades[j] ){
  54.                 swap( int, grades[j-1], grades[j] );
  55.                 flag = 1;
  56.             } // end if
  57.         } // end j for
  58.         if( flag == 0 ) break; // if flag is false eg 0 stop looping
  59.     } // end i for
  60.    
  61.     int sum;
  62.     sum = 0;
  63.     /*
  64.      * calculate the sum
  65.      * start at index 2 because the array is in
  66.      * increasing order thus grades[0] and grades[1]
  67.      * are the lowest and the dropped grades
  68.      */
  69.     for( i = 2; i < NUM_GRADES; ++i ){
  70.         sum += grades[i];
  71.         printf("Grades[%d] = %d\n", i, grades[i]);
  72.     }
  73.    
  74.     /*
  75.      * output the name, sum and average of scores
  76.      *
  77.      * %s tells the function to print that string (s_name)
  78.      * %d tells the function to print that integer (sum)   
  79.      */
  80.     fprintf(stdout, "Student name: %s\nTotal of top 8 scores: %d\nAverage: %d\n", s_name, sum, sum/8);
  81.    
  82.     /* C stuff */
  83.     exit(1);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement