I am currently stuck on part a of this problem. I am having a problem with my stub, I am also running this on visual C++ 2008 express. My stub is a program i had to create to find the maximum value from the 10x20 array. For some reason that's not setting up right. I will post y errors the compiler gave me, but my compiler also don't least lines for whatever reason. so the errors may not make sense, but i'll point them out as carefully as possible.

okay the official problem states this:

A. Write a function named findmax() that finds and displays the maximum values
in a two dimensional array of integers. The array should be declared as a 10 row
by 20 column array of integers in main() and populated with random numbers
between 0 and 100.

B. Modify the function written above so that it also displays the row and column
numbers of the element with the maximum value.

Here's what I have so far:

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

int findMax(int[][20]);

int main()
{
	const int j = 10;
	const int k = 20;
	int i,u;
	int max;
	
	int grade[j][k];

	srand(time(NULL));

	for(i=0;i<j;i++)
	{
		cout << endl;
		for(u=0;u<k;u++)
		{
			grade[i][u]=rand()%100;
			cout << setw(4) << grade[i][u];
		}
	}

	 findMax(grade);

	cout <<"The max value is " << max;

	system("pause");
		return 0;
}

int findMax(int grade[][20])
{
	int i, u;
	int j = 10;
	int k = 20;
	int max = 0;
 for(i=0;i<j; i++)
 	 for(u=0; u<k; u++)
	 { if (max < grade[i][u])
			max = max+grade[i][u];
	 }
 
		 return max;
}

New Errors:

Run-Time Check Failure #3 - The variable 'max' is being used without being initialized.

So After the changes were made, the stub runs, but for some reason it isn't return my max to the int main. When I have it set to cout the maximum value it says the variable max wasn't initialized with a value.

Edited 7 Years Ago by jinjishu: n/a

I got rid of all of the errors by just initializing max in my stub, so on line 41 I put int max = 0; moving all codes underneath that point down by one.

now my error is :

(44) : error C2065: 'grade' : undeclared identifier
(45) : error C2065: 'grade' : undeclared identifier

where line 44 is this line if (max < grade)
and line 45 is this max = max+grade;

I want to know how exactly do i initialize it? I can't help but think I am ot supposed to place grade[j][k] in that spot.

int findMax(int [][20]) needs to be int findMax(int grade[][20]) Definitions always need the identifier, declarations do not.
There's no identifier there so the compiler reflects a mismatched brace somewhere even though there isn't.

Thanks, that helped it run, but now I am having trouble returning the value max so it can cout the maximum value in my array.

It says this:
Run-Time Check Failure #3 - The variable 'max' is being used without being initialized.

Edited 7 Years Ago by jinjishu: n/a

In main set max = findMax(grade); I had missed that before, but if you don't have a variable to return your function to the value is lost.

All of your post editing is getting confusing I'm not sure where you are in the chronology.

Edited 7 Years Ago by jonsca: n/a

yeah I actually just noticed that when I ran program and it stated max was 101. Thanks a bunch. You've been real helpful, now in part b I only need to return the index and not the value which should be a quick change.

I decided to give myself a break before continuing.

I am looking at this now and realizing I didn't think this through. I thought I would add a counter to the if statement in my stub to keep track of the i and u counters. I realized this was a bad idea when I remember you can't return 2 different variables. I was going to return my i counter and u counter and have it

cout<< " the index for the make value is in row " << row << " column " << col << endl;

any advice and what I can try instead to return the index of the my maximum value?

Edited 7 Years Ago by jinjishu: n/a

I decided to give myself a break before continuing.

I am looking at this now and realizing I didn't think this through. I thought I would add a counter to the if statement in my stub to keep track of the i and u counters. I realized this was a bad idea when I remember you can return 2 different variables. I was going to return my i counter and u counter and have it

cout<< " the index for the make value is in row " << row << " column " << col << endl;

