Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4.  
  5. /*
  6. * Returns the maximum value of the two integer value pointers given as parameter. One of the parameters may be NULL, but not both.
  7. *
  8. * Special cases:
  9. *
  10. * 1. If one of the values is NULL, return the other value.
  11. * 2. If the values are equal, return either one.
  12. *
  13. */
  14.  
  15. int max(int* param1, int* param2){
  16.  
  17. if (param1 == NULL || *param2 > *param1){
  18. return *param2;
  19. }
  20. if (param2 == NULL || *param2 <= *param1){
  21. return *param1;
  22. }
  23. else return *param1;
  24. }
  25.  
  26.  
  27.  
  28.  
  29. /*
  30. * Returns a pointer to the minimum value of the two integer value pointers given as parameter.
  31. * In this case both, or either of the parameters may be NULL.
  32. *
  33. * Special cases:
  34. *
  35. * 1. In case of one NULL value return the other value.
  36. * 2. If both of the values are NULL, return NULL.
  37. * 3. If the values are equal, return either one.
  38. *
  39. */
  40.  
  41.  
  42. int* min(int* param1, int* param2){
  43.  
  44. if (param1 == NULL || *param2 < *param1){
  45. return param2;
  46. }
  47. if (param2 == NULL || *param2 >= *param1){
  48. return param1;
  49. }
  50.  
  51. if (param1 == NULL && param2 == NULL){
  52. return NULL;
  53. }
  54. else return param1;
  55.  
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62. /*
  63. * Calculates the average of num_of_items values from standard input. The input values the function reads are of type double.
  64. * The program returns the calculated average as a pass-by-pointer, given to it as parameter named average.
  65. * You can assume that the average pointer's value has been initialized properly (e.g. to 0.0).
  66. *
  67. * Special cases:
  68. *
  69. * 1. If the value of num_of_items is zero or less, the function does nothing (no average calculations). In this case DO NOT TOUCH the average pointer in any way.
  70. * 2. If the num_of_items pointer is NULL, the function does nothing (no average calculations). In this case DO NOT TOUCH the average pointer in any way.
  71. * 3. If the average pointer given as parameter is NULL the program does nothing (no average calculations).
  72. * 4. In case of a failed read from standard input, the program calculates the average of the values until that point.
  73. *
  74. * Note:
  75. *
  76. * Make sure that your average function can never go to a state where it divides by zero.
  77. *
  78. */
  79.  
  80. void average(int* num_of_items, double* average){
  81.  
  82. if (*num_of_items <= 0){
  83. return;
  84. }
  85. if (num_of_items == NULL){
  86. return;
  87. }
  88. if (average == NULL){
  89. return;
  90. }
  91. int temp, sum;
  92. for (int i=0; i < *num_of_items; i++){
  93. scanf("%d", &temp);
  94. sum+=temp;
  95. *average = sum / *num_of_items;
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement