How do you put the dynamic body into a function?



  • How do you transfer the dynamic mass from the generating function to the sorting function?

    I've been through the references, after asking for a sorting algorithm, nothing happens.Press any key to continue😞

    #include "stdafx.h"
    #include <stdlib.h>
    #include <iostream>
    #include <ctime>
    #include <iomanip>
    using namespace std;
    int genmass2 (int num)
    {
    int *arr_fsort = new int[num]; // Выделение памяти для массива
    srand(time(NULL));
    for (int i = 0; i<num; i++) 
    {
    // Заполнение массива и вывод значений его элементов
    arr_fsort[i]=( rand() % 600 + 0); // диапазон равен от 1 до 600 включительно
    cout<<dec<< "Znachenie " << i << " elementa " << arr_fsort[i] << endl;
    }
    

    return 0;
    }
    int vuborom_sort(int num, int*arr_fsort)
    {
    for (int i=0; i<num; i++) {
    int min=arr_fsort[i], imin = i;
    for (int j=i+1; j<num; j++) {
    if(arr_fsort[j]<min) {
    min = arr_fsort[j];
    imin = j;
    }
    }
    if (imin != i) {
    int t = arr_fsort[i];
    arr_fsort[i] = arr_fsort[imin];
    arr_fsort[imin] = t;
    }
    }
    cout << endl;
    for (int i=0; i<num; i++) {
    cout <<"Otsortirovannue elementu(metod prostogo vubora): "<<arr_fsort[i]<<endl;
    }
    return 0;
    }
    int main ()
    {
    int num;
    cout<<"Vvedite razmer massiva: "<<endl;
    cin>>num;
    int count;
    cout<<"Vvedite tip generiruemogo massiva 2-obuchnai gen : ";
    cin>>count;
    switch (count)
    {
    case 2:
    {
    genmass2(num);
    break;
    }
    return 0;
    }
    cout<<"Vubor tipa sortirovki 2-sortirovka prostum vuborom : " <<endl;
    cin>>count;
    switch (count)
    {
    case 1:
    {
    int vuborom_sort (int num,int *arr_fsort);
    break;
    }
    return 0;
    }
    }



  • Your function of building a mass

    int genmass2 (int num)
    {
    int *arr_fsort = new int[num]; // Выделение памяти для массива
    srand(time(NULL));
    for (int i = 0; i<num; i++) 
    {
    // Заполнение массива и вывод значений его элементов
    arr_fsort[i]=( rand() % 600 + 0); // диапазон равен от 1 до 600 включительно
    cout<<dec<< "Znachenie " << i << " elementa " << arr_fsort[i] << endl;
    }
    

    return 0;
    ^^^^^^^^^
    }

    Reset from function 0. She doesn't return the index anywhere to set up. Therefore, the function main m vuborom_sort They have no access to the mass.

    Besides, you don't give a sorting function. In this fragment of the code

    case 1:
    {
    int vuborom_sort (int num,int *arr_fsort);
    break;
    }

    Proposal

    int vuborom_sort (int num,int *arr_fsort);

    is not a challenge to the function, but is merely a local declaration of the function.

    And I think that's the proposal.

    arr_fsort[i]=( rand() % 600 + 0); // диапазон равен от 1 до 600 включительно
    ^^^

    You mean 1 instead of 0.

    The programme may look as follows:

    #include "stdafx.h"
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <iomanip>

    int * generate_array( size_t n )
    {
    const int UPPER_VALUE = 600;

    if ( n == 0 ) return NULL;
    
    int *a = new int[n]; // Выделение памяти для массива
    
    std::srand( ( unsigned int )std::time( NULL ) );
    
    for ( size_t i = 0; i &lt; n; i++ ) 
    {
        // Заполнение массива и вывод значений его элементов
        a[i] = rand() % UPPER_VALUE + 1; // диапазон равен от 1 до 600 включительно
    } 
    
    return a;
    

    }

    void display_array( int *a, size_t n )
    {
    for ( size_t i = 0; i < n; i++ ) std::cout << std::setw( 2 ) << a[i] << ' ';
    std::cout << std::endl;
    }

    void selection_sort( int *a, size_t n )
    {
    for ( size_t i = 0; i < n; i++ )
    {
    size_t min = i;
    for ( size_t j = i + 1; j < n; j++ )
    {
    if ( a[j] < a[min] ) min = j;
    }

        if ( min != i ) 
        {
            int t = a[i];
            a[i] = a[min];
            a[min] = t;
        }       
    }
    

    }

    int main()
    {
    size_t n;

    std::cout &lt;&lt; "Enter array size: ";
    std::cin &gt;&gt; n;
    
    if ( n != 0 )
    {
        int *a = NULL;
    
        enum generating_type { NONE_GENERATING = 0, NORMAL_GENERATING = 2 };
    
        int type;
    
        std::cout &lt;&lt; "\nVvedite tip generiruemogo massiva: " 
                &lt;&lt; NORMAL_GENERATING &lt;&lt; " - obuchnai gen :  ";
        std::cin &gt;&gt; type;
    
        switch ( type )
        {
            case NORMAL_GENERATING:
                a = generate_array( n );
                break;
            default:
                type = NONE_GENERATING;
                break;
        }
    
        if ( type == NONE_GENERATING ) return 0; 
    
        display_array( a, n );
    
        enum sorting_type { NONE_SORT = 0, SELECTION_SORT = 2 };
    
        std::cout &lt;&lt; "\nVubor tipa sortirovki "
                &lt;&lt; SELECTION_SORT &lt;&lt; " - sortirovka prostum vuborom :  ";
        std::cin &gt;&gt; type;
    
        switch ( type )
        {
            case SELECTION_SORT:
                selection_sort( a, n );
                break;
            default:
                type = NONE_SORT;
                break;
        }            
    
        display_array( a, n );
    
        delete [] a;
    }
    
    return 0;
    

    }

    Its conclusion to the console may be as follows:

    Enter array size: 10

    Vvedite tip generiruemogo massiva: 2 - obuchnai gen : 2
    441 496 18 529 193 15 296 178 478 203

    Vubor tipa sortirovki 2 - sortirovka prostum vuborom : 2
    15 18 178 193 203 296 441 478 496 529

    And it's better to write Latin letters with English phrases, not Russians. Otherwise, it makes it difficult to read the programme.




Suggested Topics

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