Advertisement
Raul_julian

matrix.c

Apr 5th, 2016
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX 200
  4. #define LESS -1
  5.  
  6. // This function creates the matrix
  7. int** create(int n) {
  8.     int i, j ;
  9.     int **matrix ;
  10.    
  11.     matrix = (int **) malloc(n * sizeof(int *)) ;
  12.    
  13.     for(i = 0; i < n; i++)
  14.         matrix[i] = (int *) malloc(n * sizeof(int));
  15.    
  16.     for(i = 0; i < n; i++) {
  17.         for(j = 0; j < n; j++)  
  18.             matrix[i][j] = 0 ;
  19.     }
  20.    
  21.     return matrix ;
  22. }
  23.  
  24. // This function prints the matrix
  25. void print(int **matrix, int n) {
  26.     int i, j ;
  27.    
  28.     for(i = 0; i < n ; i++) {
  29.         for(j = 0; j < n; j++)
  30.             printf("%d ", matrix[i][j]);
  31.  
  32.         printf("\n");
  33.     }
  34. }
  35.  
  36. int finish(int **matrix, int n) {
  37.     int i, j ;
  38.     int row, col ;
  39.    
  40.     // check rows
  41.     for(i = 0; i < n; i++) {
  42.         row = 0;
  43.        
  44.         for(j = 0; j < n; j++) {
  45.             if(row == matrix[i][j]) {
  46.                 if(!row) {
  47.                     row = LESS ;
  48.                     break ;
  49.                 }
  50.             } else if(j) {
  51.                     row = LESS ;
  52.                     break ;
  53.             }  
  54.            
  55.             row = matrix[i][j] ;
  56.         }
  57.        
  58.         if(row == LESS)
  59.             return 0 ;
  60.     }
  61.    
  62.     // check cols
  63.    
  64.    
  65.     return 1 ;
  66. }
  67.  
  68. int main() {
  69.    
  70.     int i, j, k, n ;
  71.     int choose = 0 ;
  72.    
  73.     printf("Welcome new user\n\nHELP SYSTEM: The computer is 2, you is 1 and the pattern is 0.\n\n");
  74.     printf("1 - Start the game\n2-Exit\n");
  75.    
  76.     back:
  77.    
  78.     printf("Please, insert your option: ");
  79.     scanf("%d", &choose);
  80.      
  81.     switch(choose) {
  82.         case 1: {
  83.             printf("Please, insert the order of matrix: ");
  84.             scanf("%d", &n);
  85.             break ;
  86.         }  
  87.        
  88.         case 2: {
  89.             printf("Oh :( Bye bye");
  90.             break ;
  91.         }
  92.        
  93.         default: {
  94.             goto back ;
  95.         }
  96.     }
  97.    
  98.     int **matrix = create(n), cond = 0;
  99.     print(matrix, n);
  100.    
  101.    
  102.     while(!cond) {
  103.         int row, col ;
  104.        
  105.         printf("Insert the row and col: ");
  106.         scanf("%d %d", &row, &col);
  107.         matrix[row-1][col-1] = 1 ;
  108.         printf("\n");
  109.         print(matrix, n);
  110.        
  111.         if(finish(matrix, n))
  112.             cond = 1 ;
  113.     }
  114.        
  115.    
  116.     return 0 ;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement