Last active
January 17, 2017 12:22
-
-
Save olegon/6230d56c937f37d5650b02e7d6cc5077 to your computer and use it in GitHub Desktop.
Exercício 2.5 (https://www.ime.usp.br/~pf/algoritmos/aulas/recu.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void minmax(int *v, int n, int *min, int *max); | |
int main (void) { | |
int v[5] = { -1, 7, -3, 11, 4 }; | |
int min, max; | |
minmax(v, 5, &min, &max); | |
printf("Min: %d\n", min); | |
printf("Max: %d\n", max); | |
return 0; | |
} | |
/* | |
Dado um vetor de inteiros v, um inteiro n >= 1 e dois ponteiros para inteiros *min e *max, | |
atribui o menor valor do vetor v[0..n-1] para *min e | |
atribui o maior valor do vetor v[0..n-1] para *max. | |
*/ | |
void minmax(int *v, int n, int *min, int *max) { | |
if (n == 1) { | |
*min = v[0]; | |
*max = v[0]; | |
} | |
else { | |
minmax(v, n - 1, min, max); | |
/* Invariante: min e max possem o menor e o maior valor, respectivamente, do vetor v[0..n-2] */ | |
if (v[n - 1] > *max) *max = v[n - 1]; | |
if (v[n - 1] < *min) *min = v[n - 1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment