Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct matrix_nfo {
- int **matrix;
- int size;
- int ssize;
- };
- // Read and return data in an array with size and search square size (first two values of the matrix file)
- int **readmatrix(char *file, struct matrix_nfo *mnfo) {
- int size = 0; // Size of the matrix; declared from the file later
- int ssize = 0; // Size of the search square; declared from the file later
- FILE *fd = fopen(file,"r"); // File descriptor to our matrix input.
- int strlen = 1; // Keeping track of the temporary string storage size.
- char *str = malloc(sizeof(char)*strlen); // Temporary string storage
- // Tracking ourselves while reading the matrix from the file.
- int rows = 0;
- int cols = 0;
- // Variables for tokenizing the data.
- char *token; // Next delimited string token
- char *toksave; // Where strtok_r stores some info
- char *tokstr; // Some other pointer that strtok_r makes use of
- // Where we'll be storing our character input
- char in;
- int x = 0; // Random counter variable
- while(!feof(fd)) {
- in = fgetc(fd);
- if(in != '\n') {
- str[strlen-1] = in;
- str = realloc(str,++strlen);
- } else {
- // printf("%s\n",str);
- for(tokstr=str; ; tokstr=NULL) {
- token = strtok_r(tokstr, "/", &toksave);
- if(token == NULL)
- break;
- if(rows == 0 && cols == 0) {
- size = atoi(token);
- (*mnfo).size = size;
- (*mnfo).matrix = malloc(sizeof(int *)*size);
- for(x=0; x<size; x++)
- (*mnfo).matrix[x] = malloc(0);
- } else if(rows == 0 && cols == 1) {
- ssize = atoi(token);
- (*mnfo).ssize = ssize;
- } else {
- (*mnfo).matrix[rows-1] = realloc((*mnfo).matrix[rows-1],sizeof(int)*cols);
- (*mnfo).matrix[rows-1][cols-1] = atoi(token);
- }
- cols++;
- }
- memset(str,0,strlen);
- strlen = 1;
- rows++;
- cols=1;
- }
- }
- return 0;
- }
- // Search through the matrix for the largest sum of numbers in given square in the matrix
- int matrix(int **matrix, int size, int ssize) {
- int posh,posw; // position (in the matrix) variable declaration
- posh = 0;
- posw = 0;
- int offh,offw; // offset (start of matrix) variable declaration
- offh = 0;
- offw = 0;
- int totalsum = 0;
- int totalsumtmp = 0;
- int oldposh = 0;
- int oldposw = 0;
- int lsh = 0;
- int lsw = 0;
- for(offh=0;offh<=size;offh++) {
- for(offw=0;offw<=size;offw++) {
- for(posh=offh;posh<=size-(size-ssize)+offh;posh++) {
- if((size-(size-ssize)+offh) > size)
- continue;
- for(posw=offw;posw<=size-(size-ssize)+offw;posw++) {
- if((size-(size-ssize)+offw) > size)
- continue;
- oldposh = posh;
- oldposw = posw;
- // printf("matrix[%d][%d] => %dx%d = %3.d\n",posh,posw,posh+1,posw+1,matrix[posh][posw]);
- totalsumtmp += matrix[posh][posw];
- }
- posw = 0; // reset the column position
- }
- // check largest here
- if(totalsum < totalsumtmp) {
- totalsum = totalsumtmp;
- lsh = oldposh;
- lsw = oldposw;
- // printf("new total is %d [%d:%d] \n",totalsum,lsh,lsw);
- }
- totalsumtmp = 0;
- }
- }
- // printf("oldposh %d oldposw %d lsh %d lsw %d\n",oldposh,oldposw,lsh,lsw);
- for(posh=lsh-ssize;posh<=(size-(size-ssize))+lsh-1;posh++) {
- for(posw=lsw-ssize;posw<=(size-(size-ssize))+lsh-1;posw++) {
- printf("matrix[%d][%d] => %dx%d = %5.d\n",posh,posw,posh+1,posw+1,matrix[posh][posw]);
- }
- posw = 0; // reset the column position
- }
- printf("Largest square of %dx%d dimensions has a sum of %d.\n",ssize+1,ssize+1,totalsum);
- return 0;
- }
Add Comment
Please, Sign In to add comment