Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "multiplymatrix.h"
- #include <assert.h>
- int main (int argc, char *argv[]) {
- Matrix *matrixA = malloc(sizeof(Matrix));
- Matrix *matrixB = malloc(sizeof(Matrix));
- char *filename = malloc(1000*sizeof(char));
- filename = "matrixtest";
- matrixA = readMatrix(filename);
- assert(matrixA != NULL);
- for (int i = 0; i < matrixA->rows * matrixA->rows; i++)
- printf("%i - %f\n",i, matrixA->matrix[i]);
- filename = "matrixtest2";
- matrixB = readMatrix(filename);
- // assert(matrixB != NULL);
- matrixB->matrix = matrixA->matrix;
- Matrix *result = multiplyMatrix(matrixA, matrixA, 1);
- for (int i = 0; i < result->rows * result->columns; i++) {
- if(i > 0 && i % result->rows == 0) printf("\n");
- printf("%.5f ", result->matrix[i]);
- }
- printf("\n");
- free (filename);
- free(result->matrix);
- free(result);
- free(matrixA->matrix);
- free(matrixA);
- free(matrixB);
- return 0;
- }
- ----------------------------------------------------------------------------
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <pthread.h>
- #include "multiplymatrix.h"
- Matrix *readMatrix(const char filename[]) {
- FILE *stream = NULL;
- char *line = NULL;
- size_t len = 0;
- ssize_t read;
- char split[2] = " ";
- char *ptr;
- int elemCount = 0;
- Matrix *readMatrix = malloc(sizeof(Matrix));
- if (readMatrix == NULL) {
- perror("memory couldn't be allocated");
- exit(EXIT_FAILURE);
- }
- readMatrix->columns = 0;
- stream = fopen(filename, "r");
- while ((read = getline(&line, &len, stream)) != -1) {
- readMatrix->rows++;
- readMatrix->columns++;
- }
- fclose(stream);
- readMatrix->matrix = malloc(readMatrix->rows * readMatrix->rows * sizeof(double)); //nXn ->
- stream = fopen(filename, "r");
- if (stream == NULL) exit(EXIT_FAILURE);
- while ((read = getline(&line, &len, stream)) != -1) { // man7.org/linux/man-pages/man3(getdelim.3.html
- ptr = strtok(line, split);
- readMatrix->matrix[elemCount] = atof(ptr);
- elemCount++;
- while(ptr != NULL) {
- ptr = strtok(NULL, split);
- if (ptr == NULL) break;
- readMatrix->matrix[elemCount] = atof(ptr);
- elemCount++;
- }
- }
- fclose(stream);
- free(line);
- return readMatrix;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement