|
from ur post it looks like u're typing in sumthing like this at the command prompt (assume the program is called 'matrix', and data.txt is the file containing the information)
matrix < data.txt
and the information in data.txt is formatted like so :
//first line of file
thats the first line of the text file, and then, using the information u now have about the matrix (after scanning those in), u can set up a loop statement (perhaps even a nested loop) to get all the elements of the matrix...but instead of a multidimensional array, u wish to scan them all consecutively into a one dimensional array, so that, in effect, a data file in which the information is like this:
2 2
01
10
will end up giving u a one-dimensional matrix, with size 4, (lets call it 'matr') whose elements are: 0,1,1,0
in that order.
here's how i would go abt it:
#include
int *arr; //will hold the elements of the matrix
main() {
int a,b; //these hold dimensions of matrix
scanf(" %d",&a); //note the space, it is intentional
scanf(" %d",&b);
a*=b; //total number of elements
//allocate space
arr=(int *) malloc((a+1)*sizeof(int)); //(a+1) to put in '\0' at end
b=0;
while(b
scanf(" %1d",arr+b++); //each element is only 1 digit long
}
*(arr+a)='\0';
b=0;
while (b
printf("%d ",*(arr+b++));
}
}//end main
that program will work only for single digit entries...i havent built in any error trapping into it, tho
to use it, create a data file (in the same folder as the exe of the program), and if ur program is named 'matrix', type in this at the command prompt:
matrix < data.txt
in place of data.txt put the name of ur data file
hth
|