-
Notifications
You must be signed in to change notification settings - Fork 0
/
ponteiro4.c
65 lines (39 loc) · 1.5 KB
/
ponteiro4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
/*
3- Escreva um método que receba como parâmetros um vetor de inteiros, e retorne o maior elemento e o menor elemento. Observe que uma função em C não pode retornar dois valores, assim você deverá passar por referência duas variáveis, uma que represente o maior e outra o menor valor do vetor
*/
//passando com ponteiro, passamos a referencia do endereço.
void maiorMenor(int vet[], int *maior, int *menor ){
int i;
*maior = vet[0];
*menor = vet[0];
for(i=1; i<5; i++){
if (vet[i]> *maior){
*maior = vet[i];
}
if (vet[i]< *menor)
*menor = vet[i];
}
}
int main(){
int *vetor, tamanho,i, Ever = 1;
double *vetor2;
vetor2 = malloc (sizeof(double)*10); //aloca 10 doubles para o vetor 2
int *maior, *menor;
//for (;Ever;){
printf("Tamanho do vetor a ser declarado: ");
scanf("%d", &tamanho);
vetor = malloc (sizeof(int)*tamanho); //aloca um vetor de inteiros de "tamanho"
for (i=0; i< tamanho; i++)
scanf("%d", &vetor[i]);
free(vetor);//LIBERA MEMOMÓRIA QUE CRIAMOS.
//}
maior = malloc (sizeof(int)); //reserva/aloca o endereço de memória.
menor = malloc (sizeof(int));
maiorMenor(vetor, maior, menor);
printf("Maior: %d\n Menor: %d\n\n", *maior, *menor);
free(maior); //libera um espaço de memória
free(menor); //libera o espaço de memória do menor.
return 0;
}