sum summed up

3

This function should add the couple, but it is not, I did not understand the pq.

#include <stdio.h>

int somapar(int vet[], int n) {
    int soma = 0, i;

    for (i = 0; i < n; i++) {
        if (vet[n] % 2 == 0) 
            soma = soma + vet[n];
    }
    return soma;
}


int main () {

    int v[20] = {2, 1, 8, 3, 4};
    int a;

    a = somapar (v, 5);
    printf ("%d ", a);

    return 0;
}
    
asked by anonymous 29.01.2017 / 12:53

1 answer

3

Code:

#include <stdio.h>

int somapar(int vet[], int n) {
    int soma = 0, i;

    for (i = 0; i < n; i++) {
        if (vet[i] % 2 == 0)
            soma = soma + vet[i];
    }
    return soma;
}


int main() {

    int v[20] = {
        2, 1, 8, 3, 4
    };
    int a;

    a = somapar(v, 5);
    printf("%d ", a);

    return 0;
}

Check the vet[i]

    
29.01.2017 / 13:05