The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

(More) C++ Help!

clsCorwinclsCorwin Registered User regular
edited January 2007 in Help / Advice Forum
Ok, so I'm back again. Now, this program will compile just fine, and it will run. However, my output is wrong, and the 2 methods that are not working right, I can't find any errors in them. I've just about gone nuts trying to find this error, so maybe you guys have some better eyes for bugs than I do.

This program reads in a file (stars.txt) which contains 2 numbers (R, C) on the first line, and then the rest is data in a RxC grid which will later be read into a 2D array. I've checked my print methods with predefined data and they work fine, input to the array is fine. Somewhere in the check methods I'm getting an error (specifically in checkDown() and checkLeft() ) as I figured out through some testing.


This is the data I was using in stars.txt.

Any insights would be appreciated.

10 10
9 1 1 1 1 1 1 8 4 9
1 1 1 1 1 1 1 7 1 1
1 1 1 1 1 1 9 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 9 1 1 1 1 1
1 1 1 1 9 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
9 1 1 1 1 1 1 1 1 9


#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

const int R = 30;
const int C = 10;


typedef int arrayOfStars[R][C];
typedef char seeTheStars[R][C];
arrayOfStars data;
seeTheStars stars;

class STARS
{

    int r, c;

    bool isDarker(int current, int other)                //checks to see if one location is darker than another
    {                                                   
        if( (current - other) > 3)
            return true;
        else
            return false;
    }

    bool checkLeft(int rowPosition, int colPosition)        //checks to the left of star[x][y] to see if
    {                                                        //it is darker than itself
        int currentValue = data[rowPosition][colPosition];
        int leftValue = data[rowPosition][colPosition - 1];

        if(colPosition = 0)                                    //checks if we are all the way left already
            return true;
        else if(isDarker(currentValue, leftValue) == true)
            return true;
        else
            return false;
    }

    bool checkRight(int rowPosition, int colPosition)        //checks to the right of star[x][y] to see if
    {                                                        //it is darker than itself
        int currentValue = data[rowPosition][colPosition];
        int rightValue = data[rowPosition][colPosition + 1];

        if(colPosition = (c - 1) )                            //checks if we are on far right already
            return true;
        else if(isDarker(currentValue, rightValue) == true)
            return true;
        else
            return false;
    }

    bool checkUp(int rowPosition, int colPosition)            //checks above star[x][y] to see if
    {                                                        //it is darker than itself
        int currentValue = data[rowPosition][colPosition];
        int upValue = data[rowPosition - 1][colPosition];
        int upLeftValue = data[rowPosition - 1][colPosition - 1];
        int upRightValue = data[rowPosition - 1][colPosition + 1];   

        if(rowPosition = 0)                                    //checks if we are at the top already
            return true;
        if( (isDarker(currentValue, upValue) == true) && (isDarker(currentValue, upLeftValue) == true) &&
           (isDarker(currentValue, upRightValue) == true) )
            return true;
        else
            return false;
    }

    bool checkDown(int rowPosition, int colPosition)        //checks below star[x][y] to see if
    {                                                        //it is darker than itself
        int currentValue = data[rowPosition][colPosition];
        int downValue = data[rowPosition + 1][colPosition];
        int downLeftValue = data[rowPosition + 1][colPosition - 1];
        int downRightValue = data[rowPosition + 1][colPosition + 1];

        if(rowPosition = (r - 1) )                            //checks if we are at the bottom already
            return true;
        if( (isDarker(currentValue, downValue) == true) && (isDarker(currentValue, downLeftValue) == true) &&
           (isDarker(currentValue, downRightValue) == true) )
            return true;
        else
            return false;
    }

    bool checkAll(int row, int column)
    {
        if( (checkUp(row, column) == true) && (checkDown(row, column) == true) &&
            (checkLeft(row, column) == true) && (checkRight(row, column) == true) )
            return true;
        else
            return false;
    }

public:

    void findStars()
    {
        for(int x = 0; x < r; x++)
        {
            for(int y = 0; y < c; y++)
            {
                if(checkAll(x, y) == true)
                    stars[x][y] = '*';
                else
                    stars[x][y] = ' ';
            }
        }
    }

    void getInput()
    {
        ifstream dataInput("stars.txt");

        dataInput >> r >> c;

        while(!dataInput.eof())
        {
            for(int row = 0; row < r; row++)
            {
                for(int column = 0; column < c; column++)
                {
                    dataInput >> data[row][column];
                }
            }
        }
        dataInput.close();
    }

    void printInput()
    {
        cout << "Here is the data from stars.txt:\n";
        cout << "   1 2 3 4 5 6 7 8 9 10" << endl;
        int rowNumber = 1;

        for(int x = 0; x < r; x++)
        {
            if(x != c - 1)
                cout << " " << rowNumber << " ";
            else
                cout << rowNumber << " ";

            for(int y = 0; y < c; y++)
            {
                if(y == c - 1)
                {
                    cout << data[x][y] << endl;
                    rowNumber++;
                }
                else
                    cout << data[x][y] << " ";       
            }
        }
        cout << endl;
    }

