Advertisement
marcospaulodc

Maior menor com ponteiro

Nov 5th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. /*Escreva uma função que dado um vetor
  2.  * de inteiros de tamanho max, calcule
  3.  * o maior e o menor elemento do vetor.
  4.  * Utilize ponteiros, faça o main.
  5.  
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8.  
  9.   int main()
  10.   {
  11.     int i, max, menor=1000, maior=0;
  12.    
  13.     printf("Tamanho do vetor: ");
  14.     scanf("%d",&max);
  15.    
  16.     int vet[max];
  17.    
  18.     printf("Preencha o vetor:\n");
  19.     for(i=0;i<max;i++){
  20.       scanf("%d",&vet[i]);
  21.     if (vet[i]<menor){
  22.         menor=vet[i];}
  23.      
  24.     if(vet[i]>maior){
  25.       maior=vet[i];}}
  26.    
  27.     printf("O vetor digitado eh: ");
  28.     for(i=0;i<max;i++)
  29.     printf("%d ",vet[i]);
  30.    
  31.     printf("\nO menor do vetor eh: %d ",menor);
  32.     printf("\nO maior do vetor eh: %d \n",maior);  } */ // a minha
  33.    
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36.  
  37.   void func(int n[], int max, int *maior, int *menor){
  38.     int i;
  39.     for(i=0;i<max;i++){
  40.       if(n[i]<*menor){
  41.       *menor=n[i];}
  42.       if(n[i]>*maior){
  43.       *maior=n[i];}
  44.     }
  45.   }
  46.    
  47.     int main()
  48.     {
  49.       int i, max, x=0, y=1000000000;
  50.      
  51.       scanf("%d",&max); //tamanho do vetor
  52.       int n[max];
  53.       for(i=0;i<max;i++)
  54.     scanf("%d",&n[i]);
  55.      
  56.       func(n,max,&x,&y);
  57.       printf("%d %d\n",y,x);
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement