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
  22. Jul 21, 2010 #21

    Mark44

    Staff: Mentor

    All or most of the if clauses need to be restructured, since you are processing many of the array cells more than once. Here's an example of what I'm talking about.
    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++;
    }
    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++;
    }
     
    Suppose you are working with cell[0][colSize-1]. Here row == 0, so the first outer if clause above is entered, and the code checks all 5 neighbors (there are only 3, so that's an error).
    Next, since row == 0 and col == colSize -1, the next outer if clause is entered, and the code checks all three neighbors. The thing is, your code has processed cell[0][colSize-1] twice.

    The code I picked out is not the only place this is happening. To prevent this from happening, you should reorganize the entire body of the inner for loop. The way I would probably go is like this:
    Code (Text):

    Is the cell in the top row of the array?
       Is the cell a corner point? If so, count the 3 neighbors. Otherwise, count the 5 neighbors.
    Otherwise, is the cell in the bottom row of the array?
       Is the cell a corner point? If so, count the 3 neighbors. Otherwise, count the 5 neighbors.
    Otherwise, is the cell along the left edge of the array? (It isn't a corner,
          since these have already been processed, above.) If so, count the 5 neighbors.
    Otherwise, is the cell along the left edge of the array? (It isn't a corner,
          since these have already been processed, above.) If so, count the 5 neighbors.
    Otherwise, the cell must be in the interior of the array, so count the 8 neighbors.
    Also, it doesn't seem to me that you are using your neighbors count correctly. For each cell, the number of neighbors determines the status of that cell in the next iteration of the array (the next life). When you find out how many neighbors a given array cell has, you should be doing something with the next array iteration, and I don't see that happening.
     
  23. Jul 21, 2010 #22
    Wait, what's wrong with letting the computer do all the checks in a compact format? The first two loops work to cycle through all cells to check. The next two for loops cycle through all neighbors for said cell. They take it through all 8 blocks around that said cell as well as through the cell in question. The first if statement makes sure you don't count the cell whose neighbors you're evaluating as a neighbor (if right || up means right and up cannot equal 0 at the same time. If they do, you're on the cell whose neighbors you are evaluating.) The next if statement ensures you do not try to evaluate an illegal index of the array. It cannot be negative in terms of row && it cannot be negative in terms of column && you cannot evaluate a column greater than the biggest column && you cannot evaluate a row greater than the biggest row. The last if statement checks if the neighbor you are evaluating is living.

    Another, possibly easier, way to implement your game is to have an n+2 by n+2 board for any game sized at n by n. If you then use the interior parts of the array, with the edges preset to be "dead", you could eliminate all conditional statements since the corner evaluations would occur without causing a run-time error and will always return "dead," meaning it won't affect your game.

    Code (Text):
    for(int row = 0; row < rows; ++row)
    {
        for(int column = 0; column < columns; ++columns)
        {
                    neighbors = 0;
            for(int up = -1; up < 2; ++up)
            {
                for(int right = -1; right < 2; ++right)
                {
                    if(right || up)
                    {
                        if(row + right > -1 && column + up > -1 && column + up < columns && row + right < rows)
                        {
                            if (cell[row + right][colomn + up] == 1) neighbors++;
                        }
                    }
                }
            }
        }
    }
     
    Last edited: Jul 21, 2010
  24. Jul 21, 2010 #23

    Mark44

    Staff: Mentor

    Being understandable is more important than having a compact format. Right now magnifik is having difficulty understanding what his own code is doing and how that relates to the problem he's trying to solve, so it's probably a little soon for clever solutions. IMO.
     
  25. Jul 21, 2010 #24
    I'm having trouble implementing your solution as well, because you have to think about so much damn stuff.
     
  26. Jul 21, 2010 #25
    i didn't put that in the segment i put up, but it is in my actual code. thank you for all your help! is what i have below finally right??

    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 (row == 0){
                     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 (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++;
                     }
                     else{
                         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 == rowSize - 1){
                     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 (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++;
                     }
                     else{
                         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++;
                 }
                 else 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++;
                 }
                 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++;
                    }
                 // apply the rules of the game, determining births and deaths
                 if (cell[row][col] == 1 && neighbors < 2)
                    newLife[row][col] = 0;
                 else if (cell[row][col] == 1 && neighbors > 3)
                    newLife[row][col] = 0;
                 else if (cell[row][col] == 1 && (neighbors == 2 || neighbors == 3))
                    newLife[row][col] = 1;
                 else if (cell[row][col] == 0 && neighbors == 3)
                    newLife[row][col] = 1;
             }
         }
     
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook