#define X 5 //these are the dimensions of the array that will
#define Y 3 //hold the values.
#define Z 20
void ExtractArray( int a[X][Y][Z], ifstream& in);
//your code here
/* pre-condition: (a) is pointing to an enough allocated memory.
(in) is connected to the input file.
post-condition: (a) contains the extracted array. */
void ExtractArray( int a[X][Y][Z], ifstream& in)
{
for(int x = 0 ; x < X ; x++)
{
for(int y = 0 ; y < Y ; y++)
{
for(int z = 0 ; z < Z ; z++)
{
while(in.peek() < '0' || in.peek() > '9')
{
in.ignore(); //ignore everything other than a digit.
}
a[x][y][z] = in.get() - '0'; //extract the digit, and transform
//it from ascii to value.
}
}
}
}
i think this should be enough for you to complete this journey.
I have one more question. Those lines with words contain performance measures (like solve time, error bound), so they also contain numbers. So to set to pointer to where I want I'm doing this, because I know there are 14 lines before "x=":
Yes. For the singe digits, I'm using the following modification of the code above:
1 2 3 4 5 6 7 8 9 10 11 12
int ExtractArraySmall2( int room, ifstream& in)
{
while(in.peek() < '0' || in.peek() > '9')
{
in.ignore(); //ignore everything other than a digit.
}
room = in.get() - '0'; //extract the digit, and transform
//it from ascii to value.
return room;
}
This takes one digit at a time, so I don't know how to read the double digit numbers in.
While I was working on this, I came across another problem. The X, Y, Z numbers that are defined at the beginning are things that I would like to read in from the data file (X=P, Y=R, Z=T), but when I do that this error comes up: expression must have a constant value; because these are the sizes of the arrays. Is there any way to go around that?
int number = 0;
while(in.peek()>='0' && in.peek() <= '9')
{
char temp = in.get(); // extract one byte of the number.
temp -= '0'; // convert it from ASCII value to binary value.
number |= temp; // paste the extracted byte on the low order byte of number.
number <<= temp; //shift number 8 bits left to clear space for the next byte.
}
number >>= 8; // after the loop breaks, number is shifted 8bits
//left more than needed, so correct that.
after the algorithm finishes, number will contain one number of the source stream.
EDIT - PS:
this algorithm shall be changed to be used with different encodings, like little indian.
also, if the file already contains the number as a binary value, you would delete line 5.
Thank you very much, but it did not give the right number (which is 20). Using the following checkpoints it gave me this
1 2 3 4 5 6 7 8 9 10 11 12 13
while(in.peek()>='0' && in.peek() <= '9')
{
char temp = in.get(); // extract one byte of the number.
temp -= '0'; // convert it from ASCII value to binary value.
number |= temp; // paste the extracted byte on the low order byte of number.
cout<<number<<" one\n";
number <<= temp; //shift number 8 bits left to clear space for the next byte.
cout<<number<<" two\n";
}
cout<<number<<" three\n";
number >>= 8; // after the loop breaks, number is shifted 8bits
//left more than needed, so correct that.
cout<<number<<" four\n";