c - Why does my fscanf not read? -


i have write program finds path of maximum value in triangular maze of integer numbers.

  • this program should calculate highest sum of numbers passed on route starts @ top , ends somewhere on base.
  • each step can go either diagonally down left or diagonally down right.
  • the number of rows in triangle > 1 <= 100.
  • the numbers in triangle, integers, between 0 , 99.

i have problem fscanf function. doesn't read reason. highly appreciate or advise project due asap.

here example of maze:

30

47 1

35 65 21

0 74 94 58

71 29 34 28 60

97 6 29 19 26 68

37 1 48 98 57 89 64

60 38 33 23 49 57 19 50

4 83 52 47 84 60 16 56 90

19 59 6 10 97 47 96 93 59 50

and i've got far:

/#include <stdio.h> /#include <stdlib.h> /#define max 100  void read    (int maze [][max]); int findpath (int maze[][max], int map[][max], int size); void print   (int map [][max]);  int main() {     int maze [][max] = {};     int map  [][max] = {};      int sum = 0;     int size = max;      read     (maze);     findpath (maze, map,size);     print    (map);      return; }  void read (int maze[][max]) {     file * mazefile;     int num, r, c, count;      if ((mazefile = fopen ("t4.txt", "r")) == null)     {         printf ("error opening file\n");     }     else     {         while (mazefile != eof)         {                     fscanf (mazefile, "%d", &maze[r][c]);              (r = 0; r < 100 ; r++)             {                 count = r + 1;                 (c = 0; c <= count; c++)                 {                     printf ("(%d, %d) = %d\n",r, c, maze[r][c]);                 }             }             fclose (mazefile);             return;         }     } }  int findpath (int maze[][max], int map[][max], int size) {     int sum [max][max] = {0};     int row, col, maxnum;      for(row=(size-1); row >= 1; --row)     {         (col-row;col>=1;--col)         {             maxnum = (sum[row+1][col] > sum [row+1][col+1] ? col : col + 1);             sum[row][col]= maze[row][col] + sum [row+1][maxnum];              map[row][col] = maxnum;         }     }       return sum [0][0]; }  void print (int map [][max]) {     printf ("(%d, %d) = ", map[0][0], map[0][1]);     return; } 

well :-) there lot of issues 1 ask reading file address now:

some corrections

  • first not use read, write, standard library functions run lots of issues;
  • do not use void return type functions, use int , return 0 on success , negative numbers failure (it's conventional) , helps write code catches errors
  • do use comments describe function
  • tabs good, @ least 4 spaces ( kernel programmer 8-space tabs) when sleepy @ 2:30am , trying figure out answers
  • all statements inside function should tabbed in , not line { }
  • a convention use opening { on newline function body , rest if, for, while etc { should on same line statement , closing } should line first letter of statement.
  • do not put many white spaces between each statement. makes code less readable.

example of approach

here example of how code reading file properly.

step 1:

create simple function reads contents , dumps out know reading fine.

#include <stdio.h>  /**   * @function: read_maze  * @desc: reads maze file , dumps stdout,   * @return: returns 0 on success, -1 on failure  *  */  int read_maze(const char *filename)  {      file *fp;      int entry;       fp = fopen(filename, "r");      if ( !fp ) {           perror(filename); /* prints system error related                              * problem reading mazefile                               */           return -1;      }       while(!feof(fp)) {           if (fscanf(fp, "%d", &entry) == 1) {                printf ("%d\n", entry);           }      }       return 0; }  int main() {      if(read_maze("t4.txt")) {           return -1;      }       return 0; } 

step 2:

create triangular maze array makes sense:

okay know can read file let's try , put structure makes sense.

we know have bunch of rows (100), 1st row has 1 column, 2nd row has 2, 3rd row 3, , on...

in other words each row has same number of columns cardinality (index+1);

so following?

int row0[1]; int row1[2]; int row2[3];    .    .    . int row99[100];  

that's kinda ugly , not programmatic; nice automatically; know fixed number number of rows , dynamic allocation associated index. let's make array of pointers integers; , each member of array can dynamically allocated memory block big cardinality of member. (mouthful hehe)

#define maxrows 100  int *row[maxrows];  

note: conceptually maxrows corresponds hardcoded 0-99 stuck in front word row , not declaration piece of array size in brackets. int row55[56]; declaring 55; 56 declaration come malloc later;

now know pointer data type that, pointer point memory blocks our data (essentially acting array) in other words let's allocate appropriate columns each row:

 /* starting beginning */   #define maxrows 100   int *rows[maxrows];   int init_triangular_array()   {      int k;      memset(rows, 0, sizeof(int *)*maxrows); /* make rows array zeros */      (k = 0; k < maxrows; k++) {          rows[k] = (int *)calloc(k+1, sizeof(int)); /* cardinality */          if( rows[k] == null ) {               fprintf(stderr, "unable allocate memory, quoting\n");               exit(-1); /* kill program */          }      }  } 

now have function initialize array ... let's create function free array when done it; that's coding practice.

 void free_triangular_array()  {      int k;      (k = 0; k  < maxrows; k++ ) {          if ( rows[k] )                free(rows[k]);      }  } 

and lets write function fill our file:

 int fill_triangular_array(const char *filename)  {         file *fp = fopen(...);        int row = 0, col = 0;         while(!feof(fp)) {              (col = 0; col <= row_number; col++ ) {                    // read entry above                     rows[row][col] = entry;               }        }  } 

you fill in missing bits.

and once you've done use

 int main()   {           init_triangular_array();          fill_triangular_array();          /* array */          free_triangular_array();   } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -