MAX ELEMENT IN An ARRAY

hi guys. i am trying to find the max element in a given row of a 2d array. I have this code but it does not work. please assist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <cstdlib>
#include <cctype>
#include <stdlib.h>
#include "MatrixReloaded.h"
#include <algorithm>
using namespace std;

const int row=3;
const int col=4;
typedef int functionArray[col][col];
functionArray myArray;


namespace MatrixReloaded
{

void Max()
{
	row=2;
	int largest=myArray[row][0];

	for ( col=1;col <col;col++)
		if(largest <myArray[row][col]);
	       largest=myArray[row][col];

	       cout << "The maximum for row 2 = " << largest << endl;

}

void min()
{

}
void average()
{

}

}t the code you need help with here.
Last edited on
You've defined "Row" and "col" as const ints at the beginning, but you reuse those variables in Max(). You can't assign to a const int.

Also look at the loop:
 
for ( col=1;col <col;col++)


"col<col" will never EVER be true: a variable is never less than itself.

But you're REALLY close to having it right. "Row" and "col" define the bounds of the array. Use different variables (maybe r and c) as indexes INTO the array.

Dave
got it...working perfectly. thanks dave
Line 12 looks suspicious.
 
typedef int functionArray[col][col];


Do you mean:
 
typedef int functionArray[row][col];


The error is harmless since you're merely allocating an extra row.
haha ye my bad i noticed that error as well.fixed it
Topic archived. No new replies allowed.