how do i convert the min_max function to the method of the IntArray class?

how do i convert the min_max function to the method of the IntArray class?
class IntArray
{private :
int a[];
int n;
..........
};
void min_max(int n,int a[],int&mini,int &maxi)

{
mini=a[0];
maxi=a[0];
for (int i=1; i<n; i++)
{
if (mini>a[i])
mini=a[i];
if (maxi<a[i])
maxi=a[i];

}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class IntArray
{
private:
    int a[];
    int n;
public:
    void min_max(int &mini, int &maxi)
    {
        mini = a[0];
        maxi = a[0];
        for (int i = 1; i < n; i++)
        {
            if (mini > a[i])
                mini = a[i];
            if (maxi < a[i])
                maxi = a[i];
        }
    }
};

But see my suggestion in the other thread to return the index in mini and maxi instead of the value.

Note that I don't pass n or a to the method. The method will use the n and a members of the class.
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <cmath>
using namespace std;
class IntArray {
  private:
    int n;

  public:
      int a[3];
      void citire()
{
    cout << "n=";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cout << "a[" << i << "]=";
        cin >> a[i];
    }
}
      /**void afisare() {
          for (int i = 0; i < n; i++) {
              cout << a[i] << " ";
          }
          cout << endl;
      }*/

void min_max(int&mini,int &maxi)
{
    mini=0;
    maxi=0;
    for (int i=1; i<n; i++)
    {
        if (a[mini]>a[i])
            mini=i;
        if (a[maxi]<a[i])
            maxi=i;

    }
}
/**int suma()
    {
        int s=0;
        for(int i=0; i<n; i++)
            s=s+a[i];
        return s;
    }
    double media()
    {
        double m=suma()/n;
        return m;
    }
    double devstd()
    {
        double deviatia;
        int s=0;
        double m=media();
        for (int i=0; i<n; i++)
            s=s+pow(a[i]-m,2);
        deviatia=sqrt(s/n);
        return deviatia;

    }**/
};
int main()
{ int maxi,mini;
    IntArray ia;
    ia.citire();
    /*ia.afisare();**/
    ia.min_max();
    /*cout<<"media este "<<ia.media()<<endl;
    cout<<"deviatia este "<<ia.devstd();**/
    cout<<"minimul este "<<ia.a[mini] << '\n';
    cout<<"maximul este "<<ia.a[maxi] << '\n';
    return 0;
}

Error : no matching function for call to 'IntArray::min_max()'|
why?

Stop double posting, Repeater is already said what was wrong in the other thread!
Last edited on
Topic archived. No new replies allowed.