-2

I need to input from keyboard some random values and put them in an array. After that I need to print the average of the prime numbers only. This is my code but it doesn't work:

#include<stdio.h>
#include<conio.h>

int main()
{
int v[50], n, i, nrprim = 0, sum = 0, j;
float medie = 0;
printf("dati numarul de elemente al vectorului:\t");
scanf("%d", &n);
for ( i = 0; i < n; i++)
{
    printf("dati elmentele vectorului:\t");
    scanf("%d", &v[i]);
}

for(i=0; i<n; i++)
    for(j=2; j<v[i]; j++)
    {
        if(v[i]%j!=0)
        {
            sum = sum + v[i];
            nrprim++;
        }
    }

medie =( sum/nrprim);
printf("%f", medie);

_getch();
return 0;

}
Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 3
    "it doesn't work" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Dec 21 '18 at 11:24
  • `sum` and `nrptim` are `int`s. So what do you expect? Integer division truncates. 5 / 2 = 2. Read also [this](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division). There may be other problems as well. – Jabberwocky Dec 21 '18 at 11:25
  • It doesn't show the correct answer. If I input: 2,3,5 the result I get is 5 – Coder Disorder Dec 21 '18 at 11:26
  • 1
    You're not testing for primes correctly. You're treating the number as prime if any other number doesn't divide it evenly. For instance, you'll call `6` a prime because `5` isn't a factor. – Barmar Dec 21 '18 at 11:27
  • 1
    OK - is that because it's splitting the string wrong, or detecting primes wrong, or summing them wrong, or averaging them wrong? You can debug your program either with a debugger or by adding extra print trace to work this out. – Rup Dec 21 '18 at 11:27
  • @Rup I think it is because it is detecting primes wrong – Coder Disorder Dec 21 '18 at 11:28
  • @CoderDisorder divide and conquer. First code the part that detects primes. Once that works, deal with the average – Jabberwocky Dec 21 '18 at 11:29
  • 1
    This is a ball-of-wax problem. Write an IsPrime() function, much harder to get it wrong and much easier to test. – Hans Passant Dec 21 '18 at 11:29
  • @HansPassant I'll bet if he did that he'd put `isPrime = 1` in his inner loop, in the same place where he did the sums. Either he understands the logic or he doesn't. – Barmar Dec 21 '18 at 11:35

2 Answers2

2

Your prime check is wrong. Instead of testing whether a number can't be divided by any other number, you're considering a number a prime when you find the first number that you can't divide it by. So when you for instance test if 9 is prime, you say "yes" because it couldn't be divided by 2, without checking if it can be divided by 3. Try something like this instead:

int flag;
    for (i = 0; i < n; i++)
    {
        flag = 1;
        for (j = 2; j < v[i]; j++)
        {
            if (v[i] % j == 0)
            {
                flag = 0;
                break;
            }
        }
        if (flag) {
            sum = sum + v[i];
            nrprim++;
        }
    }

Also, your program crashes if no prime number is entered, so you need to handle that case as well. I would suggest something such as:

if (nrprim) {
    medie = (sum / nrprim);
    printf("%f\n", medie);
}
else {
    printf("Error: no prime numbers were entered.\n");
}
Blaze
  • 16,736
  • 2
  • 25
  • 44
  • This solved it! Thank you! I will also write an IsPrime function for the sake of training. I am new to C xD – Coder Disorder Dec 21 '18 at 11:32
  • Glad it helped. Yes, dividing up your program into functions like an `IsPrime` function is a very good idea. – Blaze Dec 21 '18 at 11:33
2

You're adding to sum and nrprim every time you find a number that isn't a factor of it. For instance, when i == 8, you'll add to them when j is 3, 5, 6, or 7.

A number is only prime if none of the numbers below it are factors. You have to wait until the end of the j loop to know this.

And if you want fractions in the average, you need to convert one of the values to float before dividing. Otherwise you get integer division.

#include<stdio.h>
#include<conio.h>

int main()
{
    int v[50], n, i, nrprim = 0, sum = 0, j;
    float medie = 0;
    printf("dati numarul de elemente al vectorului:\t");
    scanf("%d", &n);
    for ( i = 0; i < n; i++)
    {
        printf("dati elmentele vectorului:\t");
        scanf("%d", &v[i]);
    }

    for(i=0; i<n; i++)
        int is_prime = 1;
    for(j=2; j<v[i]; j++)
    {
        if(v[i]%j == 0)
        {
            is_prime = 0;
            break;
        }
    }
    if (is_prime) {
        sum = sum + v[i];
        nrprim++;
    }

    medie = float(sum)/nrprim;
    printf("%f", medie);

    _getch();
    return 0;

}
Barmar
  • 741,623
  • 53
  • 500
  • 612