/* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Author: Marco Vitetta E-mail: WebSite: http://ervito.altervista.org */ #include #include int **mktabi(FILE *SOURCE_FILE_TABLE, int X_SIZE_TABLE, int Y_SIZE_TABLE); int main() { FILE *fp; int i,j,k,l,max=-1,prd,**tab; fp=fopen("src","r"); tab=mktabi(fp,20,20); if(tab!=NULL) { // Testing rows. for(i=0;i<20;i++) for(j=k=0;j<20;j++) { l=j; prd=tab[i][j]; while(++l<20 && k<3) { k++; prd*=tab[i][l]; } if(prd>max) max=prd; } // Testing columns. for(i=0;i<20;i++) for(j=k=0;j<20;j++) { l=j; prd=tab[j][i]; while(++l<20 && k<3) { k++; prd*=tab[l][i]; } if(prd>max) max=prd; } // Testing \ diagonal. for(i=0;i+4<20;i++) for(k=0;k+4<20;k++) { j=i+4; l=k+4; prd=tab[j][l]; while(j>i+1) prd*=tab[--j][--l]; if(prd>max) max=prd; } // Testing / diagonal. for(i=20-1;i-4>=0;i--) for(k=0;k+4<=20;k++) { j=i; l=k; prd=tab[j][l]; while(j>i-3) prd*=tab[--j][++l]; if(prd>max) max=prd; } printf("\n\tmax: %i\n\n",max); } } int **mktabi(FILE *SOURCE_FILE_TABLE, int X_SIZE_TABLE, int Y_SIZE_TABLE) { if(SOURCE_FILE_TABLE != NULL) { char string[13]; // In counting 13 chars the following have been included: // - 10 digits (from 0 to 2147483647); // - any sign; // - the \0 that will overwrite spaces and \n; // - one more space to check if the read value // doesn't exceed the range. int col,i,row,**table; /************************************************************************************************/ /** Table allocation. **/ /**/ /**/ /**/ table=malloc(Y_SIZE_TABLE*sizeof(int *)); /**/ /**/ // ^ This is really important because i've /**/ /**/ // to allocate an array of POINTERS (to /**/ /**/ // other arraies). /**/ /**/ for(col=row=0;row47 && string[i]<58 && i<12)); /**/ /**/ // ^ These values ^ /**/ /**/ // are respectively the character /**/ /**/ // before 0 and the one after 9. /**/ /**/ /**/ /************************************************************************************************/ if(i<12)// With the condition above it's impossible to receive { // a void string. Even if it would be possible, there // is no way to manage a symbolic value indicating an // invalid status. char *limit="2147483648"; int flag=0; switch(string[0]) { case '+':limit[9]--; case '-': { if(i==11) { // Values control. for(i=0;i<10 && flag==0;i++) if(string[i+1]limit[i]) flag=1; i=12; } }break; default: { if(i==10) { /*************************************************************************************************/ /** Values control. **/ /** This cycle checks if the value (10 */ /**/ /** digits long) contained in the string */ for(i=0;i<10 && flag==0;i++) /**/ /** could be converted into int or not. */ if(string[i]limit[i]) /**/ /** one (with also a length control). */ flag=1; /**/ /** */ i=10; /**/ /*************************************************************************************************/ } else if(i>10) flag=1; } } if(flag>0) i=0; string[i]='\0'; table[row][col]=atoi(string); } if(++col>=X_SIZE_TABLE) { col=0; row++; } } return table; } else { printf("\n\n\tError: the file hasn't been opened.\n\n"); return NULL; } }