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

Is there a way to fix the loop?

     while(k--)
    {
        int *mna=min_element(a,a+n);
        int *mxb=max_element(b,b+n);
        int ma=*find(a,a+n,*mna);
        int mb=*find(b,b+n,*mxb);
        cout<<*mna<<" *" <<*mxb<<endl;
        cout<<ma<< " "<<mb<<endl;
        if(*mna<*mxb)
        {
            b[mb-1]=*mna;
            a[ma-1]=*mxb;
        }
    }

I’m trying to swap elements in two arrays using a number of moves(k) although the code I wrote is only swapping 1 element (mna and mxb are not changing when it loops)
how can I solve it ?

>Solution :

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

Please provide more details of your context. And here is my suggestion for your current code.

    while(k--)
    {
        int *mna = min_element(a, a + n);
        int *mxb = max_element(b, b + n); 
        if (*mna < *mxb)
        {
            swap(*mna, *mxb);
        }
    }
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