Advertisement
Nurbo

MaiorValorPosicao

Aug 16th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int array[5], valor, maiorValor = -1, posMaiorValor;
  6.     bool teste = 1;
  7.     for(int i = 0; i < 5; i++) //Preenchendo o vetor.
  8.         array[i] = 0;
  9.     for(int j = 0; j < 5; j++)
  10.     {
  11.         printf("Digite o %d valor >>> ",j+1);
  12.         scanf("%d",&valor);
  13.         for(int i = 0; i < 5; i++) //Percorre o vetor para fazer o teste.
  14.         {          
  15.             if(valor == array[i] || valor <= 0) //Testa se o valor é diferente e maior que zero.
  16.             {
  17.                 teste = 0;
  18.                 j--;
  19.                 printf("ERRO!!! Este valor ja existe ou e menor ou igual a 0.\n");
  20.                 break;
  21.             }  
  22.             if(valor > maiorValor) //Pega o maior valor e a posição no vetor em que ele se encontra.
  23.             {
  24.                 maiorValor = valor;
  25.                 posMaiorValor = j;
  26.             }  
  27.         }
  28.         if(teste) //Se existir um valor igual ou menor que 0 no vetor, o valor não será atribuído.
  29.             array[j] = valor;
  30.         else
  31.             teste = 1;
  32.     }
  33.     printf("O maior valor digitado foi %d\n",maiorValor);
  34.     printf("Sua posicao e %d",posMaiorValor+1);
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement