Advertisement
Guest User

Exercício de Thread

a guest
Sep 20th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NUM_ROWS 2
  5. #define NUM_COLUMNS 2
  6.  
  7. struct params
  8. {
  9.     int m_1[NUM_ROWS][NUM_COLUMNS];
  10.     int m_2[NUM_ROWS][NUM_COLUMNS];
  11.     int m[NUM_ROWS][NUM_COLUMNS];
  12.     int row;
  13.     int column;
  14. };
  15. typedef struct params params_t;
  16.  
  17. void calculate_cell(void *params);
  18.  
  19.  
  20. int main(int argc,char* argv[]){
  21.     int mat1[NUM_ROWS][NUM_COLUMNS] = {{2,2},{5,1}};
  22.     int mat2[NUM_ROWS][NUM_COLUMNS] = {{3,3},{3,4}};
  23.     int matResul[NUM_ROWS][NUM_COLUMNS];
  24.     int i, j, k, result=0;
  25.  
  26.     params_t *p = malloc(sizeof(params_t));
  27.  
  28.     // ERRO NO PARSE
  29.     // matriz.c:31:10: error: incompatible types when assigning to type ‘int[2][2]’ from type ‘int (*)[2]’
  30.     p->m_1 = mat1;
  31.     p->m_2 = mat2;
  32.     p->m = matResul;
  33.  
  34.     for (i = 0; i < NUM_ROWS; i++){
  35.    
  36.         for (j = 0; j < NUM_COLUMNS; j++) {
  37.            
  38.             p->row = i;
  39.             p->column = j;
  40.            
  41.             calculate_cell((void*)p);
  42.         }
  43.     }
  44.  
  45.     printf("Result\n");
  46.     for(i = 0; i < NUM_ROWS; i++) {
  47.         for (j = 0; j < NUM_COLUMNS; j++)
  48.             printf("%d\t", matResul[i][j]);
  49.         printf("\n");
  50.     }
  51.  
  52.     return 0;
  53. }
  54.  
  55. void calculate_cell(void *params)
  56. {
  57.     int i;
  58.     int result = 0;
  59.     int value_row;
  60.     int value_column;
  61.     params_t *p = (params_t *) params;
  62.  
  63.     for (i = 0; i < 2; i++)
  64.     {
  65.         value_row = p->row;
  66.         value_column = p->column;
  67.        
  68.         result += p->m_1[value_row][i] * p->m_2[i][value_column];
  69.     }
  70.  
  71.     p->m[value_row][value_column] = result;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement