Can be improved

Asked

Viewed 123 times

-1

Good to have how to improve? I believe so. But improve what? the code make it lighter, improve performance these things. I would like tips and as I am still studying. I will post below 3 vector codes.

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

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

this sorts two vectors of 5 positions and on another vector of decreasing form. I don’t think there’s much to worry about but check out 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;
}
  • 1

    College exercise, huh? I read only the first of the prime numbers, apart from 2 no other pair is first, no multiple of 5, so you can optimize

  • 3

    Evaluation is here: http://codereview.stackexchange.com/

  • OK thanks I will leave two more days to post as they answered me here but I will make a there too @Papacharlie

1 answer

3

Performance of prime numbers:

  1. you do not need to see if it is multiples of 1 or of itself; in the end you check whether the total of divisors is 0 (instead of 2).

  2. do not need to check dividers below the square root of the number (for example to 33 (square root == 5.74...) you just need to check if it’s multiple of 2, 3, and 5).

  3. Once you find a divider you can exit the cycle because the number is composed (for 33, as soon as you see that it is multiple of 3 no longer need to check whether it is multiple of 5)

  4. Treats the 2 as special case and will increase the dividers of 2 in 2 (2, 3, 5, 7, 9, 11, ...). for (j = 3; j < LIMITE, j += 2)

  5. Expanding case 4, you can treat 2 and 3 as special cases and check the shape dividers 6*k ± 1.

  6. As you will handle multiple numbers in the same program, you can store any primes you find in an array, and test only if those numbers are divisors for further testing.

Have fun!

Browser other questions tagged

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