seergiomv

Examen modificar matriz contar impares (doble menu)

Jan 8th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #define N 3
  3. void matriz(int x[N][N]);
  4. void matriz(int x[N][N]){
  5.     printf("Dame la matriz: \n");
  6.     for (int i = 0; i < N; i++){
  7.         for (int j = 0; j < N; j++){
  8.             scanf("%d",&x[i][j]);
  9.         }
  10.     }
  11.     for (int i = 0; i < N; i++){
  12.         for (int j = 0; j < N; j++){
  13.             printf("%d\t",x[i][j]);
  14.         } printf("\n");
  15.     }
  16. }
  17. void cambio(int x[N][N]){
  18.     int i,j;
  19.     printf("\nSeleccione fila:\n");
  20.     scanf("%d",&i);
  21.     printf("\nSeleccione columna:\n");
  22.     scanf("%d",&j);
  23.     printf("Introduce cifra:\n");
  24.     scanf("%d",&x[i-1][j-1]);
  25. }
  26. void cuenta_impar(int x[N][N]){
  27.     int impares = 0;
  28.     for (int i = 0; i < N; i++){
  29.         for (int j = 0; j < N; j++){
  30.             if(x[i][j] % 2 != 0){
  31.                 impares++;
  32.             }
  33.         }
  34.     } printf("\nHay %d impares\n",impares);
  35. }
  36. int main(){
  37.     int opcion,opcion2,x[N][N];
  38.     matriz(x);
  39.     do{
  40.         printf("1- Cambiar elemento\n2- Contar impares\n3- Ver matriz\n4- Salir\nSeleccione una opcion:\n");
  41.         scanf("%d",&opcion);
  42.         switch (opcion){
  43.             case 1: cambio(x);
  44.             break;
  45.             case 2: cuenta_impar(x);
  46.             break;
  47.             case 3: for (int i = 0; i < N; i++){
  48.                 for (int j = 0; j < N; j++){
  49.                     printf("%d\t",x[i][j]);
  50.                 } printf("\n");
  51.             }
  52.                 break;
  53.             case 5: do {
  54.                 printf("\t1- Volver atrás:\n");
  55.                 scanf("%d",&opcion2);
  56.             } while (opcion2 != 1);
  57.                 break;
  58.         }
  59.     } while (opcion != 4);
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment