2d Array Help

I just need a little bit of help.

Can someone please tell me how to pass a 2d Array to a function and then have that function return the highest number in the array to the main function?

Thanks
like so
1
2
3
4
5
6
const int max = 5;
int f (int array[][max]) {
       int x;
       //... use for loop
       return x;
}

you'll have to use some for loop to find higest number in that array.
Last edited on
So I only have to use the first parameter for the array?

this is my array
double food [3][7]

and massing it to a function would look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 const int ROWS = 3; 
const int COLUMNS = 7;
double getLargest (array[][3])
{
double high = array[0][0]
//get HIGHEST number in the array
for (int a = 0; a <= ROWS; a++)
{
    for(int b = 0; b <=COLUMNS; b++)
	{
         if (array[ROWS][COLUMNS] > high)
             high = array [a][b];
	}
}
return high;
}
Last edited on
So I only have to use the first parameter for the array?

no, you have to use all dimensions except the first one or you may include first one as well but it's not deeded,

you fogod to specify type of the array pased into function:
1
2
3
4
5
6
7
8
9
10
11
int ROWS = 3;  //no need for const
const int COLUMNS = 7; //all dimenstions must be const except first one

double getLargest (double array[][COLUMNS]) { //or any other type
double high = array[0][0]
for (int a = 0; a < ROWS; ++a) 
    for(int b = 0; b < COLUMNS; ++b)
         if (array[ROWS][COLUMNS] > high)
             high = array [a][b];
return high;
}
Sorry, its a double array.

Also, can I not pass an array by reference so that a function can change the values in an array.
arrays are always passed by reference to the functions.
but you may use some other metods like copying array and the passing the copy.
I'm not shure if that would be a smart move tough...
unless I'm mistaken, this should get the lowest from the array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
double getLeast (double lowFood[][COLUMNS])
{
	double low = lowFood[0][0];
	//get LOWEST number in the array
	for (int c = 0; c <= ROWS; c++)
		{
		for(int d = 0; d <=COLUMNS; d++)
			{
			if (lowFood[ROWS][COLUMNS] <= low)
			low = lowFood[c][d];
			}
		}
	return low;
}


but the only thing that the this function only returns is 0.0 and not anything else.
Last edited on
closed account (D80DSL3A)
Line 9 is wrong and you are going one too high in the for loops.
Line 9 is wrong and you are going one too high in the for loops


how so? The opposite, get the highest number, works like its supposed to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double getMost (double highFood[][COLUMNS])
{
	//accumulator
	double high = highFood[0][0];

	//get HIGHEST number in the array
	for (int a = 0; a <= ROWS; a++)
		{
		for(int b = 0; b <=COLUMNS; b++)
			{
			 if (highFood[ROWS][COLUMNS] >= high)
			 high = highFood[a][b];
			}
		}
	return high;
}
Last edited on
I'm just retarded with the get lowest.

however, my get average isn't working correctly either, the math isn't correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const int COLUMNS = 7;
cons int ROWS = 3;

double getAverage (double aveFood[][COLUMNS])
{
	cout << fixed << showpoint << setprecision(1);
	double mean = 0.0;
	double total = 0.0;
	//get MEAN number in the array
	for (int e = 0; e <= ROWS-1; e++)
		{
		for(int f = 0; f <= COLUMNS-1; f++)
			{
				total += aveFood[e][f];
			}
		}
	mean  = total / (ROWS * COLUMNS);
	return mean;
}
Last edited on
why is the loop going to 8 instead of stopping on 7?

I don't understand that.
this is your problem:
for (int e = 0; e <= ROWS-1; e++)

and this is what you need
for (int e = 0; e < ROWS; ++e)

because an array beging with 0 and eds with ROWS -1

SAME IS WITH COLUMNS!

No2
make shure your array is fully initialized because unused members will have value of zero (if they are automaticly located.

or use if checking to stop when 0 is reached so you will get right result and not 0

if array is located dinamicaly then initialize your array with zeros and do the same check for first zero.
Last edited on
Topic archived. No new replies allowed.