 |
|
|
|
 |
AnotherWay83
The B00b Maintenance Guy™

Registered: Aug 2000
Location: land of d(-_-)b
|
|
|
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
|
|
Mar-05-2003 06:21
|
|
|
 |
 |
|  |
All times are GMT. The time now is 06:55.
Forum Rules:
You may not post new threads
You may not post replies
You may not edit your posts
|
HTML code is ON
vB code is ON
[IMG] code is ON
|
|
|
|
|
|
Contact Us - return to tranceaddict
Powered by: Trance Music & vBulletin Forums
Copyright ©2000-2026, Jelsoft Enterprises Ltd.
Privacy Statement / DMCA
|