gonzalob

Untitled

Apr 30th, 2020
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void ordenacionSeleccion(int matriculas[20],char nombres[20][30],int validos);
  6.  
  7.  
  8. int main()
  9. {
  10.     int matriculas[20];
  11.     int edades[20];
  12.     char nombres[20][30];
  13.     int validos = cargarDatos(matriculas,nombres);
  14.     mostrar(matriculas,nombres,validos);
  15.     ordenacionSeleccion(matriculas,nombres,validos);
  16.     mostrar(matriculas,nombres,validos);
  17.     return 0;
  18. }
  19.  
  20. int cargarDatos(int matriculas[20],char nombres[20][30])
  21. {
  22.     int validos = 0;
  23.     char mander = 's';
  24.     while ((mander == 's') && (validos<20))
  25.     {
  26.         printf("ingrese matricula del alumno %d\n",validos+1);
  27.         fflush(stdin);
  28.         scanf("%d",&matriculas[validos]);
  29.  
  30.         printf("ingrese nombre del alumno %d\n",validos+1);
  31.         fflush(stdin);
  32.         scanf("%s",&nombres[validos]);
  33.  
  34.         printf("desea seguir? s/n \n");
  35.         fflush(stdin);
  36.         scanf("%c",&mander);
  37.  
  38.         validos++;
  39.     }
  40.  
  41.  
  42.     return validos;
  43. }
  44.  
  45. void mostrar(int matriculas[20],char nombres[20][30], int validos)
  46. {
  47.     int i = 0;
  48.     for (i=0;i<validos;i++)
  49.     {
  50.         printf("Nombre --> %s\n",nombres[i]);
  51.         printf("Mat --> %d\n",matriculas[i]);
  52.     }
  53. }
  54.  
  55.  
  56.  
  57. int posicionMenor(int arreglo[10],int validos, int pos)
  58. {
  59.     int menor = arreglo[pos];
  60.     int posMenor = pos;
  61.  
  62.     int indice = pos + 1;
  63.  
  64.     while (indice < validos)
  65.     {
  66.         if (menor > arreglo[indice])
  67.         {
  68.             menor = arreglo[indice];
  69.             posMenor = indice;
  70.         }
  71.  
  72.         indice++;
  73.     }
  74.  
  75.     return posMenor;
  76. }
  77.  
  78. void ordenacionSeleccion(int arreglo[],char nombres[20][30],int validos)
  79. {
  80.     int indice = 0;
  81.     int posMenor;
  82.     int aux;
  83.  
  84.     char auxNombre[30];
  85.  
  86.     while (indice<validos-1)
  87.     {
  88.         //el menor de todos pero a partir de indice
  89.         posMenor = posicionMenor(arreglo,validos,indice);
  90.  
  91.         aux = arreglo[posMenor];
  92.         arreglo[posMenor] = arreglo[indice];
  93.         arreglo[indice] = aux;
  94.  
  95.  
  96.         strcpy(auxNombre,nombres[posMenor]);//destino - origen
  97.         strcpy(nombres[posMenor],nombres[indice]);
  98.         strcpy(nombres[indice],auxNombre);
  99.  
  100.         indice++;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment