jeudi 16 septembre 2021

What is Problem in this C++ code? It return a return value of 322277 something can anyone explain

This is my Code : Input My array to be split from half of its value then for both arrays it should find minimum and a maximum

IT SHOWS NO OUTPUT But return a value can someone help what is the problem here. USED : 1.Function for finding minimum 2. Function for finding maximum 3. in main() used a single loop for splitting the array.

   #include<bits/stdc++.h>
    using namespace std;
    int max(vector<int> v){
        int max_value = v[0];
        
        for(int i = 0 ;i<v.size();++i){
            if(v[i] > max_value){
                max_value = v[i];
            }
            
        }
        return max_value;
    }
    int min(vector<int> v){
        int min_value = v[0];
    
        for(int i = 0 ;i<v.size();++i){
            if(v[i] < min_value){
                min_value = v[i];
            }
        }
            
        return min_value;
    }

int main(){
    int n;  
    int p = 0;
    cin >> n;
    int arr[n];
    
    for(int i = 0;i < n;++i){
        cin >> arr[i];
    }

    vector<int> vect1;
    vector<int> vect2;
    int half = n/2; //2
    
    for(int x = 0;x<n;++x){
        if(x<=half){
            vect1[x] = arr[x];
        }
        else{
            
            vect2[p] = arr[x];
            ++p;
        }
    }   
    int min1 = min(vect1);
    int min2 = min(vect2);
    int max1 = max(vect1);
    int max2 = max(vect2);
    int res1 = min1-min2;
    int res2 = max1 - max2;
    int res = res1+res2; 
    cout << res;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire