2d array max min

I dont understand the error

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<cstdlib>

using namespace std;

class MaxMin{
    int r,c,max,min;
    int **p;
    public:
        void insert(){
        cout<<"Enter Row & Col ";
        cin>>r>>c;
        p=new int*[r];
        for(int i=0;i<r;i++) p[i]=new int[c];
        for(int i=0;i<r;i++)
        for(int j=0;j<c;j++)

        p[i][j]=rand()%10;
        }

        void findmaxmin(){
            max=[0][0];
            for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                if(p[i][j]>max)
                    max=p[i][j];
                if(p[i][j]<min)
                    min=p[i][j];
            }
            }

            cout << "Max = " << max << endl;
            cout << "Min = " << min << endl;
        }

        void display()
        {
            for(int i=0;i<r;i++){
                for(int j=0;j<c;j++){
                    cout<<p[i][j]<<" ";
                }cout<<endl;
            }
        }
};


int main(){
    MaxMin p;

    int r,c;
    p.insert();
    p.display();
    p.findmaxmin();



}

What error? We cannot read your mind.
Line 22: max is a simple integer. What do you think that line does?
OHH sorryy i made a silly mistakes ......here i should intialize Max=Min=0
One has to start from the first error; some of the later errors may be mere by-products if the compiler is already "sidetracked".

An error message might look "unrelated", if the syntax error such that compiler's "best guess" is way off.

Each error message should list at least the line, where the error occurs (and perhaps column too). That (the row) is the first place to look at, but some errors are actually in preceding lines.


The line 22. The cpp.sh -site's compiler talks about "lambda". Lambda function syntax was added in C++11. Your code does not have proper lambda function on line 22, and most likely isn't supposed to have one either. Assuming that you are not familiar with lambdas, it is no surprise that the error message makes no sense at all.

The code does have an error, and in this case the error message is of limited help (apart from pointing to the line). Your compiler might give a different message. Impossible to tell, for you did not show the error to us.


Edit:

You do use new, but have no delete anywhere. You hopefully have planned to fix that later, but it is easy to forget such (crucial) details. It is a bad habit to start like that. You have to add three member functions to sort that out. Do you know which?

What if you call display() or findmaxmin() before calling insert()? Is the MaxMin object consistent before insert()? How should you fix that?

You do have using namespace std;, like almost every cursed example has. Your program does not use all symbols from namespace std, so it would be more precise and less error-prone to replace that statement with:
1
2
3
using std::cin;
using std::cout;
using std::endl;


The findminmax() does not compute the min correctly.
Last edited on
Topic archived. No new replies allowed.