dhtmlextreme

C Example

Dec 12th, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define NUM_STUDENTS 25
  4. #define NUM_TESTS 10
  5.  
  6. int get_highest(int a[][NUM_TESTS], int row, int col);
  7. int *p;
  8.  
  9.  
  10.  
  11.  union {
  12.               int u_i;
  13.       }var;
  14.  
  15. /* Define a type point to be a struct with integer members hi, fa */
  16. typedef struct {
  17.    int    hi;
  18.    int    fa;
  19. } score;
  20.  
  21.  
  22. int factorial(int f);
  23.  
  24.  
  25. typedef enum
  26. {  Poor,
  27.    Average,
  28.    Good
  29. } myenum;
  30.  
  31.  
  32. int main()
  33. {
  34.    char* test[]={"Poor","Average", "Good"};
  35.  
  36.    int grades[NUM_STUDENTS][NUM_TESTS] = { {5, 6, 4},
  37.                                            {8, 9, 7} };
  38.    int num_students = 2;
  39.    int num_tests = 3;
  40.    int high_test;
  41.    int grade;
  42.    score student; //define a variable student of type score
  43.  
  44.    p = &var.u_i;         /* initialise pointer */
  45.  
  46.    high_test = get_highest( grades, num_students, num_tests);
  47.  
  48.    *p = factorial(high_test); //use the pointer to change the value of highest_test
  49.  
  50.    //store variables inside the structure
  51.    student.hi=high_test;
  52.    student.fa=var.u_i;
  53.  
  54.    myenum value=Average;
  55.    char tmp[0]={0x0};
  56.  
  57.    printf("The highest score is %d.\n", student.hi);
  58.    printf("This has a factorial of %d.\n",student.fa);
  59.  
  60.    if(student.hi>0 && student.hi<=4) {grade=0;}
  61.    else if(student.hi>4 && student.hi<=6) {grade=1;}
  62.    else {grade=2;}
  63.  
  64.    strcpy(tmp,test[grade]); //grade score of student here
  65.    printf("Final Grading is %s.\n",tmp);
  66.  
  67.    return 0;
  68. }
  69.  
  70. int get_highest(int a[][NUM_TESTS], int row, int col)
  71. /* Assumes that there is at least one element */
  72. {
  73.    int i, j;
  74.    int highest = a[0][0];
  75.  
  76.    for( i = 0; i < row; i++)
  77.       for( j = 0; j < col; j++)
  78.          if ( a[i][j] > highest)
  79.             highest = a[i][j];
  80.    return highest;
  81. }
  82.  
  83.  
  84.  
  85. int factorial(int number)
  86.     {
  87.         if (number < 0)
  88.         {
  89.             printf("\nError - negative argument to factorial\n");
  90.             exit(1);
  91.         }
  92.         else if (number == 0)
  93.             return 1;
  94.         else
  95.             return (number * factorial(number - 1));
  96.     }
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment