seergiomv

Examen matriz, contar pos neg ceros...

Jan 9th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #define N 10
  3. int introducir_datos(int x[N][N]);
  4. int contar(int x[N][N]);
  5. void ver_matriz(int x[N][N]);
  6. int substituir(int x[N][N]);
  7. int gestion(int x[N][N]){
  8.     int opcion;
  9.     do{
  10.         printf("\n1- Introducir dato\n2- Contar numeros positivos, negativos o ceros\n3- Ver matriz\n4- Cambiar por"
  11.            " matriz identidad\n5- Salir\nSeleccione una opcion:\n");
  12.     scanf("%d",&opcion);
  13.     switch (opcion){
  14.         case 1: introducir_datos(x);
  15.             break;
  16.         case 2: contar(x);
  17.         break;
  18.         case 3: ver_matriz(x);
  19.         break;
  20.         case 4: substituir(x);
  21.         break;
  22.     }
  23.     } while (opcion != 5);
  24.     return 0;
  25. }
  26. int introducir_datos(int x[N][N]){
  27.     int i,j;
  28.     printf("Introduce fila:\n");
  29.     scanf("%d",&i);
  30.     printf("Introduce columna:\n");
  31.     scanf("%d",&j);
  32.     printf("Introduce un valor:\n");
  33.     scanf("%d",&x[i-1][j-1]);
  34.     return x[i-1][j-1];
  35. }
  36. int contar(int x[N][N]){
  37.     int opcion2,pos = 0, neg = 0, zero = 0;
  38.     do {
  39.         printf("\n21- Contar positivos\n22- Contar negativos\n23- Contar ceros\n24-Volver atrás\nSeleccione:\n");
  40.         scanf("%d",&opcion2);
  41.         switch (opcion2){
  42.             case 21: for (int i = 0; i < N; i++){
  43.                 for(int j = 0; j < N; j++){
  44.                     if (x[i][j] > 0){
  45.                         pos++;
  46.                     }
  47.                 }
  48.             }
  49.             printf("Hay %d positivos",pos);
  50.             break;
  51.             case 22: for (int i = 0; i < N; i++){
  52.                 for (int j = 0; j < N; j++){
  53.                     if(x[i][j] < 0){
  54.                         neg++;
  55.                     }
  56.                 }
  57.             }
  58.             printf("Hay %d negativos\n",neg);
  59.             break;
  60.             case 23: for (int i = 0; i < N; i++){
  61.                 for (int j = 0; j < N; j++){
  62.                     if(x[i][j] == 0){
  63.                         zero++;
  64.                     }
  65.                 }
  66.             }
  67.             printf("Hay %d ceros",zero);
  68.             break;
  69.         }
  70.     } while (opcion2 != 24);
  71.     return 0;
  72. }
  73. void ver_matriz(int x[N][N]){
  74.     for (int i = 0; i < N; i++){
  75.         for (int j = 0; j< N; j++){
  76.             printf("%d\t",x[i][j]);
  77.         } printf("\n");
  78.     }
  79. }
  80. int substituir(int x[N][N]){
  81.     for (int i = 0; i < N; i++){
  82.         for (int j = 0; j < N; j++){
  83.             if (i == j){
  84.                 x[i][j] = 1;
  85.             } else{
  86.                 x[i][j] = 0;
  87.             }
  88.         }
  89.     }
  90. }
  91. int main(){
  92.     int mat[N][N];
  93.     for (int i = 0; i < N ; i++){
  94.         for (int j = 0; j < N; j++){
  95.             mat[i][j] = 0;
  96.         }
  97.     }
  98.     gestion(mat);
  99. }
Add Comment
Please, Sign In to add comment