Hi
I am trying to write a function in a program that reads a 2d array withs 2 rows & determines the largest number & whehter that number is in row 0 or row 1.

For example:

Enter distinct integers for a 2d array with 2 rows and 5 columns.
3 1 5 2 4
8 6 9 7 0

The maximum is 9 in row 1 and column 2

How do I determine if the largest number (in this case '9') is in the 1st row or the 2nd & return that into main?

My coding so far works great at finding the maximum number of the 1st row but cant return the row its in into main.

#include <iostream>
#include <iomanip>

using namespace std;

// read values for a two-dimentional array
void read2d(int a[][5], int rowsize);

// display the values in a two-dimentional array
void display2d(const int a[][5], int rowsize);

// find the ma and min in 2d array a
void max(int a[][5], int rowsize, int& row, int& col);  //this is the function that isn't working

int main()
{
	int table[2][5];
	int rowIndex, colIndex;


	cout << "Enter distinct integers for a 2d array with 2 rows and 5 columns." << endl; // don't modify
	read2d(table, 2); // don't modify

	display2d(table, 2); // don't modify

	max(table, 2, rowIndex, colIndex);
	// write code to locate the maximum in 2d array table

	cout << "The maximum is " << table[rowIndex][colIndex] << " in row " << rowIndex << " and column " << colIndex << endl; // don't modify

	return 0;
}

void read2d(int a[][5], int rowsize)
{
	for (int i = 0; i < rowsize; i++)
		for (int j = 0; j < 5; j++)
			cin >> a[i][j];
}

void display2d(const int a[][5], int rowsize)
{
	for(int i = 0; i < rowsize; i++)
	{
		for(int j = 0; j < 5; j++)
			cout << setw(5) << a[i][j];
		cout << endl;
	}
	cout << endl;

}

void max(int a[][5], int rowsize, int& row, int& col)
{

	int temp= 0;
	int max;
	int j= 0;

	for (j; j <= 1; j++)
	{
		for (int i = 0; i <= 5; i++)
		{
			if (a[j][i] >= temp) {
				cout << a[j][i] << endl;
				temp = a[j][i];
			}
		}
	}
	temp = max;
	return a[j][max];
}

This is the function not doing what I want it to do; find the maximum & what row it is in.

void max(int a[][5], int rowsize, int& row, int& col)
{

	int temp= 0;
	int max;
	int j= 0;

	for (j; j <= 1; j++)
	{
		for (int i = 0; i <= 5; i++)
		{
			if (a[j][i] >= temp) {
				cout << a[j][i] << endl;
				temp = a[j][i];
			}
		}
	}
	temp = max;
	return a[j][max];
}

To find both row and column, start the function out by setting both values to 0, and temp to the value of whatever is in a[0][0]. Then if the value in a[j] > temp (note: not >=) change the value of all three variables -- temp, row, and col.

Thanks it now returning in main but not the correct maximum or row or column.

This is what I have now but *urgh* still not working but should work. Any advice?

int max(int a[][5], int rowsize, int& row, int& col)
{

	int temp= 0;
	int max= 0;
	int i = 0;
	int j = 0;
	row = 0;
	col= 0;

	for (j= 0; j > 2; j++)
	{
		for (i= 0; i >= 5; i++)
		{
			if ( a[j][i] > temp) {
				cout << a[j][i] << endl;
				temp = a[j][i];
				row = a[j][i];
				col = a[j][i];
			}
			cout << a[j][i] << endl;
		}
	}
	temp = max;
	return a[row][col];
}

You need to pay attention to detail more closely.

>>for (j= 0; j > 2; j++)

use the < operator, not the > operator.

>>for (i= 0; i >= 5; i++)
Same problem as above -- use < operator, not the >= operator.

>> row = a[j];
>> col = a[j];

No, No, No. You want to set row and col to the values of the loop counters i and j.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.