Implement a C program that displays the AMOUNT of prime numbers present in a vector, using dynamic vector allocation

Asked

Viewed 150 times

-2

I’m putting together a little program that aims to count how many prime numbers are present in a vector and tell the user the amount. In this case, if the vector has 5 primes, the program must print the number 5.

int primos(int n, int* vet)
{
    int i, prim;
    for(i = 0; i < n; i++){
        for(i = 2; i <= vet[i]/2; i++){
            if(vet[i]%i==0){
            prim++;
          }
       }
    }
    return(prim);
}


int main()
{

    int i, n, prim;

    printf("Digite o Tamanho do vetor: ");
    scanf("%d", &n);

    int* vet = (int*) malloc(n*sizeof(int));
        if(vet == NULL){
            printf("Erro de alocação!");
            exit(1);
        }
    printf("Digite os elementos do vetor: ");
        for(i = 0; i < n; i++){
            scanf("%d", &vet[i]);
        }
        prim = primos(n, vet);
    printf("O vetor tem %d Numero(s) primo(s).", prim);
    free(vet); return 0;
}

I’m having a hard time with this program, you’d be able to help me complete it?

  • What is your difficulty, implementing some logic, or fixing a problem? Is it a build error? Runtime error? Different result than expected? Describe what you need.

  • I wanted tips on how to make it.

1 answer

2


I have identified the following errors in your program:

  • The variable prim is not initialized (prim = 0) within the function primos().
  • The same variable called is being used i for both of us for() nested within the function primos(), causing a for change the variable of the other.
  • Your loop for internal in function primos() increments the variable prim exactly when the number is divisible by i (vet[i] % i == 0). It has to be otherwise, the number will only be prime if it is not divisible by any i.
  • I believe you’ve already done this, but you need to include libraries stdio.h and stdlib.h

Some considerations that do not affect the operation of the program, but can help you:

  • When not needed outside the loop, the temporary variables used for looping iteration for can be declared and initialized directly within the clause for [ex. for(int i, i < 10; ++i){;}].
  • You can call the function primos() directly from within the printf().
  • Compile your programs preferably with the flag -Wall and eventually -pedantic (gcc -Wall -pedantic primos.c -o primos). This will help you identify errors.

Following is the version I made of the program:

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

int primos(int n, int* vet)
{
    int prim = 0, j;

    for(int i = 0; i < n; i++)
    {
        for(j = 2; j <= vet[i]/2; j++)
            if(vet[i] % j == 0)
                break;

        if (j == vet[i]/2 + 1)
            prim++;
    }

    return(prim);
}


int main()
{

    int n;

    printf("Digite o Tamanho do vetor: ");
    scanf("%d", &n);

    int* vet = (int*) malloc(n*sizeof(int));
    if(vet == NULL)
    {
            printf("Erro de alocação!");
            exit(1);
    }

    printf("Digite os elementos do vetor:\n");
    for(int i = 0; i < n; i++)
    {
            printf("Elemento %d: ", i);
            scanf("%d", &vet[i]);
    }

    printf("O vetor tem %d Numero(s) primo(s).", primos(n, vet));

    free(vet); 
    return 0;
}
  • 1

    Thank you so much for your help !!

Browser other questions tagged

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