Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   input file problem (http://www.go4expert.com/forums/input-file-problem-t27347/)

favorsight861993 13Dec2011 15:29

input file problem
 
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;
}


shabbir 13Dec2011 15:58

Re: input file problem
 
Please stop creating the same thread. I see it 4 times now.

xpi0t0s 14Dec2011 10:08

Re: input file problem
 
The program only reads two integers, so just use Notepad to create input.txt containing something like:
Code:

27 36


All times are GMT +5.5. The time now is 07:43.