    void printStars()
    {
        cout << "Here are the stars:\n";
        cout << "   1 2 3 4 5 6 7 8 9 10" << endl;
        int rowNumber = 1;

        for(int x = 0; x < r; x++)
        {
            if(x != c - 1)
                cout << " " << rowNumber << " ";
            else
                cout << rowNumber << " ";

            for(int y = 0; y < c; y++)
            {
                if(y == c - 1)
                {
                    cout << stars[x][y] << endl;
                    rowNumber++;
                }
                else
                    cout << stars[x][y] << " ";       
            }
        }
        cout << endl;
    }

    void printCoordinates()
    {
        int rowNumber = 1;
        cout << "The stars are located at:";
        for(int x = 0; x < r; x++)
        {
            if(x != c - 1)
                cout << endl << " " << rowNumber << " ";
            else
                cout << endl << rowNumber << " ";
            rowNumber++;

            for(int y = 0; y < c; y++)
            {
                if(stars[x][y] == '*')
                    cout << "(" << (x + 1) << ", " << (y + 1) << ")" << " ";
            }
        }
        cout << endl;
    }
};

int main()
{
    STARS star;
    star.getInput();
    star.findStars();

    star.printInput();
    star.printStars();
    star.printCoordinates();
    return 0;
}

clsCorwin on

Posts

  • Bob SappBob Sapp Registered User regular
    edited January 2007
    if(colPosition = 0) 
    if(colPosition = (c - 1) )  
    

    I think you can see the problem here.

    Edit: this would have been a perfect opportunity to use a debugger. If you don't know how to use one, you should really learn. I picked this out just by a quick visual scan of your function, but you would have found the same thing if you used a debugger.

    Bob Sapp on
    fizzatar.jpg
  • clsCorwinclsCorwin Registered User regular
    edited January 2007
    No, thats supposed to be there.

    colPosition is the current position along the Y that we are looking at. In checkLeft() I use the first if to check if we are at position [X][0], if we are at, then there is no data to the left of it, so it returns TRUE. Likewise, in checkRight() if we are at the end of the row [X][C-1] (and its C-1 because C is in this case, 10. In the array, thats 0 through 9. 10 - 1 = 9 which is the final position in the row.

    Or are you trying to say theres another problem with that?

    clsCorwin on
  • Bob SappBob Sapp Registered User regular
    edited January 2007
    You're setting colPosition to 0 instead of checking that it equals 0.

    Should be if(colPosition == 0)

    Bob Sapp on
    fizzatar.jpg
  • clsCorwinclsCorwin Registered User regular
    edited January 2007
    I'm a moron, thanks. lol

    clsCorwin on
  • clsCorwinclsCorwin Registered User regular
    edited January 2007
    Ok, much better, but now my top left and bottom right are not being flagged as a "Star" when they should. Any other obvious mistakes you can see?

    clsCorwin on
  • localh77localh77 Registered User regular
    edited January 2007
    This wouldn't be causing the problem, but it should be fixed anyway. In the check functions, you don't want to access data until you know that it's a valid index. I would do the "if(rowPosition == 0) return true" or whatever at the beginning of the functions, rather than after you read in the values from data.
    Other than that, it looks ok. Did you make sure to check all the ifs for "=" instead of "==", there's a few in there.
    If it's still not working, could you paste the output?

    localh77 on
  • clsCorwinclsCorwin Registered User regular
    edited January 2007
    Yes, I did change the other = to ==. Lemme post my output here...

    [code]
    Here is the data from stars.txt:
    1 2 3 4 5 6 7 8 9 10
    1 9 1 1 1 1 1 1 8 4 9
    2 1 1 1 1 1 1 1 7 1 1
    3 1 1 1 1 1 1 9 1 1 1
    4 1 1 1 1 1 1 1 1 1 1
    5 1 1 1 1 9 1 1 1 1 1
    6 1 1 1 1 9 1 1 1 1 1
    7 1 1 1 1 1 1 1 1 1 1
    8 1 1 1 1 1 1 1 1 1 1
    9 1 1 1 1 1 1 1 1 1 1
    10 9 1 1 1 1 1 1 1 1 9

    Here are the stars:
    1 2 3 4 5 6 7 8 9 10
    1 *
    2
    3
    4
    5
    6
    7
    8
    9
    10 *

    The stars are located at:
    1 (1, 10)
    2
    3
    4
    5
    6
    7
    8
    9
    10 (10, 1) [\code]

    And I should be showing stars at (1,1) and (10,10), but they aren't being flagged.

    clsCorwin on
  • ecco the dolphinecco the dolphin Registered User regular
    edited January 2007
    localh77 wrote:
    This wouldn't be causing the problem, but it should be fixed anyway. In the check functions, you don't want to access data until you know that it's a valid index. I would do the "if(rowPosition == 0) return true" or whatever at the beginning of the functions, rather than after you read in the values from data.

    This man speaks truth, and I think your bug is also caused by a variant on this problem.

    Think about what happens when you checkDown() at position 0, 0. You validate the row data - but don't validate the column data. As a result, you're trying to access column -1 in your data with downLeftValue.

    Similar, checkUp() at the bottom-most row.

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • clsCorwinclsCorwin Registered User regular
    edited January 2007
    Yea, I thought of that, and for a bit split down, downLeft, and downRight into separate functions, but I still had my mistake of = and not ==. I think I'll split them back up and see if I can tidy things up that way.

    clsCorwin on
  • clsCorwinclsCorwin Registered User regular
    edited January 2007
    Perfection. Thanks for the help.

    clsCorwin on
Sign In or Register to comment.