Pweebs

Untitled

Oct 27th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. // Jevin Olano
  2. // jolano
  3. // CSE 101
  4. // October 24, 2019
  5. // Sparse.c
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "List.h"
  11. #include "Matrix.h"
  12.  
  13. int main(int argc, char* argv[]) {
  14.  
  15. // Declarations -------------------------------------
  16.  
  17. FILE *in, *out; // handlers for input/output files
  18. char line[256];
  19. int size = 0; // size of the matrix to be created
  20. int numLinesA = 0; // # of lines in Matrix A
  21. int numLinesB = 0; // # of lines in Matrix B
  22.  
  23. // Preliminary --------------------------------------
  24.  
  25. // check command line for correct number of arguments
  26. if (argc != 3) {
  27. printf("Usage: %s <input> <output>\n", argv[0]);
  28. exit(1);
  29. }
  30.  
  31. // open input file for reading
  32. in = fopen(argv[1], "r");
  33. if (in == NULL) {
  34. printf("Unable to read from file %s\n", argv[1]);
  35. exit(1);
  36. }
  37.  
  38. // open output file for writing
  39. out = fopen(argv[2], "w");
  40. if (out == NULL) {
  41. printf("Unable to write to file %s\n", argv[2]);
  42. exit(1);
  43. }
  44.  
  45. // Matrix Manipulation ------------------------------
  46.  
  47. fgets(line, sizeof(line), in);
  48. sscanf(line, "%d %d %d", &size, &numLinesA, &numLinesB); // reads first line of input file
  49. fgets(line, sizeof(line), in); // should read the blank line
  50.  
  51. // create matrices
  52. Matrix A = newMatrix(size);
  53. Matrix B = newMatrix(size);
  54.  
  55. readMatrix(in, A, numLinesA); // handles Matrix A
  56. fgets(line, sizeof(line), in); // should read the blank line
  57. readMatrix(in, B, numLinesB);// handles Matrix B
  58.  
  59. // print initial matrices (A and B)
  60. fprintf(out, "A has %d non-zero entries:\n", numLinesA); // numLinesA might be the wrong variable to use
  61. printMatrix(out, A);
  62. fprintf(out, "\n");
  63. fprintf(out, "B has %d non-zero entries:\n", numLinesB); // numLinesB might be the wrong variable to use
  64. printMatrix(out, B);
  65.  
  66. fprintf(out, "\n");
  67.  
  68. // print (1.5)*A
  69. fprintf(out, "(1.5)*A =\n");
  70. Matrix sMult = scalarMult(1.5, A);
  71. printMatrix(out, sMult);
  72. freeMatrix(&sMult);
  73.  
  74. fprintf(out, "\n");
  75.  
  76. // print A+B
  77. fprintf(out, "A+B =\n");
  78. Matrix sum1 = sum(A, B);
  79. printMatrix(out, sum1);
  80. freeMatrix(&sum1);
  81.  
  82. fprintf(out, "\n");
  83.  
  84. // print A+A
  85. fprintf(out, "A+A =\n");
  86. Matrix sum2 = sum(A, A);
  87. printMatrix(out, sum2);
  88. freeMatrix(&sum2);
  89.  
  90. fprintf(out, "\n");
  91.  
  92. // print B-A
  93. fprintf(out, "B-A =\n");
  94. Matrix diff1 = diff(B, A);
  95. printMatrix(out, diff1);
  96. freeMatrix(&diff1);
  97.  
  98. fprintf(out, "\n");
  99.  
  100. // print A-A (should output blank line)
  101. fprintf(out, "A-A =\n");
  102. Matrix diff2 = diff(A, A);
  103. printMatrix(out, diff2);
  104. freeMatrix(&diff2);
  105.  
  106. fprintf(out, "\n");
  107.  
  108. // print Transpose(A)
  109. fprintf(out, "Transpose(A) =\n");
  110. Matrix trans = transpose(A);
  111. printMatrix(out, trans);
  112. freeMatrix(&trans);
  113.  
  114. fprintf(out, "\n");
  115.  
  116. // print A*B
  117. fprintf(out, "A*B =\n");
  118. Matrix product1 = product(A, B);
  119. printMatrix(out, product1);
  120. freeMatrix(&product1);
  121.  
  122. fprintf(out, "\n");
  123.  
  124. // print B*B
  125. fprintf(out, "B*B =\n");
  126. Matrix product2 = product(B, B);
  127. printMatrix(out, product2);
  128. free(product2);
  129.  
  130. fprintf(out, "\n");
  131.  
  132. }
  133.  
  134. // helper method to read input files
  135. void readMatrix(FILE* in, Matrix M, int numLines) {
  136. char line[256];
  137. int rowNum;
  138. int colNum;
  139. double value;
  140. for (int i = 0; i < numLines; i++) {
  141. fgets(line, sizeof(line), in);
  142. sscanf(line, "%d %d %lf", &rowNum, &colNum, &value);
  143. changeEntry(M, rowNum, colNum, value);
  144. }
  145. }
Add Comment
Please, Sign In to add comment