Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Jevin Olano
- // jolano
- // CSE 101
- // October 24, 2019
- // Sparse.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "List.h"
- #include "Matrix.h"
- int main(int argc, char* argv[]) {
- // Declarations -------------------------------------
- FILE *in, *out; // handlers for input/output files
- char line[256];
- int size = 0; // size of the matrix to be created
- int numLinesA = 0; // # of lines in Matrix A
- int numLinesB = 0; // # of lines in Matrix B
- // Preliminary --------------------------------------
- // check command line for correct number of arguments
- if (argc != 3) {
- printf("Usage: %s <input> <output>\n", argv[0]);
- exit(1);
- }
- // open input file for reading
- in = fopen(argv[1], "r");
- if (in == NULL) {
- printf("Unable to read from file %s\n", argv[1]);
- exit(1);
- }
- // open output file for writing
- out = fopen(argv[2], "w");
- if (out == NULL) {
- printf("Unable to write to file %s\n", argv[2]);
- exit(1);
- }
- // Matrix Manipulation ------------------------------
- fgets(line, sizeof(line), in);
- sscanf(line, "%d %d %d", &size, &numLinesA, &numLinesB); // reads first line of input file
- fgets(line, sizeof(line), in); // should read the blank line
- // create matrices
- Matrix A = newMatrix(size);
- Matrix B = newMatrix(size);
- readMatrix(in, A, numLinesA); // handles Matrix A
- fgets(line, sizeof(line), in); // should read the blank line
- readMatrix(in, B, numLinesB);// handles Matrix B
- // print initial matrices (A and B)
- fprintf(out, "A has %d non-zero entries:\n", numLinesA); // numLinesA might be the wrong variable to use
- printMatrix(out, A);
- fprintf(out, "\n");
- fprintf(out, "B has %d non-zero entries:\n", numLinesB); // numLinesB might be the wrong variable to use
- printMatrix(out, B);
- fprintf(out, "\n");
- // print (1.5)*A
- fprintf(out, "(1.5)*A =\n");
- Matrix sMult = scalarMult(1.5, A);
- printMatrix(out, sMult);
- freeMatrix(&sMult);
- fprintf(out, "\n");
- // print A+B
- fprintf(out, "A+B =\n");
- Matrix sum1 = sum(A, B);
- printMatrix(out, sum1);
- freeMatrix(&sum1);
- fprintf(out, "\n");
- // print A+A
- fprintf(out, "A+A =\n");
- Matrix sum2 = sum(A, A);
- printMatrix(out, sum2);
- freeMatrix(&sum2);
- fprintf(out, "\n");
- // print B-A
- fprintf(out, "B-A =\n");
- Matrix diff1 = diff(B, A);
- printMatrix(out, diff1);
- freeMatrix(&diff1);
- fprintf(out, "\n");
- // print A-A (should output blank line)
- fprintf(out, "A-A =\n");
- Matrix diff2 = diff(A, A);
- printMatrix(out, diff2);
- freeMatrix(&diff2);
- fprintf(out, "\n");
- // print Transpose(A)
- fprintf(out, "Transpose(A) =\n");
- Matrix trans = transpose(A);
- printMatrix(out, trans);
- freeMatrix(&trans);
- fprintf(out, "\n");
- // print A*B
- fprintf(out, "A*B =\n");
- Matrix product1 = product(A, B);
- printMatrix(out, product1);
- freeMatrix(&product1);
- fprintf(out, "\n");
- // print B*B
- fprintf(out, "B*B =\n");
- Matrix product2 = product(B, B);
- printMatrix(out, product2);
- free(product2);
- fprintf(out, "\n");
- }
- // helper method to read input files
- void readMatrix(FILE* in, Matrix M, int numLines) {
- char line[256];
- int rowNum;
- int colNum;
- double value;
- for (int i = 0; i < numLines; i++) {
- fgets(line, sizeof(line), in);
- sscanf(line, "%d %d %lf", &rowNum, &colNum, &value);
- changeEntry(M, rowNum, colNum, value);
- }
- }
Add Comment
Please, Sign In to add comment