Advertisement
Guest User

axasa

a guest
Nov 19th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. #define NUM_ARREGLOS 10
  5.  
  6.  
  7. void verarreglo(int *ver, int na){ // NA ES = A NUM_ARREGLOS
  8. int i;
  9. for(i = 0; i != na; i++){
  10. printf("N%d) %d\n",i,ver[i]);
  11. }
  12. }
  13.  
  14. int calcular_p(int arreglos[NUM_ARREGLOS]){ // CALCULAR PARES
  15. int t_pares = 0;
  16. int i = 0;
  17. for(i = 0; i != NUM_ARREGLOS; i++) if(arreglos[i] % 2 == 0) t_pares++; // TOTAL DE PARES
  18. return t_pares; // DEVOLVEMOS EL VALOR DE T_PARES.
  19. }
  20.  
  21. int sumar_arreglo(int *arreglos, int na){
  22. int temp=0; //TEMPORAL
  23. int i = 0;
  24. for(i = 0;i!= na; i++) temp += arreglos[i];
  25. return temp; // RETORNAMOS LA VARIABLE TEMPORAL QUE ES LA SUMA DE TODOS LOS ARREGLOS-
  26. }
  27.  
  28. void ordenarburbuja(int *arreglos, int na){ // NA ES = A NUM_ARREGLOS
  29. //ALGORITMO BURBUJA
  30. int temp;
  31. int z, v;
  32.  
  33. for(z = 1; z < na; ++z) {
  34. for(v = 0; v < (na - z); v++) {
  35. if(arreglos[v] > arreglos[v+1]){
  36. temp = arreglos[v];
  37. arreglos[v] = arreglos[v + 1];
  38. arreglos[v + 1] = temp;
  39. }
  40. }
  41. }
  42. }
  43.  
  44. int main(int argc, char **argv){
  45.  
  46. int arreglo[NUM_ARREGLOS];
  47. int i, z;
  48. for(i = 0; i != NUM_ARREGLOS; i++){ // Creamos a I y pedimos que se repita el ciclo hasta que I (Ingresados) Sea igual a la cantidad de arreglos definida.
  49. printf("Ingrese el arreglo %d: ", i+1);
  50. scanf(" %d",&arreglo[i]); // Pedimos el arreglo y lo ingresamos en la posicion I..
  51. for(z = 0; z != i; z++){ // Creamos a Z Y pedimos que se repita el for hasta que Z sea igual a I (Ingresados)
  52. if(arreglo[i] == arreglo[z]){ // COMPROBAMOS QUE EL ARREGLO INGRESADO (i) NO SEA IGUAL A NINGUNO DE LOS YA DEFINIDOS. (Z)
  53. printf("No se pueden repetir\n");
  54. i--; // Restamos uno a i para volver un ciclo antras en el for.
  55. break; //Salir del ciclo for
  56. }
  57. }
  58. }
  59.  
  60. printf("Orden ingresado:\n");
  61. verarreglo(arreglo, NUM_ARREGLOS);
  62.  
  63. printf("Orden burbuja:\n");
  64. ordenarburbuja(arreglo, NUM_ARREGLOS);
  65. verarreglo(arreglo, NUM_ARREGLOS);
  66.  
  67. //Crear arreglo par/inpar:
  68. int num_pares[calcular_p(arreglo)];
  69. int num_impares[(NUM_ARREGLOS-calcular_p(arreglo))];
  70. printf("Hay %d y %d impares\n", calcular_p(arreglo), (NUM_ARREGLOS-calcular_p(arreglo)) );
  71.  
  72. //AGREGAR PARES A ARREGLO
  73. int pares = 0, impares = 0;
  74. for(i = 0; i != NUM_ARREGLOS; i++) //SIZEOF ES PARA CALCULAR EL TAMAÑO DEL ARREGLO.
  75. {
  76. if(arreglo[i] % 2 == 0){
  77. num_pares[pares] = arreglo[i];
  78. pares++; // CANTIDAD DE PARES ENCONTRADOS HASTA EL MOMENTO.
  79. }
  80. else{
  81. num_impares[impares] = arreglo[i];
  82. impares++;
  83. }
  84. }
  85.  
  86. printf("Pares(%d):\n", pares);
  87. verarreglo(num_pares, pares);
  88. printf("Impares(%d):\n", impares);
  89. verarreglo(num_impares, impares);
  90. int suma_de_todos = sumar_arreglo(arreglo, NUM_ARREGLOS); // DEVOLVEMOS EL VALOR DE LA SUMA TOTAL
  91. int suma_p = sumar_arreglo(num_pares, pares); // Asignamos el valor de la suma total a la variable suma_p
  92. int suma_i = sumar_arreglo(num_impares, impares); // IGUAL PERO A SUMA_I
  93. printf("Suma total: %d - Suma pares %d - Suma impares %d\n", suma_de_todos, suma_p, suma_i);
  94.  
  95.  
  96. return 0;
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement