Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "multiplymatrix.h"
  4. #include <assert.h>
  5.  
  6.  
  7. int main (int argc, char *argv[]) {
  8.    
  9.     Matrix *matrixA = malloc(sizeof(Matrix));
  10.     Matrix *matrixB = malloc(sizeof(Matrix));
  11.     char *filename = malloc(1000*sizeof(char));
  12.     filename = "matrixtest";
  13.     matrixA = readMatrix(filename);
  14.     assert(matrixA != NULL);
  15.         for (int i = 0; i < matrixA->rows * matrixA->rows; i++)
  16.         printf("%i - %f\n",i, matrixA->matrix[i]);
  17.        
  18.     filename = "matrixtest2";
  19.     matrixB = readMatrix(filename);
  20.    // assert(matrixB != NULL);
  21.     matrixB->matrix = matrixA->matrix;
  22.    
  23.     Matrix *result =  multiplyMatrix(matrixA, matrixA, 1);
  24.    
  25.     for (int i = 0; i < result->rows * result->columns; i++) {
  26.         if(i > 0 && i % result->rows == 0) printf("\n");
  27.         printf("%.5f ", result->matrix[i]);
  28.     }
  29.     printf("\n");
  30.    
  31.  
  32.     free (filename);
  33.     free(result->matrix);
  34.     free(result);
  35.     free(matrixA->matrix);
  36.     free(matrixA);
  37.     free(matrixB);
  38.     return 0;
  39. }
  40.  
  41.  
  42. ----------------------------------------------------------------------------
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <pthread.h>
  47. #include "multiplymatrix.h"
  48.  
  49. Matrix *readMatrix(const char filename[]) {
  50.  
  51.     FILE *stream = NULL;
  52.     char *line = NULL;
  53.     size_t len = 0;
  54.     ssize_t read;
  55.     char split[2] = " ";
  56.     char *ptr;
  57.     int elemCount = 0;
  58.     Matrix *readMatrix = malloc(sizeof(Matrix));
  59.       if (readMatrix == NULL) {
  60.         perror("memory couldn't be allocated");
  61.         exit(EXIT_FAILURE);
  62.     }
  63.    
  64.     readMatrix->columns = 0;
  65.    
  66.  
  67.     stream = fopen(filename, "r");
  68.     while ((read = getline(&line, &len, stream)) != -1) {
  69.         readMatrix->rows++;
  70.         readMatrix->columns++;
  71.     }
  72.     fclose(stream);
  73.    
  74.     readMatrix->matrix  = malloc(readMatrix->rows * readMatrix->rows * sizeof(double)); //nXn ->
  75.    
  76.     stream = fopen(filename, "r");
  77.     if (stream == NULL) exit(EXIT_FAILURE);
  78.     while ((read = getline(&line, &len, stream)) != -1) { // man7.org/linux/man-pages/man3(getdelim.3.html
  79.         ptr = strtok(line, split);
  80.         readMatrix->matrix[elemCount] = atof(ptr);
  81.         elemCount++;
  82.         while(ptr != NULL) {
  83.             ptr = strtok(NULL, split);
  84.             if (ptr == NULL) break;
  85.             readMatrix->matrix[elemCount] = atof(ptr);
  86.             elemCount++;
  87.         }
  88. }
  89.  
  90.     fclose(stream);
  91.     free(line);
  92.     return readMatrix;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement