Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- /*
- * Returns the maximum value of the two integer value pointers given as parameter. One of the parameters may be NULL, but not both.
- *
- * Special cases:
- *
- * 1. If one of the values is NULL, return the other value.
- * 2. If the values are equal, return either one.
- *
- */
- int max(int* param1, int* param2){
- if (param1 == NULL || *param2 > *param1){
- return *param2;
- }
- if (param2 == NULL || *param2 <= *param1){
- return *param1;
- }
- else return *param1;
- }
- /*
- * Returns a pointer to the minimum value of the two integer value pointers given as parameter.
- * In this case both, or either of the parameters may be NULL.
- *
- * Special cases:
- *
- * 1. In case of one NULL value return the other value.
- * 2. If both of the values are NULL, return NULL.
- * 3. If the values are equal, return either one.
- *
- */
- int* min(int* param1, int* param2){
- if (param1 == NULL || *param2 < *param1){
- return param2;
- }
- if (param2 == NULL || *param2 >= *param1){
- return param1;
- }
- if (param1 == NULL && param2 == NULL){
- return NULL;
- }
- else return param1;
- }
- /*
- * Calculates the average of num_of_items values from standard input. The input values the function reads are of type double.
- * The program returns the calculated average as a pass-by-pointer, given to it as parameter named average.
- * You can assume that the average pointer's value has been initialized properly (e.g. to 0.0).
- *
- * Special cases:
- *
- * 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.
- * 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.
- * 3. If the average pointer given as parameter is NULL the program does nothing (no average calculations).
- * 4. In case of a failed read from standard input, the program calculates the average of the values until that point.
- *
- * Note:
- *
- * Make sure that your average function can never go to a state where it divides by zero.
- *
- */
- void average(int* num_of_items, double* average){
- if (*num_of_items <= 0){
- return;
- }
- if (num_of_items == NULL){
- return;
- }
- if (average == NULL){
- return;
- }
- int temp, sum;
- for (int i=0; i < *num_of_items; i++){
- scanf("%d", &temp);
- sum+=temp;
- *average = sum / *num_of_items;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement