Can improve

1

Good to have how to improve? I believe so. But improve what? the code leaves you lighter, improve performance these things. I would like tips and because I am still studying. I'll post 3 vector codes below.

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

//cria um vetor de 10 posições e imprime somente números primos.
//minha impressão não esta nada legal e quando abro outro for os dados do vetor mudam.

int main()
{
    int num[9], i, j;
    int cont=0;

    for(i=0; i<9; i++)
    {
        printf("\ndigite 9 numeros:\n");
        scanf("%d", &num[i]);

        for(j=1; j<=num[i]; j++)
        {
            if(num[i]%j==0)
            {
                cont++;
            }
        }
        if(cont==2)
            printf("\nvetor[%d]:%d\n", i+1, num[i]);
        cont=0;

    }


    return 0;
}

===================================================== ========================

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

int main()
{
    int vet1[5], vet2[5], rvet[10];
    int i, j, k, n1=0, n2=0, aux=0;


    for(i=0; i<=4; i++)//Lê o vet1
    {

        printf("Digite para posicao %d do vet1:\n", n1++);
        scanf("%i", &vet1[i]);
    }

    fflush(stdin);
    system("cls");

    printf("Obrigado, agora:\n");

    for(j=0; j<=4; j++)//Lê vet2
    {
        printf("Digite para posicao %d do vet1:\n", n2++);
        scanf("%d", &vet2[j]);

    }

    fflush(stdin);
    system("cls");

    i=j=0;
    for(k=0;k<=9;k++)//Intercala os vetores
    {
        rvet[k]=k%2==0 ? vet1[i++] : vet2[j++];

        printf("vet[%d]:%d\n", k, rvet[k]);
    }

    return 0;
}

===================================================== ========================

it sorts two 5-position vectors and another decreasing vector. I believe that there is not much to worry about but take a look who knows

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

int main()
{
    int vet1[5], vet2[5], rvet[10];
    int i, j, k, l, m, n1=0, n2=0, aux=0;


    for(i=0; i<=4; i++)//Lê o vet1
    {

        printf("Digite para posicao %d do vet1:\n", n1++);
        scanf("%d", &vet1[i]);
    }

    fflush(stdin);//limpar sujeira  
    system("cls");//limpar a tela

    printf("Obrigado, agora:\n");

    for(j=0; j<=4; j++)//Lê vet2
    {
        printf("Digite para posicao %d do vet2:\n", n2++);
        scanf("%d", &vet2[j]);

    }

    fflush(stdin);//limpar sujeira
    system("cls");//limpar a tela


    for(k=0; k<=4; k++)//preenche as 5 primeiras posições com vet1;
    {
        rvet[k]=vet1[k];
    }

    for(k=5; k<=9; k++)//preenche as 5 ultimas posições com vet2
    {
        rvet[k]=vet2[k-5];
    }   

    //gera a ordenação dos elementos do rvet
    for(l=0; l<=9; l++)//pega elemento por elemento em sua posição original.
    {
        for(m=l+1; m<=9; m++)//pega o elemento da posição seguinte
        {
            if(rvet[m]>rvet[l])//se o proximo numero for maior...
            {
                aux=rvet[m];//guarda ele em aux
                rvet[m]=rvet[l];//rvet[1] troca por numero maior
                rvet[l]=aux;//e numero menor fica guardado na fila(aux)
            }
            //numero do aux ou o ultimo da fila eh guardado em rvet[9]
        }
    }

    for(k=0; k<=9; k++)//imprime
    {
        printf("%d\n", rvet[k]);
    }

    return 0;
}
    
asked by anonymous 08.08.2014 / 03:38

1 answer

3

Prime number performance:

  • You do not need to see if it's multiple of 1 or of itself; in the end, you check whether the total of divisors is 0 (instead of 2 ).

  • You do not need to check divisors smaller than the square root of the number (for example for 33 (square root == 5.74... ) you only need to check if it is a multiple of 2 , 3 , and 5 ).

  • As soon as you find a divisor you can exit the loop because the number is composed (for 33 , once you see that it is a multiple of 3 , you no longer need to check if it is a multiple of 5 / p>

  • Treats 2 as a special case and increases the split of 2 to 2 ( 2 , 3 , 5 , 7 , 9 , 11 , ...). for (j = 3; j < LIMITE, j += 2)

  • By expanding Case 4, you can treat 2 and 3 as special cases and check the splitters of the 6*k ± 1 form.

  • Have fun!

        
  • 08.08.2014 / 10:41