Menu
Coddy logo textTech

Recap: Function Pointer Args

Part of the Logic & Flow section of Coddy's C journey — lesson 30 of 63.

challenge icon

Challenge

Easy

Write a C program that implements a function to find both the minimum and maximum values in an array using pointer arguments. Your program should:

  1. Create a function named findMinMax that takes four parameters:
    • An integer array
    • The size of the array
    • A pointer to an integer for storing the minimum value
    • A pointer to an integer for storing the maximum value
  2. Inside the findMinMax function, iterate through the array to find the minimum and maximum values
  3. Use the dereference operator to store the minimum value at the address pointed to by the min pointer
  4. Use the dereference operator to store the maximum value at the address pointed to by the max pointer
  5. In the main function, read an integer n representing the number of elements in the array
  6. Read n integer values and store them in an array
  7. Declare two integer variables minValue and maxValue to store the results
  8. Call the findMinMax function, passing the array, its size, and the addresses of minValue and maxValue
  9. Print the results in the format: Minimum: [value] and Maximum: [value]

For example, if the input is:

5
12 7 23 4 18

Your output should be:

Minimum: 4
Maximum: 23

This challenge demonstrates the powerful technique of using multiple pointer arguments to return multiple values from a single function call, allowing the function to modify variables in the calling function's scope.

Try it yourself

#include <stdio.h>

// TODO: Write your findMinMax function here

int main() {
    int n;
    scanf("%d", &n);
    
    int arr[n];
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    
    int minValue, maxValue;
    
    // TODO: Call the findMinMax function here
    
    printf("Minimum: %d\n", minValue);
    printf("Maximum: %d\n", maxValue);
    
    return 0;
}

All lessons in Logic & Flow