Advertisement
Drowze

APC B XX - Adicionais1 - 05 (maior e menor do vetor)

Sep 21st, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1.  /* Escreve um subprograma que receba como parâmetro um vetor de inteiros
  2. e retorne, simultaneamente, o índice do maior elemento e o índice do menor
  3. elemento do vetor. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #define MAX 5
  9.  
  10. void AnalisaVetor(int a[], int *maior, int *menor);
  11.  
  12. void main(){
  13.     int a[MAX], maior, menor;
  14.     printf("Digite um 5 numeros e entregarei o indice do maior e do menor numero\n");
  15.  
  16.     for(int i=0; i<MAX; i++){
  17.         scanf("%d", &a[i]);
  18.     }
  19.  
  20.     AnalisaVetor(a, &maior, &menor);
  21.  
  22.     printf("\nIndice do maior: %d\n", maior);
  23.     printf("Indice do menor: %d\n", menor);
  24.  
  25.     system("Pause");
  26. }
  27.  
  28. void AnalisaVetor(int a[], int *maior, int *menor){
  29.     *maior = 0;
  30.     *menor = 0;
  31.     for(int i=1; i<MAX; i++){
  32.         if(a[i]> a[*maior]) *maior = i;
  33.         if(a[i]< a[*menor]) *menor = i;
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement