Advertisement
Drowze

Exercício Extra Para dia 12/10

Oct 3rd, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1.  /* Lógica: Recebo n, k e uma matriz[n][COLUNAS] de strings
  2. Coloco as strings da matriz[n][COLUNAS] em ordem alfabetica
  3. pego a string matriz[k-1] e imprimo*/
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. #define LINHAS 100 //até 100 nomes, como descrito no exercicio
  9. #define COLUNAS 21 //nome de até 20 caractéres, como descrito no exercicio
  10.  
  11. void RecebeDados(int *n, int *k, char alunos[][COLUNAS]);
  12. void Organiza(int n, int k, char alunos[][COLUNAS]);
  13.  
  14. int main(){
  15.         int n, k;
  16.         char alunos[LINHAS][COLUNAS];
  17.  
  18.         RecebeDados(&n,&k,alunos);
  19.         Organiza(n, k, alunos);
  20.  
  21.         printf("%s\n", alunos[k-1]);
  22.         return 0;
  23. }
  24.  
  25. void RecebeDados(int *n, int *k, char alunos[][COLUNAS]){
  26.         scanf("%d %d", n, k);
  27.         getchar();
  28.         for(int i=0; i<*n; i++) gets(alunos[i]);
  29. }
  30.  
  31. void Organiza(int n, int k, char alunos[][COLUNAS]){
  32.         char aux[COLUNAS];
  33.        
  34.         for(int i=0; i<n-1; i++){      
  35.                 for(int j=i+1; j<n; j++){
  36.                         if(strcmp(alunos[i], alunos[j]) > 0){
  37.                                 strcpy(aux, alunos[i]);
  38.                                 strcpy(alunos[i], alunos[j]);
  39.                                 strcpy(alunos[j], aux);
  40.                         }
  41.                 }
  42.         }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement