Guest User

Untitled

a guest
Jul 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. struct matrix_nfo {
  2. int **matrix;
  3. int size;
  4. int ssize;
  5. };
  6.  
  7. // Read and return data in an array with size and search square size (first two values of the matrix file)
  8. int **readmatrix(char *file, struct matrix_nfo *mnfo) {
  9.  
  10. int size = 0; // Size of the matrix; declared from the file later
  11. int ssize = 0; // Size of the search square; declared from the file later
  12.  
  13. FILE *fd = fopen(file,"r"); // File descriptor to our matrix input.
  14.  
  15. int strlen = 1; // Keeping track of the temporary string storage size.
  16. char *str = malloc(sizeof(char)*strlen); // Temporary string storage
  17.  
  18. // Tracking ourselves while reading the matrix from the file.
  19. int rows = 0;
  20. int cols = 0;
  21.  
  22. // Variables for tokenizing the data.
  23. char *token; // Next delimited string token
  24. char *toksave; // Where strtok_r stores some info
  25. char *tokstr; // Some other pointer that strtok_r makes use of
  26.  
  27. // Where we'll be storing our character input
  28. char in;
  29.  
  30. int x = 0; // Random counter variable
  31.  
  32. while(!feof(fd)) {
  33. in = fgetc(fd);
  34. if(in != '\n') {
  35. str[strlen-1] = in;
  36. str = realloc(str,++strlen);
  37. } else {
  38. // printf("%s\n",str);
  39. for(tokstr=str; ; tokstr=NULL) {
  40. token = strtok_r(tokstr, "/", &toksave);
  41. if(token == NULL)
  42. break;
  43. if(rows == 0 && cols == 0) {
  44. size = atoi(token);
  45. (*mnfo).size = size;
  46. (*mnfo).matrix = malloc(sizeof(int *)*size);
  47. for(x=0; x<size; x++)
  48. (*mnfo).matrix[x] = malloc(0);
  49. } else if(rows == 0 && cols == 1) {
  50. ssize = atoi(token);
  51. (*mnfo).ssize = ssize;
  52. } else {
  53. (*mnfo).matrix[rows-1] = realloc((*mnfo).matrix[rows-1],sizeof(int)*cols);
  54. (*mnfo).matrix[rows-1][cols-1] = atoi(token);
  55. }
  56. cols++;
  57. }
  58. memset(str,0,strlen);
  59. strlen = 1;
  60. rows++;
  61. cols=1;
  62. }
  63. }
  64. return 0;
  65. }
  66.  
  67. // Search through the matrix for the largest sum of numbers in given square in the matrix
  68. int matrix(int **matrix, int size, int ssize) {
  69.  
  70. int posh,posw; // position (in the matrix) variable declaration
  71. posh = 0;
  72. posw = 0;
  73.  
  74. int offh,offw; // offset (start of matrix) variable declaration
  75. offh = 0;
  76. offw = 0;
  77.  
  78. int totalsum = 0;
  79. int totalsumtmp = 0;
  80. int oldposh = 0;
  81. int oldposw = 0;
  82. int lsh = 0;
  83. int lsw = 0;
  84.  
  85. for(offh=0;offh<=size;offh++) {
  86. for(offw=0;offw<=size;offw++) {
  87. for(posh=offh;posh<=size-(size-ssize)+offh;posh++) {
  88. if((size-(size-ssize)+offh) > size)
  89. continue;
  90. for(posw=offw;posw<=size-(size-ssize)+offw;posw++) {
  91. if((size-(size-ssize)+offw) > size)
  92. continue;
  93. oldposh = posh;
  94. oldposw = posw;
  95. // printf("matrix[%d][%d] => %dx%d = %3.d\n",posh,posw,posh+1,posw+1,matrix[posh][posw]);
  96. totalsumtmp += matrix[posh][posw];
  97. }
  98. posw = 0; // reset the column position
  99. }
  100. // check largest here
  101. if(totalsum < totalsumtmp) {
  102. totalsum = totalsumtmp;
  103. lsh = oldposh;
  104. lsw = oldposw;
  105. // printf("new total is %d [%d:%d] \n",totalsum,lsh,lsw);
  106. }
  107. totalsumtmp = 0;
  108. }
  109. }
  110. // printf("oldposh %d oldposw %d lsh %d lsw %d\n",oldposh,oldposw,lsh,lsw);
  111. for(posh=lsh-ssize;posh<=(size-(size-ssize))+lsh-1;posh++) {
  112. for(posw=lsw-ssize;posw<=(size-(size-ssize))+lsh-1;posw++) {
  113. printf("matrix[%d][%d] => %dx%d = %5.d\n",posh,posw,posh+1,posw+1,matrix[posh][posw]);
  114. }
  115. posw = 0; // reset the column position
  116. }
  117. printf("Largest square of %dx%d dimensions has a sum of %d.\n",ssize+1,ssize+1,totalsum);
  118. return 0;
  119. }
Add Comment
Please, Sign In to add comment