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];

}

}
Define it inside the class.
And your instructor probably intends that you remove the n and int a[] parameters of the function as well, since they are now accessible from the class scope.
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
#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 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 main()
{ int maxi,mini;
    IntArray ia;
    ia.citire();
    ia.min_max();
    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?
ia.min_max();
This is an attempt to call the function IntArray::min_max()

What function did you actually write? IntArray::min_max(int&mini,int &maxi)

See the difference?
error: expected primary-expression before 'int'|
I can't see your screen. You'll have to tell us the code with the error.
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{ int maxi,mini;
    IntArray ia;
    ia.citire();
    ia.afisare();
    IntArray::min_max(int&mini,int&maxi);
    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 line 6
Last edited on
That's not how to call a class member function.

Which is very strange, because on line 4 and line 5, you correctly call a class member function. Why has it all gone wrong on line 6? How did you forget how to call a member function, even though you just did it on line 4 and line 5?

Remember, the function min_max has parameters. Have you ever called a function before that has parameters?
Last edited on
Topic archived. No new replies allowed.