min_max function

How make a function that calculates the minimum and maximum of array and uses 2 reference type parameters to return the results?

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

}
return mini,maxi;

}
Don't return anything. That's the point of using reference type parameters.
then the function should be void?
Sure should.
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];

}

}


it's ok?
how call the function in the main program?
how call the function in the main program?


Have an int that is the size of the array.
Have the array of ints.
Have an int that will be the max value.
Have an int that will the the minimum value.

min_max( the_size_of_the_array, the_array, min_value, max_value);

How to call a function is fairly basic. Very, very basic in fact. This is definitely something you should already know if you intend to write functions.
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];

}
cout<<"minimul is"<<mini;
cout<<"maximul is "<<maxi;
}
int main()
{
int x,y,n,a[6];
citire(n,a);
min_max(n,a,mini,maxi);
return 0;
}



the program doesn't display what it should.
Hello georgiana muresan19,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



the program doesn't display what it should.
Not surprised.

The program does not compile and therefor can not be run.

Once I make it run-able I believe the if statements in the function are backwards. Sometimes the order of the if condition does make a difference.

I think that citire(n,a); may be a function to input the numbers, but since it is missing I am not sure.

It is usually best to post a complete program that will compile and run.

Hope that helps,

Andy
This is code:

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

using namespace std;
void citire(int n,int a[])
{
    cout << "n=";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cout << "a[" << i << "]=";
        cin >> a[i];
    }
}
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];

    }
    cout<<"minimul este"<<mini;
    cout<<"maximul este "<<maxi;
}
int main()
{
    int x,y,n,a[6],mini,maxi;
    citire(n,a);
    min_max(n,a,mini,maxi);
    return 0;
}
You need to pass n by reference to citire() so the value gets set in main() too.

It looks like that code will work, but let me make two suggestions to make to make it
cleaner and more useful.

First, min_max should set mini and maxi to the index of the min and max values, not the values themselves. Sometimes you want to know where the values occur, not just what they are.

Second, you should print the results in main() not in min_max(). That makes min_max more useful. It can be called for other purposes than just to print out the values.
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
#include <iostream>

using namespace std;
void citire(int &n,int a[])
{
    cout << "n=";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cout << "a[" << i << "]=";
        cin >> a[i];
    }
}
void min_max(int n,int a[],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 x,y,n,a[6],mini,maxi;
    citire(n,a);
    min_max(n,a,mini,maxi);
    cout<<"minimul este "<<a[mini] << '\n';
    cout<<"maximul este "<<a[maxi] << '\n';
    return 0;
}

Topic archived. No new replies allowed.