I want to read integers through file. The first int is the value which is what
and the second int is 'where' it goes in the array. When compiler reads -1 -1 in file, it should be the end of the array data. The rest of the file data is for looking up array entries. I have globally initialized data[SIZE]
, forward[SIZE]
and backward[SIZE]
arrays with `SIZE=100'. My output is wrong. I am not sure what is wrong with the code or what is my mistake. I am not allowed to use STL , classess or structs.
A sample input file contains data like:
42 7
93 9
11 4
‐1 ‐1
7
8
9
88
‐1
For which the output would be:
Position 7 has been initialized to value 42.
Position 8 has not been initialized.
Position 9 has been initialized to value 93.
My output:
Position 7 has not been initialized.
Position 9 has not been initialized.
Position 4 has been initialized to value 11
Position 8 has not been initialized.
Position 88 has been initialized to value 9
My code:
int i, valid_count=0;
for (i=1; i<SIZE; i++){
infile >> what >> where;
if(infile.fail())break;
valid_count=valid_count +1;
data[where]=what;
if (where >0 && what > 0){ // to determine -1-1 sequence of the pair
forward[valid_count]=where;
backward[where]=valid_count;
if(backward[i]>0 && backward[i] <= valid_count && forward[backward[i]]==i)
cout << "Position " << where << " has been initialized to value " << what << endl;
else
cout << "Position " << where << " has not been initialized. " << endl;
}}