Is there a smaller way to resolve the question below? and know if it is correct

Asked

Viewed 123 times

0

  • Create an algorithm that contains an integer vector. 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;
     }
  • You need to see if there are any statements that require anything, there are a few that diminish a little, but it may not be the intention to do so.

  • Careful, c and c++ are completely different languages. Writing in one language does not mean it will work in the other. By the way, your program does not work with a C compiler

4 answers

4


Its resolution is correct yes, however it can reduce, optimize and improve a little:

  • Can make arrays without being of fixed size, using a variable or a constant.
  • It does not need to make two ordinations because the ascending ordination is inverse to decreasing. With this you can after the first order copy reversed to the second.
  • Using even a similar sorting algorithm, the Insertion Sort, can build 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;
}

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

4

Well, how are you using the tag c++, pq does not solve the problem using the best one has in C++?

For the vector, use the Std::vector. To handle the data, the STL has several algorithms that you can use, for example, the std::sort. Below is an example where you do what you ask:

#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. Use C++11 or higher

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;
}

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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.