pa08.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Do not modify this file
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "pa08.h"
  7. int main ( int argc , char ** argv )
  8. {
  9. int i; //index
  10. //check arguments
  11. if (argc != 3)
  12. {
  13. printf("usage: ./pa04 <input file> <output file>\n");
  14. return EXIT_FAILURE;
  15. }
  16. //initializing input file
  17. FILE * fptr = NULL;
  18. fptr = fopen(argv[1], "r");
  19. if (fptr == NULL)
  20. {
  21. printf("File error!\n");
  22. return EXIT_FAILURE;
  23. }
  24. /**********************************
  25. //initialize array 1
  26. **********************************/
  27. int number_of_value_1 = 0;
  28. fscanf(fptr, "%d", &number_of_value_1);
  29. printf("\nlength: %d\n", number_of_value_1);
  30. int* indices_1 = malloc(sizeof(int)* number_of_value_1);
  31. if (indices_1 == NULL)
  32. {
  33. printf("Array_1 indices malloc error!\n");
  34. return EXIT_FAILURE;
  35. }
  36. int* values_1 = malloc(sizeof(int)* number_of_value_1);
  37. if (values_1 == NULL)
  38. {
  39. printf("Array_1 values malloc error!\n");
  40. return EXIT_FAILURE;
  41. }
  42. printf("indices: ");
  43. for (i = 0; i < number_of_value_1; i++)
  44. {
  45. fscanf(fptr, "%d", &indices_1[i]);
  46. printf("%d ", indices_1[i]);
  47. }
  48. printf("\nvalues: ");
  49. for (i = 0; i < number_of_value_1; i++)
  50. {
  51. fscanf(fptr, "%d", &values_1[i]);
  52. printf("%d ", values_1[i]);
  53. }
  54. printf("\n");
  55. SparseNode * array_1 = NULL ;
  56. array_1 = SparseArray_build(indices_1, values_1, number_of_value_1);
  57. /************************************
  58. //initialize array 2
  59. ************************************/
  60. int number_of_value_2 = 0;
  61. fscanf(fptr, "%d", &number_of_value_2);
  62. printf("\nlength: %d\n", number_of_value_2);
  63. int* indices_2 = malloc(sizeof(int)* number_of_value_2);
  64. if (indices_2 == NULL)
  65. {
  66. printf("Array_2 indices malloc error!\n");
  67. return EXIT_FAILURE;
  68. }
  69. int* values_2 = malloc(sizeof(int)* number_of_value_2);
  70. if (values_2 == NULL)
  71. {
  72. printf("Array_2 values malloc error!\n");
  73. return EXIT_FAILURE;
  74. }
  75. printf("indices: ");
  76. for (i = 0; i < number_of_value_2; i++)
  77. {
  78. fscanf(fptr, "%d", &indices_2[i]);
  79. printf("%d ", indices_2[i]);
  80. }
  81. printf("\nvalues: ");
  82. for (i = 0; i < number_of_value_2; i++)
  83. {
  84. fscanf(fptr, "%d", &values_2[i]);
  85. printf("%d ", values_2[i]);
  86. }
  87. printf("\n");
  88. SparseNode * array_2 = NULL ;
  89. array_2 = SparseArray_build(indices_2, values_2, number_of_value_2);
  90. //finilizing the initialization step
  91. fclose(fptr);
  92. free(indices_1);
  93. free(values_1);
  94. free(indices_2);
  95. free(values_2);
  96. /***********************************
  97. ***********************************
  98. * Printing results
  99. ***********************************
  100. **********************************/
  101. SparseNode * onenode = NULL;
  102. fptr = fopen(argv[2], "w");
  103. if (fptr == NULL){
  104. printf("output file error\n");
  105. return EXIT_FAILURE;
  106. }
  107. /********************************************************
  108. * array 1 and array 2
  109. ********************************************************/
  110. printf("*******************************\n");
  111. printf("Array 1\n");
  112. printf("*******************************\n");
  113. if (array_1 != NULL)
  114. {
  115. for (i = SparseArray_getMin(array_1);
  116. i <= SparseArray_getMax(array_1); i++)
  117. {
  118. onenode = SparseArray_getNode(array_1,i);
  119. if (onenode != NULL)
  120. {
  121. printf("%5d: %6d\n" , i , onenode->value);
  122. }
  123. }
  124. }
  125. printf("\n*******************************\n");
  126. printf("Array 2\n");
  127. printf("*******************************\n");
  128. if (array_2 != NULL){
  129. for (i = SparseArray_getMin(array_2);
  130. i <= SparseArray_getMax(array_2); i++)
  131. {
  132. onenode = SparseArray_getNode(array_2,i);
  133. if (onenode != NULL)
  134. {
  135. printf("%5d: %6d\n" , i , onenode->value);
  136. }
  137. }
  138. }
  139. printf("\n*******************************\n");
  140. /********************************************************
  141. * copy array
  142. ********************************************************/
  143. SparseNode * copy = SparseArray_copy(array_1);
  144. printf("Array copy\n");
  145. printf("*******************************\n");
  146. if (copy != NULL){
  147. for (i = SparseArray_getMin(copy);
  148. i <= SparseArray_getMax(copy); i++)
  149. {
  150. onenode = SparseArray_getNode(copy,i);
  151. if (onenode != NULL)
  152. {
  153. printf("%5d: %6d\n" , i , onenode->value);
  154. }
  155. }
  156. }
  157. printf("\n*******************************\n");
  158. /********************************************************
  159. * doing addition
  160. ********************************************************/
  161. SparseNode * array_new = NULL;
  162. array_new = SparseArray_merge(array_1, array_2);
  163. printf("Array result\n");
  164. printf("*******************************\n");
  165. if (array_new != NULL){
  166. for (i = SparseArray_getMin(array_new);
  167. i <= SparseArray_getMax(array_new); i++) {
  168. onenode = SparseArray_getNode(array_new,i);
  169. if (onenode != NULL)
  170. {
  171. printf("%5d: %6d\n" , i , onenode->value);
  172. fprintf(fptr, "%5d: %6d\n" , i , onenode->value);
  173. }
  174. }
  175. }
  176. printf("\n*******************************\n");
  177. SparseArray_destroy ( array_1 );
  178. SparseArray_destroy ( array_2 );
  179. SparseArray_destroy ( array_new );
  180. SparseArray_destroy ( copy );
  181. fclose(fptr);
  182. return EXIT_SUCCESS ;
  183. }