any advice and what I can try instead to return the index of the my maximum value?

You should post your updated code and explain exactly what your present problem is. You've made a lot of revisions, but no one knows what they are.

oh sorry about that, I was going to do that when I realized the edit button on my orginal post has disappeared. I guess I forgot to just post it on a new spot.

Here is my current code:

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

int findMax(int[][20]);

int main()
{
	const int j = 10;
	const int k = 20;
	int i,u;
	int max;
	
	int grade[j][k];

	srand(time(NULL));

	for(i=0;i<j;i++)
	{
		cout << endl;
		for(u=0;u<k;u++)
		{
			grade[i][u]=rand()%101;
			cout << setw(4) << grade[i][u];
		}
	}

	 max = findMax(grade);

	cout <<"The max value is " << max << "\n\n";

	system("pause");
		return 0;
}

int findMax(int grade[][20])
{
	int i, u;
	int j = 10;
	int k = 20;
	int max = 0;
 for(i=0;i<j; i++)
 	 for(u=0; u<k; u++)
	 { if (max < grade[i][u])
			max = grade[i][u];
	 }
 
		 return max;
}

What I am trying to do now is return the index number of the maximum value this time around instead of just returning the actual maximum value which is what the code shows now.

As stated before I can not return 2 values at the same time, so I am wondering how do I go about return the index of the maximum value to the int main?

I wanted to replace my old cout with this cout on line 30:

cout<< " The index for the max value is in row " << row << " column " << col << endl;

Edited 7 Years Ago by jinjishu: n/a

You CAN "return" more than one value. "Return" is in quotes on purpose. Pass the row and column by reference (denoted by &).

int findMax(int grade[][20], int& row, int& column)
{
    // same as before.  Return the max.  In addition, inside this function, assign the row and column of that maximum to the variables of these names.
}

Function call from main is largely the same as you have it, as well as the display: See revised code. I've left it to you to actually give the correct values:

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

int findMax(int grade[][20], int& row, int& column);

int main()
{
	const int j = 10;
	const int k = 20;
	int i,u;
	int max, row, column;
	
	int grade[j][k];

	srand(time(NULL));

	for(i=0;i<j;i++)
	{
		cout << endl;
		for(u=0;u<k;u++)
		{
			grade[i][u]=rand()%100;
			cout << setw(4) << grade[i][u];
		}
	}

	 max = findMax(grade, row, column);
cout <<"The max value is " << max << "\n\n";
cout<< " the index for the make value is in row " << row << " column " << col << endl; 
	system("pause");
		return 0;
}

int findMax(int grade[][20], int& row, int& column)
{
	int i, u;
	int j = 10;
	int k = 20;
	int max = 0;
 for(i=0;i<j; i++)
 	 for(u=0; u<k; u++)
	 { if (max < grade[i][u])
			max = grade[i][u];
	 }
 
                 row = 4;
                 column = 6;

		 return max;
}

Run it and you should have it (incorrectly) say that row 4, column 6, are the max indexes. Delete lines 49 and 50. Replace with code inside of the function that assigns row and column correctly.

But this should solve your problem of passing parameters and the problem of not being able to "return" more than one value.

Edited 7 Years Ago by VernonDozier: Accidentally deleted the line that displayed the max. Now that line is back in.

Oh totally forgot about pass-by references. Thanks a bunch dude. here's the changes I made also

I changed my cout to this:

cout <<"\n\n The max value is " << max <<" The max value index is [" << row << "][" << column <<"]\n";

and my stub now looks liek this:

int findMax(int grade[][20], int& row, int& column)
{
	int i, u;
	int j = 10;
	int k = 20;
	int max = 0;
 for(i=0;i<j; i++)
 	 for(u=0; u<k; u++)
	 { if (max < grade[i][u])
		{	max = grade[i][u];
		row = i;
		column = u;
		}
	 }
This article has been dead for over six months. Start a new discussion instead.