Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,501 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,363 people online right now. Registration is fast and FREE... Join Now!




Like most of this entire Forum...Question about an array

 
Reply to this topicStart new topic

Like most of this entire Forum...Question about an array

mattman059
28 Feb, 2007 - 05:29 AM
Post #1

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 359


Dream Kudos: 175
My Contributions
Okay...I have this array see and its 5x5 and I'm doing something similiar to the game of life (but not really..its different) I need to check the neighbors for a certain element in the array, and determine whether to move that element or keep it in its spot. my problem is not with the middle element because it has its eight neighbors and that's fine, my problem lies in the corners, and the edges which have respectively 3, and 5 neighbors. I was wondering if anyone knew how to both, tell a spot is a corner or edge, and how to manipulate the neighbors without hard coding a specific function for corners, and edges..

Here is some code that tells you whether an element is an edge..

CODE

    srand(time(NULL));
    int rows, columns, i , j;

    int **matrix;
    cin >> rows >> columns;
    matrix = new int* [rows];
    for (i = 0; i< rows; i++)
        matrix[i] = new int[columns];

    //Fill the array with 0's
    for(int row = 0; row < rows; row++)
        for(int col = 0; col < columns; col++)
            matrix[row][col] = rand() %2;

for (int a=0; a<rows; a++)
{
    for (int b=0; b<columns; b++)
    {
        if(a ==0 || a== rows-1)
            cout << "(" << a << "," << b << ") is an edge" << endl;
        if(b ==0 || b== columns-1)
            cout << "(" << a << "," << b << ") is an edge" << endl;
    }
}


// Print the array
    for (int pRow = 0; pRow < rows; pRow++)
    {
        for (int pCol = 0; pCol< columns; pCol++)
        {
            cout << matrix[pRow][pCol];
        }
        cout << endl;
    }
    

    for (i=0; i<rows; i++)
        delete[] matrix[i];
    delete [] matrix;


    return 0;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Like Most Of This Entire Forum...Question About An Array
28 Feb, 2007 - 05:41 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
If the element is array[min][min], you know it is a corner with three neighbors...the same with array[min][max], array[max][min], and array[max][max],

An element is an edge with 5 neighbors if it is located anywhere in which it is not one of the previous cases, but one of the indexes is either min or max.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Like Most Of This Entire Forum...Question About An Array
28 Feb, 2007 - 01:46 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
I am afraid that you really have to code in conditions for each different "Type" of cell. One thing that you can do is encase the matrix into a class and offer little functions like "isCorner(row, col)" or "isEdge(row, col). This might simplify the logic in your main routine. The other thing you can do is make inline functions "isCorner(...)" and isEdge(...). The compiler will replace each inline function call with the code inside so that it will be equivalent to hard coding each condition . This will also simplify what you have to type as well as the logic involved.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month