-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr11ex2.c
52 lines (40 loc) · 1.03 KB
/
pr11ex2.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
// Aluno: Bernardo Lansing
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int substitui_menor(int vetor[], int tam, int x) {
int i, menor = 101, indmenor;
// ENCONTRAR O MENOR ELEMENTO ---------------
for (i = 0; i < tam; i++)
if (vetor[i] < menor) {
menor = vetor[i];
indmenor = i;
}
// ------------------------------------------
// SUBSTITUIR OU NÃO O ELEMENTO -------------
if (menor > x) {
*(vetor + indmenor) = x;
return 1;
}
// ------------------------------------------
return 0;
}
int main() {
int i, x, vetor[10];
// GERAR E IMPRIMIR O VETOR -----------------
srand(time(NULL));
for (i = 0; i < 10; i++) {
vetor[i] = rand() % 101;
printf("%i ", vetor[i]);
}
// ------------------------------------------
// COLETAR X E EXECUTAR A OPERAÇÃO ----------
printf("\nEntre com o valor de X: ");
scanf("%i", &x);
if (substitui_menor(vetor, 10, x)) {
for (i = 0; i < 10; i++) printf("%i ", vetor[i]);
puts("\nFoi feita a troca!");
}
else puts("\nNão foi feita a troca!");
return 0;
}