Function findMax and findMin

How can one create an array of two functions which accepts integer array along with its size as two input variables, and return maximum or minimum respectively, and showing its function prototype


This is what i have so far, but is not fully right :(

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

int main()
{
int findMax(int[],int n);
int max=0;

int n;
cout << " Enter an integer that represents the size of input variables: ";
cout << endl;
cin >> n;

for(int i = 1; i < n; i++)
{
if(array[i] > max)
cout << max = array{i};
}

system("Pause");
return 0;
}
int findMax(int [], int n)
{
return max;
}

1
2
3
4
5
6
7
8
9
10
11
struct A
{
	static int f1( int [], int ) { std::cout << "A::f1\n"; return ( 1 ); }
	static int f2( int [], int ) { std::cout << "A::f2\n"; return ( 2 ); }
};

int ( *fp[] )( int [], int ) = { A::f1, A::f2 };

int a[2];

for ( int i = 0; i < 2; i++ ) fp[i]( a, 2 );
Last edited on
an array of two functions
Be careful what you wish for...


As for actually being helpful,
1
2
3
4
int findMax(int [], int n)
{ 
    return max; 
}
You need to give a name to that int array, then you have the size 'n' and you can do what you normally do to find the max.
Last edited on
Thanks
But how do we combine two functions in one program?
Like
Int findMin and findMax ?
J have not understood your question. I showed you how to declare an array of funcrion pointers. What is the problem?
vlad, you have an array of two function pointers. The OP wants "an array of two functions":
http://ideone.com/SUK0Mf
LB wrote:
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
int MyMinFunc(int a, int b)
{
    return a < b ? a : b;
}
int MyMaxFunc(int a, int b)
{
    return a > b ? a : b;
}
 
template<typename T>
class RefList
{
    struct Node
    {
        T &t;
        Node *n;
        Node() = delete;
        Node(T &t) : t(t), n(0) {}
        Node(const Node &) = delete;
        Node &operator=(const Node &) = delete;
        ~Node(){}
    } *h;
public:
    RefList() : h(0) {}
    RefList(const RefList &) = delete;
    RefList &operator=(const RefList &) = delete;
    void AddLast(T &t)
    {
        if(!h) h = new Node(t);
        else
        {
            Node *n = h;
            while(n->n) n = n->n;
            n->n = new Node(t);
        }
    }
    T &operator[](unsigned i)
    {
        if(!i) return h->t;
        Node *n = h;
        while(i--) n = n->n;
        return n->t;
    }
    ~RefList()
    {
        while(h)
        {
            Node *n = h;
            h = h->n;
            delete n;
        }
    }
};
 
#include <iostream>
 
int main()
{
    RefList<int (int, int)> MyArray;
    MyArray.AddLast(MyMinFunc);
    MyArray.AddLast(MyMaxFunc);
    std::cout << MyArray[0](4, 7) << std::endl;
    std::cout << MyArray[1](4, 7) << std::endl;
}
The RefList class could be written in C++03 but I just wanted to use the = delete feature of C++11.
Last edited on
my problem is how to edit this program. I don't need a new program with function i never learn in my class yet. may you please show me how to solve the question using my program

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

int main()
{
int findMax(int[],int n);
int max=0;

int n;
cout << " Enter an integer that represents the size of input variables: ";
cout << endl;
cin >> n;

for(int i = 1; i < n; i++)
{
if(array[i] > max)
cout << max = array{i};
}

system("Pause");
return 0;
}
int findMax(int [], int n)
{
return max;
}

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

int main ()
{
int a[5];
int i;

for(int i = 0; i < 5; i++)
{
cout << "Enter the variables to return the maximum or minimum value of the array." << endl;
cin >> a[i];
}

double findMax;
double findMin;
findMax = a[0];
findMin = a[0];
int b = 0;

while ( b< 5)
{ if ( a[b] > findMax)
{ findMax = a[b];
}
else if ( a[b] < findMin)
{ findMin = a[b];
}
b++;
}

cout << " The maximum in an array is : " << findMax << endl;
cout << " The minimum in an array is : " << findMin << endl;

system("Pause");
return 0;
}
If I understand you correctly you need simply to use separate functions for finding minimum and maximum.


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
#include <iostream>
#include <cstdlib>

using namespace std;

int FindMax( const int [], int );
int FindMin( const int [], int );
 
int main ()
{
   const int N = 5; 
   int a[N];

   for( int i = 0; i < N; i++ )
   {
      cout << "Enter the values to find the maximum or minimum among them." << endl;
      cin >> a[i]; 
   }
 
   cout << " The maximum in an array is : " << FindMax( a, N ) << endl;
   cout << " The minimum in an array is : " << FindMin( a, N ) << endl;

   system("Pause");
 
   return 0;
}

int FindMax( const int a[], int n )
{
   int max = a[0];

   for ( int i = 1; i < n; i++ )
   {
      if ( max < a[i] ) max = a[i];
   }

   return ( max );
}

int FindMin( const int a[], int n )
{
   int min = a[0];

   for ( int i = 1; i < n; i++ )
   {
      if ( a[i] < min ) min = a[i];
   }

   return ( min );
}
Last edited on
Vlad u have saved my life thanks so much!!
Topic archived. No new replies allowed.