2

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;
}}
muzzi
  • 372
  • 2
  • 13
  • If you want to check for `> -1` then `if (where >0 && what > 0)` is not correct implemented. IMHO, `if (where >= 0 && what >= 0)` would be better. ;-) – Scheff's Cat Aug 13 '18 at 07:30
  • `data[where] = what;` before checking `what` and `where` (in the next line) - this makes an out of bound access for `-1 -1`. You should swap these two lines, shouldn't you. – Scheff's Cat Aug 13 '18 at 07:32
  • I believe I understood for what `data` array is used. But, for what are `forward` and `backward` good for? – Scheff's Cat Aug 13 '18 at 07:34
  • 1
    Your code is plain wrong: you first write `data[where]` and only after test whether `where`could be -1 => write outside of the array guaranteed. Later you load `backward[where]` where `where` comes from the file with expected values of 7,9 and 4, and test `backward[i]` where `i` starts at 1... Too much inconsistency for me to try to fix... – Serge Ballesta Aug 13 '18 at 07:39
  • 2
    IMHO, there are (at least) two design issues in your code: 1st Why `for (i=1; i – Scheff's Cat Aug 13 '18 at 07:39
  • Btw. the array has two ends where writing beyond is out of range. The check for `where` should consider both ends e.g. `if (where < 0 || where >= SIZE) break; // illegal index`. – Scheff's Cat Aug 13 '18 at 07:43
  • `forward[valid_count]` is the position where it stores the index of the `data[where]` while `backward[where]` is the index of the forward array. – muzzi Aug 13 '18 at 10:05

2 Answers2

3

Try this instead, you have 1 loop to read in the file, followed by another loop to display the output. Without changing much, (except the formatting and variable names) this should give you the correct output. As your initial codes were printing the position of the current index(causing it to "jump" around), unlike the expected output which is printing in sequence. The algorithm you've implemented is not working in the way you are envisioning, but that's where you have to solve it yourself (:

int valid_count = 0;

for (int i = 1; i < SIZE; i++) {

    infile >> value >> pos;

    if(infile.fail())
       break;

    valid_count++;
    data[pos] = value;

    if (pos > 0 && value > 0) {   // to determine -1-1 sequence of the pair
        forward[valid_count] = pos;
        backward[pos] = valid_count;

    } // End of if-else
} // End of For-Loop

    //Display the array
for ( int j = 1; j < SIZE; j++ ) {

     if( (backward[i] > 0) && (backward[i] <= valid_count) && (forward[backward[i]]==i) )
         cout << "Position " << pos << " has been initialized to value " << value << endl;
     else
          cout << "Position " << pos << " has not been initialized. " << endl;   
}

Hope this helps!

Note: Array index should start at 0, not 1. There are many reasons why, some are listed here -> Why does the indexing start with zero in 'C'?

Matt
  • 76
  • 1
  • 10
  • Nice guess, but your code will pair every second line after the first -1, -1 to previous one. Unsure whether it is expected, but I'll assume that you have a pretty nice crytal ball... +1 anyway for your brave attempt. – Serge Ballesta Aug 13 '18 at 07:42
  • 1
    @SergeBallesta I'm pretty sure this is an assignment from school, thus i'm only guiding him with the right directions, not the answers (: He has to change the algo himself. – Matt Aug 13 '18 at 07:45
  • I have tried using two loops , one to read the data from file and one to see if the array element is invalid or not but this way is also not giving me the correct output. It gives me -1 for all the values – muzzi Aug 13 '18 at 10:00
  • That's where you have to look at your algorithm, how your array is being set & utilized. I've changed the variable names to something more meaningful to help you deduce the problem. You can start off with deciding if you need 3 arrays to solve this question. IMO 2 arrays should be fine – Matt Aug 13 '18 at 11:56
  • Another useful tip, if you can't visualize the output. Write every iteration out, from the input to the output. That's where you can visually see if your code is doing what you intend it to. – Matt Aug 13 '18 at 12:02
1
int what, where,probe;
while(1){
    infile >> what  >> where;
    if(what==-1 || where==-1)break;

    valid_count=valid_count+1;;

    data[where]=what;

    forward[valid_count]=where;
    backward[where]=valid_count;
}
while( infile >> probe )             //reading single value
{
  if(probe==-1)    // if -1 occured break
  {
    break;
  }

    if(backward[probe]>0 && backward[probe] <= valid_count && forward[backward[probe]]==probe)             //if true
        cout << "Position " << probe << " has been initialized to value " << data[probe] << "." << endl;
    else
        cout << "Position " << probe << " has not been initialized. " << endl;
}
muzzi
  • 382
  • 3
  • 10
  • 1
    This `(where >0 && what > 0)` way of checking for the value -1-1 won't work according to your requirement. See my coding . Hope it would work. – muzzi Aug 16 '18 at 07:34
  • 1
    Also, You need to read single value in order to print out the requirement. Because the entire concept of virtual initialization is to know the initialized value without reading every single element of the array. For loop wont work in this situation. – muzzi Aug 16 '18 at 07:36
  • 1
    I have removed `For loop` and added `probe` to read single value of the file and then I am printing out the element. – muzzi Aug 16 '18 at 07:37