Its been a little while since i have used Dani Web but i need some help. If the code looks a little off then you have to forgive me. I was getting help from a friend who i found knew less about it then me.

Here is the criteria for the code:

-Write a function named findMax()that finds and returns the maximum value in a two-dimensional array of integers.
-The 3-row-by-4-column array in main()should be declared as:
int numbers[3][4] = {8, 16, 9, 52, 3, 15, 27, 6, 14, 25, 2, 10};

Here is my current code however it has a multitude of errors due to my getting lost from help that didnt understand.

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

int findMax int([][4]);

int main()
{
	const int j = 3;	
	const int k = 4;
	int i,u;
	int max; 
	int numbers[j][k];

	int numbers [3][4];

	for(i=0; i<j; i++)
	{
		cout << endl;
		for(u=0;u<k;u++)
		{
			int numbers[i][u]= {8, 16, 9, 52, 3, 15, 27, 6, 14, 25, 2, 10};
			cout << setw(4) << numbers [i][u];

		}
	}
	max = findMax(numbers);

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

	return 0;
}

int findMax(int numbers[][4])
{
	int i, u;
	int j = 3;
	int k = 4;
	int max = 0;
	for(i=0;i<j; i++)
	for(u=0; u<k; u++)
	{if (max < numbers[i][u])
					max = numbers[i][u];
	}
return max;
}

I would greatly appreciate anyone who can help me correct this.

Recommended Answers

All 5 Replies

Hello.

If you've initializing a two dimensional array, then group your numbers as such. In this case, you have three sets of four integers i.e.

int numbers[3][4] = { {8, 16, 9, 52}, { 3, 15, 27, 6 }, { 14, 25, 2, 10} };

Along with this, you don't need to put

int numbers[][]

each time you need to use the array. It was already declared at the start of main, so remove every other line that declares an array after the first time.

Also, if you're initializing the array, then the loop in main is useless. All you'd need to do is call findMax and it should return the result. Hope it helps.

thank you for being willing to help me with this. I am a little confused however. Were you saying that a loop is not at all necessary in this?

No problem.

In your main function, the nested for loops aren't needed because you are initializing the array with the values.
Ex:

int numbers[3][4] = { {8, 16, 9, 52}, { 3, 15, 27, 6 }, { 14, 25, 2, 10} };

If you wanted to enter the values yourself, you could do something like:

for (int a = 0; i < 3; a++) 
   for (int b = 0; b < 4; b++) 
      cin >> numbers[a][b];

But, in doing this, you would just have to declare your array without any initialization list.

ok so i think i have done what you have said but i still think im missing something. This is what i currently have:

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

int findMax [3][4];

int main()
{
	const int j = 3;	
	const int k = 4;
	int i,u;
	int max; 
	int numbers[j][k];

	int numbers[i][u]= {{8, 16, 9, 52},{3, 15, 27, 6},{14, 25, 2, 10}};
	cout << setw(4) << numbers [i][u];

	max = findMax(numbers);

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

	return 0;
}

int findMax(int numbers[][4])
{
	int i, u;
	int j = 3;
	int k = 4;
	int max = 0;
	for(i=0;i<j; i++)
	for(u=0; u<k; u++)
	{if (max < numbers[i][u])
					max = numbers[i][u];
	}
return max;
}

When you declare a variable, array, etc, you don't want to try and initialize it afterwards. Keep in mind that there's a difference between declaring a variable and initializing a variable.

To make a long story short, if you declare a variable in a function or file, for instance, don't try to declare it again.

Look over your code to find the faults. If not that, then when you try and compile it, it should indicate where the issue is.

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.