Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Find the sum of all odd-numbered elements in a one-dimensional integer array
Find the sum of all odd-numbered elements in a one-dimensional integer array

int sum(int *p, int n)

{

int i, s=0;

for(i = 0; i < n; i++)

{

if(i % 2 == 0)

s += *p;

< p>p++;

}

return s;

} Because the index starts from 0, odd subscripts appear as even numbers.

If I understand correctly, you want the index to be an odd number. Please change the sentence if(i % 2 == 0) to if(i % 2 != 0)