Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to include multiple variables in max() function?

I have this simple but long code that outputs the electron arrangement when user inputs the atomic number of wanted element.

    #include<iostream>
using namespace std;

int main()
{
    int n, s1, s2, p2, s3, p3, s4, d3, p4, s5, d4, p5, s6, f4, d5, p6, s7, f5, d6, p7;
    cout << "Atomic number: ";
    cin >> n;
    
    if(n<=2){
    s1 = n;
    }
    else if(n>=2){
    s1 = 2;
    }
    
    if(n<=4){
    s2 = n-2;
    }
    else if(n>=4){
    s2 = 2;
    }
    
    if(n<=10){
    p2 = n-4;
    }
    else if(n>=10){
    p2 = 6;
    }
    
    if(n<=12){
    s3 = n-10;
    }
    else if(n>=12){
    s3 = 2;
    }
    
    if(n<=18){
    p3 = n-12;
    }
    else if(n>=18){
    p3 = 6;
    }
    
    if(n<=20){
    s4 = n-18;
    }
    else if(n>=20){
    s4 = 2;
    }
    
    if(n<=30){
    d3 = n-20;
    }
    else if(n>=30){
    d3 = 10;
    }
    
    if(n<=36){
    p4 = n-30;
    }
    else if(n>=36){
    p4 = 6;
    }
    
    if(n<=38){
    s5 = n-36;
    }
    else if(n>=38){
    s5 = 2;
    }
    
    if(n<=48){
    d4 = n-38;
    }
    else if(n>=48){
    d4 = 10;
    }
    
    if(n<=54){
    p5 = n-48;
    }
    else if(n>=54){
    p5 = 6;
    }
    
    if(n<=56){
    s6 = n-54;
    }
    else if(n>=56){
    s6 = 2;
    }
    
    if(n<=70){
    f4 = n-56;
    }
    else if(n>=70){
    f4 = 14;
    }
    
    if(n<=80){
    d5 = n-70;
    }
    else if(n>=80){
    d5 = 10;
    }
    
    if(n<=86){
    p6 = n-80;
    }
    else if(n>=86){
    p6 = 6;
    }
    
    if(n<=88){
    s7 = n-86;
    }
    else if(n>=88){
    s7 = 2;
    }
    
    if(n<=102){
    f5 = n-88;
    }
    else if(n>=102){
    f5 = 14;
    }
    
    if(n<=112){
    d6 = n-102;
    }
    else if(n>=112){
    d6 = 10;
    }
    
    if(n<=118){
    p7 = n-112;
    }
    else if(n>=118){
    p7 = 6;
    }


    if(d3==4 && s4==2){
    d3++;
    s4--;
    }
    
    if(d3==9 && s4==2){
    d3++;
    s4--;
    }
    
    if(d4==4 && s5==2){
    d4++;
    s5--;
    }
    
    if(d4==9 && s5==2){
    d4++;
    s5--;
    }
    
    if(d4==4 && s5==2){
    d4++;
    s5--;
    }
    
    cout << "s1: " << s1;
    cout << "\ns2: " << s2; 
    cout << "\np2: " << p2;
    cout << "\ns3: " << s3;
    cout << "\np3: " << p3;
    cout << "\ns4: " << s4;
    cout << "\nd3: " << d3;
    cout << "\np4: " << p4;
    cout << "\ns5: " << s5;
    cout << "\nd4: " << d4;
    cout << "\np5: " << p5;
    cout << "\ns6: " << s6;
    cout << "\nF4: " << f4;
    cout << "\nd5: " << d5;
    cout << "\np6: " << p6;
    cout << "\ns7: " << s7;
    cout << "\nf5: " << f5;
    cout << "\nd6: " << d6;
    cout << "\np7: " << p7;
    
    return 0;
}

My problem with it is, when I input 57 for example, some variables are negative because code subtracts a certain number from inputted value. So I need to make those variables 0 if they are smaller than 0. I have 2 ways to do this and first is to write 19 new else if statements which is not efficient. My other idea is to use max() function but I don’t know how to use it to include all 19 values and turn them to 0 at once. What do you all think I should do and how can I include multiple variables in max()?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use std::max with an initializer list:

auto max_value = std::max({x, y, z});

Note that the elements in the list will be copied into the initializer list though, so if time is of the essence, you could just call it multiple times:

auto& max_element = std::max(std::max(x, y), z);

If you have many values, you may want to use a std::vector and a standard algorithm, like std::max_element:

std::vector<int> values { ... };
auto& max_element = *std::max_element(values.begin(), values.end());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading