-
2-d arrays and fscanf
alright, i have absolutly no idea what im doing here, as the concepts of pointers, dynamic memory and 2-d arrays are all new things to me
i have a file that i need to read into a 2-d array...in the end that array needs to be dynamically allocated, but for now, lets just say it is a static 10x10 array.
i can read in a single string just fine, but i cant seem to figure out how to read in the data to the multi dimentional array.
the input file is formated like this:
4
0 100
1 .004
2 54
where the first line is how many lines the file is, the first number of each line represents the line number and the second number in each line represents the data that i need to read in.
any help at all would be apprecated....
-
Do you know how to print each element of a single dimension array? If not, start there.
Do you know how to read into each element of a single dimensaion array? If not, start there.
Do you know how to print each element of a single dimension array? If not, start there.
Post your attempt of whatever stumps you, and we'll lend a hand.
Quzah.
-
yes, i can print each element of a single dimension array
Code:
int x[10] = "adsfad";
printf("%c\n",x[3]);
and this is my best attempt at reading in a single element at a time
Code:
FILE *input;
int array[10];
int i;
myinput = fopen("file","r");
for(i=0;i<11;i++){
fscanf(input,"%c",array[i]);
}
im pretty comfortable working with one-d strings....its just when moving into tow dimensions things seem to get tricky.
just an overall idea of whats going on is what im looking for...i want to write this myself, but ive tried everything i can think of, so i came here for help.
-
myinput should be input....my bad, im getting the variables im using in the project confused with the variables i wanted to use online.
-
and it should be %i not %c.
sorry...im trying to frantically write code and get help from about a million different sources at once.
-
Do you know how to display the contents of a two dimensional array?
Code:
int array[ 3 ][ 2 ] =
{
{ 1, 2 },
{ 3, 4 },
{ 5, 6 }
};
int y, x;
for( y = 0; y < 3; y++ ) /* each row... */
{
for( x = 0; x < 2; x++ ) /* each column... */
{
printf("array[ %d ][ %d ] is %d\n", y, x, array[ y ][ x ] );
}
}
Now see if you can either write one of your own to do something similar, with a different number of rows and columns, for practice; or try and modify this to read into each part of the array.
Quzah.