0

My solution to "That's odd" doesn't work, and I dont'understand why. The #4 case remains always wrong.[SOLVED]

#include <iostream> using namespace std; int main() { int N, *p, i, out=0; cin >> N;//how many numbers? if(N<=0) return (-1); p = (int *)malloc(sizeof(int)); for(i=0;i<N;i++) { cin>>p[i]; if(p[i]%2==0) out+=p[i]; } cout << out; return 0; }

5th Apr 2020, 10:27 PM
Federico Di Stefano
Federico Di Stefano - avatar
2 Respuestas
+ 1
Resolved. I forgot to multiply the memory in the pointer to N. I had to do: p = (int *)malloc(N*sizeof(int));
5th Apr 2020, 11:19 PM
Federico Di Stefano
Federico Di Stefano - avatar
+ 1
p[i] % 2 == 0 -> here you check if a number is even (divisible by 2) But you need to add odd numbers. p[i] % 2 != 0 or p[i] % 2 == 1
5th Apr 2020, 11:14 PM
Denise Roßberg
Denise Roßberg - avatar