Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Return multiple values from a function using pointers

#include <iostream>

using namespace std; 

void maxAndMin(int num[], int size, int* max, int* min){
    for(int i = 0; i < size; i++){
    if(num[i] < *min)
        *min = num[i];
    if(num[i] > *max)
        *max = num[i];
    }
    cout << "Minimum number is: "<< *min << endl;
    cout << "Maximum number is: "<< *max << endl;
}

int main(){
    int numbers[5];
    for (int i = 0 ; i < 5 ; i++){
        cout << "Number: ";
        cin >> numbers[i];
    }
    int min = numbers[0];
    int max = numbers[0];
    maxAndMin(numbers, 5, &max, &min);
}
 
PREVIOUS NEXT
Tagged: #Return #multiple #values #function #pointers
ADD COMMENT
Topic
Name
8+2 =