-2

So basically i need to replace the two largest elements of an array, inputed by user. I don't understand why doesn't my code working.

#include <iostream>
    using namespace std;
    double Max (double a[], int n) { 
    double max1=0, max2=0;
    
    for (int i=0; i<n; i++){
        if(max1 < a[i]){
            max2 = max1;
            max1 =a[i];}
        else
            if(max2 < a[i]){
            max2 = a[i];}
    }
    return max2,max1;
    }
    
    int main () { 
    setlocale(0,".1251");
    double a[7];
    cout<<"Input 7-digit array:\n";
    
    for (int i=0; i<7; i++) cin >> a[i];
    
    for (int i=0; i<7; i++) {
        cout<<a[i]<<"\t";
    }
    
    system ("pause>>void");
    return 0;
    }

1 Answers1

0

The function double Max (double a[], int n) is only defined, but you haven't called it. As it is right now, you are only outputting the same digits you inputted. Also double Max (double a[], int n) can only return 1 double, so I'm assuming you were trying to return two variables return max2,max1; at once which isn't supported. A fix to this could be by using a std::vector as data type and returning a 2 element array of max1 & max2 and then you would have to finish the code by actually calling the double Max (double a[], int n) function.

vector Maximums = Max(a[],7);
cout<< "\n max 1:\n" << Maximums[0] << "\n max 2:\n" << Maximums[1]