1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Trouble printing to a file C++

  1. Jul 17, 2010 #1
    it prints fine for the first generation, but nothing else after that. i am thinking that my problem lies in the cellStatus function and its implementation within the printCell function. attached are the files that i am getting input from and the file that i am printing to.

    Code (Text):

    #include <iostream>
    using namespace std;

    int main(){
         cout << "Resolved." << endl;
         cout << "Thanks for the help!" << endl;
    }
     
     
    Last edited: Jul 17, 2010
  2. jcsd
  3. Jul 17, 2010 #2
    you are not using inf and outf in either of your functions; if newLife is suppose to be printed to the file you aren't send it
     
    Last edited: Jul 17, 2010
  4. Jul 17, 2010 #3
    changing the inStream to inf & outStream to outf doesn't change anything when i execute it
     
  5. Jul 17, 2010 #4
    my question is how to do so
     
  6. Jul 17, 2010 #5
    outStream << NewLife[row][col] is missing; i only pointed out the inf and outf because it would cause your teacher to question why pass if you aren't using it and mark off because of it
     
  7. Jul 17, 2010 #6
    hmm, that didn't work. i assigned newLife[row][col] to be life[row][col] in order to print it, but there's still something wrong
     
  8. Jul 17, 2010 #7

    Mark44

    Staff: Mentor

    It looks to me like this code is backwards:
    Code (Text):

     // overwrite the old life to the new life
     for (int row = 0; row < rowSize; row++)
    {
        for (int col = 0; col < colSize; col++)
       {
          newLife[row][col] = life[row][col];
       }
    }
    This is in your printCell function. The code before it calculated values for your newLife array; now you are overwriting the values. Seems to me you would want to store the new calculated values (newLife) into the life array, not the other way around as you are doing above.
     
  9. Jul 17, 2010 #8
    i changed that, too, but i still have the same problem.
     
  10. Jul 17, 2010 #9
    could the problem be the beginning of my printCell function? since the series of 0's and 1's only determines the first generation, this might affect the printGame function. i'm calling the printCell for every generation, but after generation 0, there are no more 0's and 1's to read in.
     
  11. Jul 17, 2010 #10

    Mark44

    Staff: Mentor

    That makes sense. You don't have enough comments for me to easily figure out the purpose of this function, but you shouldn't be passing an input stream to it. You have already read in the initial configuration, in printGame. Once the initial configuration is read in, you have no further use for it.

    It seems that printCell (which is supposed to print a particular generation, I think - you should make that clear in a comment) should have two parameters: an output stream and a whatever matrix you want to print to the output file.
     
  12. Jul 17, 2010 #11
    i figured it out! thanks!
     
  13. Jul 17, 2010 #12

    Mark44

    Staff: Mentor

    Cool! What did you do to fix things?
     
  14. Jul 17, 2010 #13
    i added the 2D array parameter to the function and created a separate function for the loop that actually prints the asterisks and spaces
     
  15. Jul 19, 2010 #14
    i just thought of another question.. when counting the neighbors, will the program run into an error when it is checking for the neighbors surrounding the cells in row 0 or column 0?
     
  16. Jul 19, 2010 #15
    yeah. Here is some code from a "count the mines around each cell" program I had to write a while ago. In the program, I had a row by column character double array with '.' representing an empty cell and with '*' representing a mine. Here is the code I wrote to tally up how many adjacent mines each cell had.

    Code (Text):
        for(row = 0; row < rows; ++row)
            for(column = 0; column < columns; ++column)
            {
                if(board[row][column] == '.')
                {
                    adjacentMines = 0;
                    for(right = -1; right < 2; ++right)
                        for(up = -1; up < 2; ++up)
                            if(right || up)
                                if(row + right > -1 && column + up > -1)
                                    if(board[row + right][column + up] == '*')
                                        ++adjacentMines;
                    if(adjacentMines)
                        board[row][column] = adjacentMines + 48;
                }
            }
     
  17. Jul 20, 2010 #16
    so to fix my code i just need to add more for loops restricting the columns and rows in the -1 index?
     
  18. Jul 20, 2010 #17

    Mark44

    Staff: Mentor

    I wouldn't do that, but I would treat as special cases
    When a cell is at one of the corners.
    When a cell is in the first or last row.
    When a cell is in the first or last column.

    Cells in the corners have only three neighbors. Cells in the first or last row (that aren't corner cells) have only five neighbors. Same for cells in the first or last column that aren't in the corners.

    All other cells have eight neighbors.

    You don't need another for loop (or similar control structure) to deal with this - you can take care of it with an if ... else if ... else structure.
     
  19. Jul 20, 2010 #18
    i am trying to fix my code, but for some reason there is an asterisk even where there is no occupied cell. can someone catch my mistake, please?

    Code (Text):

    /* [row-1][col-1]           [row-1][col]    [row-1][col+1]
        [row][col-1]            [row][col]          [row][col+1]
        [row+1][col-1]      [row+1][col]    [row+1][col+1] */

    if (row == 0){
        if (cell[row][col+1] == 1) neighbors++;
        if (cell[row][col-1] == 1) neighbors++;
        if (cell[row+1][col+1] == 1) neighbors++;
        if (cell[row+1][col] == 1) neighbors++;
        if (cell[row+1][col-1] == 1) neighbors++;
    }
    else if (row == 0 && col == 0){
        if (cell[row][col+1] == 1) neighbors++;
        if (cell[row+1][col] == 1) neighbors++;
        if (cell[row+1][col+1] == 1) neighbors++;
    }
    else if (row == 0 && col == colSize){
        if (cell[row][col-1] == 1) neighbors++;
        if (cell[row+1][col] == 1) neighbors++;
        if (cell[row+1][col-1] == 1) neighbors++;
    }
    else if (row == rowSize && col == 0){
        if (cell[row-1][col] == 1) neighbors++;
        if (cell[row-1][col+1] == 1) neighbors++;
        if (cell[row][col+1] == 1) neighbors++;
    }
    else if (row == rowSize && col == colSize){
        if (cell[row][col-1] == 1) neighbors++;
        if (cell[row-1][col] == 1) neighbors++;
        if (cell[row-1][col-1] == 1) neighbors++;
    }
    else if (row == rowSize){
        if (cell[row][col+1] == 1) neighbors++;
        if (cell[row][col-1] == 1) neighbors++;
        if (cell[row-1][col+1] == 1) neighbors++;
        if (cell[row-1][col] == 1) neighbors++;
        if (cell[row-1][col-1] == 1) neighbors++;
    }
    else if (col == 0){
        if (cell[row][col+1] == 1) neighbors++;
        if (cell[row-1][col] == 1) neighbors++;
        if (cell[row-1][col+1] == 1) neighbors++;
        if (cell[row+1][col] == 1) neighbors++;
        if (cell[row+1][col+1] == 1) neighbors++;
    }
    else if (col == colSize){
        if (cell[row][col-1] == 1) neighbors++;
        if (cell[row-1][col] == 1) neighbors++;
        if (cell[row-1][col-1] == 1) neighbors++;
        if (cell[row+1][col] == 1) neighbors++;
        if (cell[row+1][col-1] == 1) neighbors++;
    }
    else{
        if (cell[row-1][col-1] == 1) neighbors++;
        if (cell[row-1][col] == 1) neighbors++;
        if (cell[row-1][col+1] == 1) neighbors++;
        if (cell[row][col-1] == 1) neighbors++;
        if (cell[row][col+1] == 1) neighbors++;
        if (cell[row+1][col-1] == 1) neighbors++;
        if (cell[row+1][col] == 1) neighbors++;
        if (cell[row+1][col+1] == 1) neighbors++;
    }
     
     
    Last edited: Jul 20, 2010
  20. Jul 21, 2010 #19

    Mark44

    Staff: Mentor

    There are a number of mistakes in this code. Here are the first couple of if clauses.
    Code (Text):
    if (row == 0){
        if (cell[row][col+1] == 1) neighbors++;
        if (cell[row][col-1] == 1) neighbors++;
        if (cell[row+1][col+1] == 1) neighbors++;
        if (cell[row+1][col] == 1) neighbors++;
        if (cell[row+1][col-1] == 1) neighbors++;
    }
    else if (row == 0 && col == 0){
        if (cell[row][col+1] == 1) neighbors++;
        if (cell[row+1][col] == 1) neighbors++;
        if (cell[row+1][col+1] == 1) neighbors++;
    }
     
    What happens when row=0 and col=0? The first if clause attempts to check all five neighbors, including the one at cell[0][-1], which is not in your cell array.

    When you're checking row 0, you need to first determine whether you are at either endpoint of the row. The same is true for the last row.

    When you are checking col 0 or the last column, you need to determine whether you are at either endpoint of the column.

    Another possible problem is in this code:
    Code (Text):
    else if (row == rowSize && col == colSize){
        if (cell[row][col-1] == 1) neighbors++;
        if (cell[row-1][col] == 1) neighbors++;
        if (cell[row-1][col-1] == 1) neighbors++;
    }
     
    Assuming your array has rowSize rows and colSize columns, the rows are numbered 0, 1, 2, ..., rowSize - 1. The columns are numbered 0, 1, 2, ..., colSize - 1. The code above is checking outside the bounds of the array.

    You should be able to catch most of these things on your own. One technique is to hand-simulate what your program should do using a small array (say 6 x 6) and a simple initial cell pattern. Run your algorithm a few iterations by hand, to see what should be happening in your program.

    Another thing is that you should be using the debugger that is probably available to you, by single-stepping through your code. Doing that would have caught the situation where you're attempting to read outside the bounds of your array.
     
  21. Jul 21, 2010 #20
    woops, i probably should have mentioned that i changed the for loop in this part. here is my modified code + for loop. i'm still unsure on what i'm making mistakes on

    Code (Text):

         for (int row = 0; row < rowSize; row++){
             for (int col = 0; col < colSize; col++){
                 // calculate the amount of neighbors each cell has
                 int neighbors = 0;
                 if (col > 0 && col < colSize - 1 && row > 0 && row < rowSize - 1){
                    if (cell[row-1][col-1] == 1) neighbors++;
                    if (cell[row-1][col] == 1) neighbors++;
                    if (cell[row-1][col+1] == 1) neighbors++;
                    if (cell[row][col-1] == 1) neighbors++;
                    if (cell[row][col+1] == 1) neighbors++;
                    if (cell[row+1][col-1] == 1) neighbors++;
                    if (cell[row+1][col] == 1) neighbors++;
                    if (cell[row+1][col+1] == 1) neighbors++;
                 }
                 else{
                 if (col == 0){
                    if (cell[row+1][col] == 1) neighbors++;
                    if (cell[row+1][col+1] == 1) neighbors++;
                    if (cell[row][col+1] == 1) neighbors++;
                    if (cell[row-1][col+1] == 1) neighbors++;
                    if (cell[row-1][col] == 1) neighbors++;
                 }
                 if (row == 0 && col == 0){
                     if (cell[row][col+1] == 1) neighbors++;
                     if (cell[row+1][col] == 1) neighbors++;
                     if (cell[row+1][col+1] == 1) neighbors++;
                 }
                 if ((row == rowSize - 1) && col == 0){
                    if (cell[row-1][col] == 1) neighbors++;
                    if (cell[row-1][col+1] == 1) neighbors++;
                    if (cell[row][col+1] == 1) neighbors++;
                 }
                 if (row == 0){
                    if (cell[row][col+1] == 1) neighbors++;
                    if (cell[row][col-1] == 1) neighbors++;
                    if (cell[row+1][col+1] == 1) neighbors++;
                    if (cell[row+1][col] == 1) neighbors++;
                    if (cell[row+1][col-1] == 1) neighbors++;
                 }
                 if (row == 0 && (col == colSize - 1)){
                    if (cell[row][col-1] == 1) neighbors++;
                    if (cell[row+1][col] == 1) neighbors++;
                    if (cell[row+1][col-1] == 1) neighbors++;
                 }
                 if (col == colSize - 1){
                    if (cell[row][col-1] == 1) neighbors++;
                    if (cell[row-1][col] == 1) neighbors++;
                    if (cell[row-1][col-1] == 1) neighbors++;
                    if (cell[row+1][col] == 1) neighbors++;
                    if (cell[row+1][col-1] == 1) neighbors++;
                 }
                 if ((row == rowSize - 1) && (col == colSize - 1)){
                    if (cell[row][col-1] == 1) neighbors++;
                    if (cell[row-1][col] == 1) neighbors++;
                    if (cell[row-1][col-1] == 1) neighbors++;
                 }
                 if (row == rowSize - 1){
                    if (cell[row][col+1] == 1) neighbors++;
                    if (cell[row][col-1] == 1) neighbors++;
                    if (cell[row-1][col+1] == 1) neighbors++;
                    if (cell[row-1][col] == 1) neighbors++;
                    if (cell[row-1][col-1] == 1) neighbors++;
                 }
                 }
     
     
    Last edited: Jul 21, 2010
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook