Hi everyone,
I am trying to make a function such as

void resetGrid(int grid[][gridSize])

The function needs to input the 2-D array and then set each of the values to zero. I know this should be a simple double 'for' loop but what I can't figure out is how far the 'for' loop for the rows should go since the length is unknown. Anyone have any ideas? Thanks for your help in advance.
Nick

I am not sure i got your question properly...but i guess what you need to do is to pass the size of the rows also to the function....arrays don't know there sizes...so u need to pass that or take a global constant that can be used anywhere

I hope this help.

#include <iostream>
using namespace std;

#define gridSize 5

void resetGrid(int grid[][gridSize])
{
	
	
	for(int i = 0;i < gridSize; i++)
	{
		for(int j = 0; j < gridSize; j++)
		{
			grid[i][j] = 0;
		}
	}
	
	
}


int main()
{
	int myGrid[gridSize][gridSize];
	
	for(int i = 0;i < gridSize; i++)
	{
		for(int j = 0; j < gridSize; j++)
		{
			myGrid[i][j] = 5;
		}
	}
	
	cout << "Before reset:\n\n";
	
	
	for(int i = 0;i < gridSize; i++)
	{
		for(int j = 0; j < gridSize; j++)
		{
			cout << myGrid[i][j] << " ";
		}
		
		cout << endl;
	}	
	
	
	resetGrid(myGrid);
	
	
	cout << "After reset:\n\n";
	
	
	for(int i = 0;i < gridSize; i++)
	{
		for(int j = 0; j < gridSize; j++)
		{
			cout << myGrid[i][j] << " ";
		}
		
		cout << endl;
	}	
	
	cout << endl;
	return (0);
}

here is the output

Before reset:

5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
After reset:

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

good luck!,

-r

This article has been dead for over six months. Start a new discussion instead.