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

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




2D arrays to functions

 
Reply to this topicStart new topic

2D arrays to functions

mattman059
28 Feb, 2007 - 12:23 PM
Post #1

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 359


Dream Kudos: 175
My Contributions
CODE
int isValid(array[][], int a , int b)


That is my function prototype that i will be using in my program...


CODE
int myArray[5][5];  


That is my array. I would like to be able to send my array to the function and pass the parameters a, and b to eventually find out if an element around this block is valid. (making sure that you cant get to myArray[-1][-1] and so forth)

any ideas?


User is online!Profile CardPM
+Quote Post

dr_worm
RE: 2D Arrays To Functions
28 Feb, 2007 - 12:27 PM
Post #2

D.I.C Head
**

Joined: 11 Sep, 2006
Posts: 131


My Contributions
What do you mean by "if an element around this block is valid"?
User is offlineProfile CardPM
+Quote Post

mattman059
RE: 2D Arrays To Functions
28 Feb, 2007 - 12:36 PM
Post #3

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 359


Dream Kudos: 175
My Contributions
You have this 2D array full of 0's and 1's

01001
00100
10001
00010
01001


In no particular order Okay? well I must find out whether a certain spot is happy or not based on its surroundings. so the element array[0][0] is the very first element (0) and to check to see if its happy or not you would use things like maxRow or maxCol and 0's to say array[0][0] or array[MaxRow-1][0] or something like that. Well its very easy to go outside of the array, and manipulate elements on other rows of the array so Im doing a kind of bounds checking, using a bool to determine whether a certain element is valid or not.

if I was to pass the value array[0][0]
i would only want to get back results for:
array[0][1], array[1][0], and array[1][1]
User is online!Profile CardPM
+Quote Post

bestbat
RE: 2D Arrays To Functions
28 Feb, 2007 - 12:37 PM
Post #4

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
You need to pass it in like this:
CODE
int isValid(**array, int a , int b)


I'm not too sure whether this holds for your array, I know this works for arrays using malloc.

This post has been edited by bestbat: 28 Feb, 2007 - 12:38 PM
User is offlineProfile CardPM
+Quote Post

mattman059
RE: 2D Arrays To Functions
28 Feb, 2007 - 12:40 PM
Post #5

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 359


Dream Kudos: 175
My Contributions
Happy.cpp(16) : error C2664: 'numNeighbors' : cannot convert parameter 1 from 'int [5][5]' to 'int ** '



the array is already predefined in terms of size...its definition is myArray[5][5].

so when i actually write the function do i need to dynamically allocate the memory for the array?
User is online!Profile CardPM
+Quote Post

dr_worm
RE: 2D Arrays To Functions
28 Feb, 2007 - 12:47 PM
Post #6

D.I.C Head
**

Joined: 11 Sep, 2006
Posts: 131


My Contributions
Could you please post the code in it's entirety? That way we can get a better look at where the errors are coming from and what you're trying to do. And please bear with me, but I still don't know what you mean by a happy element. Is it happy if it's only surrounded by like elements on all sides?
User is offlineProfile CardPM
+Quote Post

mattman059
RE: 2D Arrays To Functions
28 Feb, 2007 - 12:50 PM
Post #7

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 359


Dream Kudos: 175
My Contributions
CODE

#include <iostream>

using namespace std;


int numNeighbors(int**,int,int);
void Three_Neighbors(int** ,int,int);
void Five_Neighbors(int**,int,int);
void Eight_Neighbors(int**,int,int);

int main()
{
    int myArray[5][5];


    cout << numNeighbors(myArray,0,0)<<endl;


    return 0;
}



int numNeighbors(int **array,int a, int b)
{
    bool isValid;
    int neighbors = 0;

    if(array[a-1][b-1])
    {
        isValid = true;
        neighbors++;
    }

    return neighbors;
}




in my code, all values are 0, I just want to count the number of neighbors and tell if an element is valid
User is online!Profile CardPM
+Quote Post

Amadeus
RE: 2D Arrays To Functions
28 Feb, 2007 - 01:00 PM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Try
CODE

int numNeighbors(int [][5],int,int);

and check this out.

http://www.cplusplus.com/doc/tutorial/arrays.html

Multi dimensional arrays need to have the depth passed.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:50PM

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