SHARE
TWEET

Exz

allekco Jun 5th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <math.h>
  7. #include <stddef.h>
  8. #include <time.h>
  9.  
  10. typedef struct Matrix{
  11.     int ncolumns;
  12.     int nrows;
  13.     int **c;
  14. } Matrix;
  15.  
  16. int randomazer (int min, int max){
  17.     int random;
  18.     random = rand();
  19.     random = ((float)random / RAND_MAX)*(max-min)+min;
  20.     return(random);
  21. }
  22.  
  23. Matrix create (int n, int min, int max){
  24.     Matrix A;
  25.     int i=0,j=0;
  26.     if (( A.c = malloc ( n * sizeof ( int *))) == NULL )
  27.         exit (1);
  28.     for (i = 0; i < n ; i++ ) {
  29.         if (( A.c[i] = malloc ( n * sizeof ( int ))) == NULL )
  30.             exit (2);
  31.     }
  32.     for (i = 0; i < n; i++){
  33.         for ( j = 0; j < n; j++){
  34.             A.c [i][j] = randomazer(min, max);
  35.         }
  36.     }
  37.     A.nrows = n;
  38.     A.ncolumns = n;
  39.     return A;
  40. }
  41.  
  42. Matrix sum (Matrix A, Matrix B){
  43.     Matrix n;
  44.     n = create (A.nrows, A.ncolumns, 0);
  45.     int i=0, j=0;
  46.     for (i=0; i<A.nrows; i++){
  47.         for ( j=0; j<A.ncolumns; j++){
  48.             n.c[i][j]=A.c[i][j]+B.c[i][j];
  49.         }
  50.     }
  51.     n.nrows;
  52.     n.ncolumns;
  53.     return n;
  54. }
  55.  
  56. Matrix sum (Matrix A, Matrix B){
  57.     Matrix n;
  58.     n = create (A.nrows, A.ncolumns, 0);
  59.     int i=0, j=0;
  60.     for (i=0; i<A.nrows; i++){
  61.         for ( j=0; j<A.ncolumns; j++){
  62.             n.c[i][j]=A.c[i][j]+B.c[i][j];
  63.         }
  64.     }
  65.     n.nrows;
  66.     n.ncolumns;
  67.     return n;
  68. }
  69.  
  70. void output (Matrix A){
  71.     int i=0, j=0;
  72.     for (i=0; i<A.nrows; i++){
  73.         for ( j=0; j<A.ncolumns; j++){
  74.             printf ("%d ", A.c [i][j]);
  75.         }
  76.     printf (" \n" );
  77.     }
  78.  
  79. }
  80.  
  81. int main (void) {
  82.     srand(time(NULL));
  83.     Matrix A;
  84.     Matrix B;
  85.     Matrix C;
  86.     int n = 4;
  87.     A = create (n, 0, 10);
  88.     B = create (n, 0, 10);
  89.     output (A);
  90.     printf ("\n");
  91.     output (B);
  92.     C = sum(A, B);
  93.     printf ("\n");
  94.     output (C);
  95.     return 0;
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top