Single C+++



  • Dun mass N[13]. Find and remove the minimum element and its index. If the sum of the minimum element and its index is greater than 12, all zero elements should be replaced by a minimum element, otherwise all negative multiplied by 2. Remove the mass.

    No substitute for zero and minimum element

    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    

    int main()
    {
    const int N = 13;
    int n[N];

    for (int i = 0; i &lt; N;i++) {
        cout &lt;&lt; "Vvedite elementy N[" &lt;&lt; i &lt;&lt; "]:";
        cin &gt;&gt; n[i];
    }
    
    
    int min = 0;
    int sum = 0, nul = 0;
    
    for (int i = 1; i &lt; N; i++)
        if (n[i] &lt; n[min])
        {
            min = i;
            sum = n[min] + min;
        }
    
    
    
    if (sum &gt; 12)
    {
        for (int i = 0; i &lt; N; i++)
            if (n[i] == 0)
                n[i] = n[min];
    }
    else
        for (int i = 0; i &lt; N; i++)
            if (n[i] &lt; 0)
                n[i] *= 2;
    
    for (int i = 0; i &lt; N; i++)
        std::cout &lt;&lt; n[i] &lt;&lt; ' ';
    std::cout &lt;&lt; std::endl;
    
    system("pause");
    return 0;
    

    }


    As long as I'm at this stage, I want to take out Min. Element and the index of this element. For now, my success, but it doesn't make sense why the other element first turns out, although it's not minimal, and then it's just the right element:

    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    int main()
    {
    const int N = 13;
    int n[N];

    for (int i = 0; i &lt; N;i++){
        cout &lt;&lt; "Vvedite elementy N[" &lt;&lt; i &lt;&lt; "]:";
        cin &gt;&gt; n[i];
    }
    
    int min = 0, sum = 0;
    for (int i = 0; i &lt; N; i++) {
        if (n[i] &lt; n[min])
        {
            min = i;
            cout &lt;&lt; "index: " &lt;&lt; min &lt;&lt; "; Min. element: " &lt;&lt; n[min] &lt;&lt; ";";
            cout &lt;&lt; endl;
        }
    }
    
    system("pause");
    return 0;
    

    }



  • Try not to mix two different functions into one function.

    For example, in this fragment of the code

    int min = 0;
    cout << "Vse elementy: ";
    int i = 0;
    while (i < N) {
        cout << n[i] <<" ";
        if (n[i] < min) {
            min = n[i];
            cout <<"Minimalnyi element: "<<min;
            cout << endl;
            cout <<"Index min. elementa: "<<i;
        }
        i++;
    }
    

    It's not clear what you're trying to do. Whether you want to bring all the elements of the mass to the console or to find a minimum element of the mass.

    In addition, the minimum element message should be placed after the cycle. Otherwise, you'll have a report on various minimum elements several times.

    First remove the components of the mass on the console in a separate cycle, and in another cycle find the minimum element if required.

    Consider that the primary value of the minimum element of equal 0 is not correct. All the elements in the body can be more than zero, and then your program will give the wrong result that the minimum element is 0, although it is not in the mass.

    You can, for example, remove the mass to the console as follows:

    for ( int x : n ) std::cout << x << ' ';
    std::cout << std::endl;
    

    Or if your compiler doesn't support cycles for using ranges, you can write.

    for ( int i = 0; i < N; i++ ) std::cout << n[i] << ' ';
    std::cout << std::endl;
    

    There's no reason for the variable. i To declare it off the cycle.

    The search for a minimum element in the mass may be as follows:

    int min = 0;
    

    for ( int i = 1; i < N; i++ )
    {
    if ( n[i] < n[min] ) min = i;
    }

    std::cout << "Minimalnyi element: "<< n[min] << std::endl;
    std::cout << "Index min. elementa: " << min << std::endl;

    Note that the standard algorithm is defined in C++ std::min_elementin the title <algorithm> which finds the minimum element in the container, including the masses. For example

    #include <algorithm>

    //...

    int *min = std::min_element( n, n + N );

    std::cout << "Minimalnyi element: "<< *min << std::endl;
    std::cout << "Index min. elementa: " << min - n << std::endl;

    Also n - it's not the best name for the mass. Usually a name. n is used for rocky wholesale objects. You'd better name the mass. a from word array




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2