1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

input file problem

Discussion in 'C' started by favorsight861993, Dec 13, 2011.

  1. favorsight861993

    favorsight861993 Banned

    Joined:
    Dec 13, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I write a code for a problem. My roblem is:
    Enter n x m integers from text file
    Arrange above integers on the way from from outside to inside in a certain order of the matrix nx m.
    Export results to a text file.
    I don't know how to make "input.txt" to make the program run perfectly. If u can, please send me ur file "input.txt"
    Here is my code:

    Code:
    #include <stdio.h>
    
    #define MAXN 110
    
    int a[MAXN][MAXN], m, n;
    
    void input(){
        scanf("%d %d", &n, &m);
    }
    
    void initial(){
        int i;
        for (i = 0; i<=n+1; ++i){
            a[i][0]=-1;
            a[i][m+1]=-1;
        }
        for (i = 0; i<=m+1; ++i){
            a[0][i]=-1;
            a[n+1][i]=-1;
        }
    }
    
    void solve(){
        int dx[] = {-1, 0 , 1, 0};
        int dy[] = {0, 1, 0, -1};
        int tx = 1, ty = 1, dir = 0, i;
        a[1][1] = 1;
        for (i = 2; i<=n*m; ++i){
            while (a[tx+dx[dir]][ty+dy[dir]]){
                dir = (dir+1)%4;
            }
            tx = tx + dx[dir];
            ty = ty + dy[dir];
            a[tx][ty] = i;
        }
    }
    
    void output(){
        int i, j;
        for (i = 1; i<=n; ++i){
            for (j = 1; j<=m; ++j){
                printf("%4d ", a[i][j]);
            }
            printf("\n");
        }
    }
    
    int main(){
        freopen("D:\input.txt", "r", stdin);
        freopen("D:\output.txt", "w", stdout);
        input();
        initial();
        solve();
        
        output();
        return 0;
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Please stop creating the same thread. I see it 4 times now.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    The program only reads two integers, so just use Notepad to create input.txt containing something like:
    Code:
    27 36
    
     

Share This Page