Is there a smaller way to solve the question below? and know if it's correct

0
  • Create an algorithm that contains a vector of integers. Then your algorithm should generate two more vectors. The first vector must store the numbers of the initial vector in ascending order and the second must store the values of the initial vector in descending order.

code:

    #include <iostream>

      using namespace std;

      int main()
       {
    int vetor[5] = {8, 2, 7, 3, 6};
    int cresc[5] = {vetor[0], vetor[1], vetor[2], vetor[3], vetor[4]};
    int decre[5] = {vetor[0], vetor[1], vetor[2], vetor[3], vetor[4]};

        for(int i = 0; i < 5; i++){
            for(int j = 0; j < (5 - 1); j++){
                if(cresc[i] < cresc[j]){
                    int aux = cresc[i];
                    cresc[i] = cresc[j];
                    cresc[j] = aux;
                }
            }
        }
        for(int a = 0; a < 5; a++){
            for(int b = 0; b < (5 - 1); b++){
                if(decre[a] > decre[b]){
                    int aux2 = decre[b];
                    decre[b] = decre[a];
                    decre[a] = aux2;
                }
            }
        }

        for(int x = 0; x < 5; x++){
            cout << "vetor crescente[" << x << "]: " << cresc[x] << "" << endl;
        }
        cout << "--------------" << endl; 
        for(int z = 0; z < 5; z++){
            cout << "vetor decrescente[" << z << "]:" << decre[z] << endl;
        }
      return 0;
     }
    
asked by anonymous 17.09.2017 / 22:24

4 answers

4

Your resolution is correct yes, but you can reduce, optimize and improve it a bit:

  • You can make arrays without being of fixed size using a variable or a constant.
  • You do not need to do two sorts because the ascending ordering is inverse to decreasing. With this you can after the first order copy invert to the second.
  • Using up to a similar sort algorithm, insertion sort , can construct both arrays at the same time.

Applying these modifications would look like this:

#include <iostream>

using namespace std;

#define TAM 5 //constante para o tamanho aqui

int main()
{
    //agora cria os arrays com base num tamanho
    int vetor[TAM] = {8, 2, 7, 3, 6};
    int cresc[TAM], decre[TAM]; 

    //agora tem de se copiar do vetor para o cresc pois o tamanho pode variar
    for (int i = 0; i < TAM; ++i){
        cresc[i] = vetor[i];
    }

    for(int i = 0; i < TAM; i++)
    {
        for(int j = i + 1; j < TAM; j++)
        {
            if(cresc[j] < cresc[i])
            {
                int aux = cresc[i];
                cresc[i] = cresc[j];
                cresc[j] = aux;
            }
        }

        //ao mesmo tempo que constroi o cresc colocando os mais pequenos na parte inicial
        //coloca também os maiores na parte final do decre
        decre[TAM-i-1] = cresc[i];
    }

    //resto das escritas dos arrays igual
    ...

    return 0;
}

You could of course use more efficient sorting algorithms such as quicksort or mergesort that guarantee O (nlogn) complexity but are already a little more complex and probably will not part of the solutions waiting for the exercise.

    
18.09.2017 / 01:04
4

For the vector, use the std :: vector . To manipulate the data, STL has several algorithms that you can use, such as std::sort . Here is an example in which you do what you want:

#include <vector>
#include <algorithm>
#include <iostream>

template <typename T>
void imprime(const T& v)
{
    for(auto&& i : v)
        std::cout << i << " ";
    std::cout << std::endl;
}

int main()
{
    auto vetor = std::vector<int>({8, 2, 7, 3, 6});

    auto cresc = vetor;
    std::sort(cresc.begin(), cresc.end());

    auto decre = decltype(cresc)(cresc.rbegin(), cresc.rend());

    imprime(vetor);
    imprime(cresc);
    imprime(decre);
    return 0;
}

See working on coliru

PS. You have to use C ++ version 11 or higher

    
18.09.2017 / 02:31
2

In C ++ 98 you can do something like:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int nums[] = { 8, 2, 7, 3, 6 };


void exibir( vector<int> & v )
{
    for( vector<int>::iterator it = v.begin(); it != v.end(); ++it )
        cout << *it << " ";

    cout << endl;
}


int main( void )
{
    vector<int> vetor( nums, nums + (sizeof(nums)/sizeof(nums[0])) );

    vector<int> cresc( vetor );
    sort( cresc.begin(), cresc.end() );

    vector<int> decre( cresc );
    reverse( decre.begin(), decre.end() );

    exibir( vetor );
    exibir( cresc );
    exibir( decre );

    return 0;
}
    
18.09.2017 / 21:37
0

Solution in C:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int qsort_crescent( const void * a, const void * b ) { return *((int*)a) - *((int*)b); }
int qsort_decrescent( const void * a, const void * b ) { return *((int*)b) - *((int*)a); }


void exibir( int array[], int tam )
{
    int i = 0;

    for( i = 0; i < tam; i++ )
        printf( "%d ", array[i] );

    printf("\n");
}


int main( int argc, char * argv[] )
{
    int vetor[5] = { 8, 2, 7, 3, 6 };

    int cresc[5];
    int decre[5];

    memcpy( cresc, vetor, sizeof(vetor) );
    qsort( cresc, 5, sizeof(int), qsort_crescent );

    memcpy( decre, vetor, sizeof(vetor) );
    qsort( decre, 5, sizeof(int), qsort_decrescent );

    exibir( vetor, 5 );
    exibir( cresc, 5 );
    exibir( decre, 5 );

    return 0;
}
    
18.09.2017 / 22:19