Home>

For example, in an array{3 5 -4 6 -2 7 9 10 -11 15 13}subsequence{5 -4 6 -2 7}-alternating. Need to replace-4and-2on5(the minimum value of a positive element in this alternating sequence)
Output:3 5 5 6 5 7 9 10 -11 15 13

I get a completely different conclusion, namely:{3 5 5 6 6 7 9 10 10 15 13}

Please tell me how to fix the code.

cout <
<
 "n:";
    int n= 0;
    cin >
>
 n;
    int * array= new int [n];
    for (int i= 0; i <
 n; i ++) {
        cin >
>
 array [i];
    }
    cout <
<
 endl;
    int min= 0;
    for (int i= 0; i <
 n; i ++) {
        if (array [i] >
 0)
            min= array [i];
        if ((array [i] >
 0) &
&
 (array [i + 1] <
 0))
            array [i + 1]= min;
        cout <
<
 array [i] <
<
 "\ t";
    }
c++
  • Answer # 1

    The example produces two such sequences:

    1. {5 -4 6 -2 7};
    2. {10 -11 15};

    In your code, if the minimum positive number is in the middle or end of the sequence, the value will not be taken.

    if ((array [i] > 0) & & (array [i + 1] < 0))-exceeding the limit of the array ati= n -1...

    There is no dynamic array deletion at the end of the program -operatordelete [] _pointername_...

    I wrote the code, tested it, it seems to work as the task is set.

    #include &lt;
    iostream &gt;
    using std :: cout;
    using std :: cin;
    using std :: endl;
    int main ()
    {
        setlocale (LC_ALL, "");
        /* cout &lt;
    &lt;
     "Enter n:";
        int n= 0;
        cin &gt;
    &gt;
     n;
        int * arr= new int [n];
        for (int i= 0; i &lt;
     n; ++ i) {
            cin &gt;
    &gt;
     arr [i];
        } * /
        const int n= 11;
        int arr [n]= {3, 5, -4, 6, -2, 1, 9, 15, -11, 10, 13};
        int min;
        cout &lt;
    &lt;
     "Source array: \ t \ t";
        for (auto &
    ind: arr) {
            cout &lt;
    &lt;
     ind &lt;
    &lt;
     "";
        }
        cout &lt;
    &lt;
     endl &lt;
    &lt;
     endl;
        for (int i= 0, j= 0; i &lt;
     n; ++ i) {
            int k= 0;
            if (((arr [i] &gt;
    = 0) &
    &
     (arr [i + 1] &lt;
     0) &
    &
     i!= n -1) || ((arr [i] &lt;
     0) &
    &
     (arr [i + 1] &gt;
    = 0) &
    &
     i!= n -1)) {
                j= i;
                if (arr [i] &gt;
     0) {
                    min= arr [i];
                }
                else {
                    min= arr [i + 1];
                }
                while (((arr [j] &gt;
    = 0) &
    &
     (arr [j + 1] &lt;
     0) &
    &
     j &lt;
     n) || ((arr [j] &lt;
     0) &
    &
     (arr [j + 1] &gt;
    = 0) &
    &
     j &lt;
     n)) {
                    if (arr [j] &gt;
    = 0 &
    &
     arr [j] &lt;
     min) {
                        min= arr [j];
                    }
                    ++ k;
                    ++ j;
                }
                if (arr [j] &gt;
    = 0 &
    &
     arr [j] &lt;
     min) {
                    min= arr [j];
                }
                for (int t= i; t &lt;
     i + k; ++ t) {
                    if (arr [t] &lt;
     0) {
                        arr [t]= min;
                    }
                }
                i += k;
            }
        }
        cout &lt;
    &lt;
     "Modified array: \ t";
        for (auto &
    ind: arr)
        {
            cout &lt;
    &lt;
     ind &lt;
    &lt;
     "";
        }
        cout &lt;
    &lt;
     endl &lt;
    &lt;
     endl;
        //delete [] arr;
        system ("pause");
        return 0;
    }
    

    The result of the program:

    Source array: 3 5 -4 6 -2 1 9 15 -11 10 13
    Modified array: 3 5 1 6 1 1 9 15 10 10 13
    Press any key to continue. ... ...
    
  • Answer # 2

    The example produces two such sequences:

    1. {5 -4 6 -2 7};
    2. {10 -11 15};

    In your code, if the minimum positive number is in the middle or end of the sequence, the value will not be taken.

    if ((array [i] > 0) & & (array [i + 1] < 0))-exceeding the limit of the array ati= n -1...

    There is no dynamic array deletion at the end of the program -operatordelete [] _pointername_...

    I wrote the code, tested it, it seems to work as the task is set.

    #include &lt;
    iostream &gt;
    using std :: cout;
    using std :: cin;
    using std :: endl;
    int main ()
    {
        setlocale (LC_ALL, "");
        /* cout &lt;
    &lt;
     "Enter n:";
        int n= 0;
        cin &gt;
    &gt;
     n;
        int * arr= new int [n];
        for (int i= 0; i &lt;
     n; ++ i) {
            cin &gt;
    &gt;
     arr [i];
        } * /
        const int n= 11;
        int arr [n]= {3, 5, -4, 6, -2, 1, 9, 15, -11, 10, 13};
        int min;
        cout &lt;
    &lt;
     "Source array: \ t \ t";
        for (auto &
    ind: arr) {
            cout &lt;
    &lt;
     ind &lt;
    &lt;
     "";
        }
        cout &lt;
    &lt;
     endl &lt;
    &lt;
     endl;
        for (int i= 0, j= 0; i &lt;
     n; ++ i) {
            int k= 0;
            if (((arr [i] &gt;
    = 0) &
    &
     (arr [i + 1] &lt;
     0) &
    &
     i!= n -1) || ((arr [i] &lt;
     0) &
    &
     (arr [i + 1] &gt;
    = 0) &
    &
     i!= n -1)) {
                j= i;
                if (arr [i] &gt;
     0) {
                    min= arr [i];
                }
                else {
                    min= arr [i + 1];
                }
                while (((arr [j] &gt;
    = 0) &
    &
     (arr [j + 1] &lt;
     0) &
    &
     j &lt;
     n) || ((arr [j] &lt;
     0) &
    &
     (arr [j + 1] &gt;
    = 0) &
    &
     j &lt;
     n)) {
                    if (arr [j] &gt;
    = 0 &
    &
     arr [j] &lt;
     min) {
                        min= arr [j];
                    }
                    ++ k;
                    ++ j;
                }
                if (arr [j] &gt;
    = 0 &
    &
     arr [j] &lt;
     min) {
                    min= arr [j];
                }
                for (int t= i; t &lt;
     i + k; ++ t) {
                    if (arr [t] &lt;
     0) {
                        arr [t]= min;
                    }
                }
                i += k;
            }
        }
        cout &lt;
    &lt;
     "Modified array: \ t";
        for (auto &
    ind: arr)
        {
            cout &lt;
    &lt;
     ind &lt;
    &lt;
     "";
        }
        cout &lt;
    &lt;
     endl &lt;
    &lt;
     endl;
        //delete [] arr;
        system ("pause");
        return 0;
    }
    

    The result of the program:

    Source array: 3 5 -4 6 -2 1 9 15 -11 10 13
    Modified array: 3 5 1 6 1 1 9 15 10 10 13
    Press any key to continue. ... ...