Created
October 25, 2016 04:35
-
-
Save tarcisio-marinho/f41cb748dc2894fae701adc43cfc0d81 to your computer and use it in GitHub Desktop.
Diferencia máxima entre dois inteiros em um vetor
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 getNumbers(int vet[],int n); | |
int maxDiff(int vet[],int n); | |
int main(){ | |
int number,result; | |
do{ | |
scanf("%d",&number); | |
}while(number<1); | |
int vet[number]; | |
vet[number]; | |
getNumbers(vet,number); | |
result=maxDiff(vet,number); | |
printf("%d",result); | |
return 0; | |
} | |
void getNumbers(int vet[],int n){ | |
int i; | |
for(i=0;i<n;i++){ | |
scanf("%d",&vet[i]); | |
} | |
} | |
int maxDiff(int vet[],int n){ | |
int i,posi,max=0,min=0; | |
for(i=0;i<n;i++){ | |
if(vet[i]>max){ | |
max=vet[i]; | |
posi=i; | |
} | |
} | |
min=max; | |
for(i=0;i<posi;i++){ | |
if(vet[i]<min){ | |
min=vet[i]; | |
} | |
} | |
if(vet[0]==max){ | |
return -1; | |
}else{ | |
return max-min; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment