0

Hi guys,somebody can help

someone can explain why it doesn’t work,the program is i must write 5 figures (negative and positive) and it must find how many positive and negative,need it do through the FOR and arrays,whats wrong? #include <stdio.h> int main() { int a[5] = {5,4,3,2,1}, sumPos = 0, sumNeg = 0; //printf("Vvedit pershe chuslo:”); //scanf("%d", &a[i]); for (int i = 0; i < 5; i++) { if (a[i] < 0) sumNeg = +a[i]; else sumPos = +a[i]; } printf("Suma negative numbers: %d\n", sumNeg); printf("Suma positive numbers: %d", sumPos); return 0; }

20th Nov 2020, 4:46 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
8 odpowiedzi
+ 1
If you are trying to count how many positive and negative numbers there are, instead of finding the sum, then change it to if (a[i] < 0) sumNeg += 1; else sumPos += 1;
20th Nov 2020, 5:35 PM
Brian
Brian - avatar
+ 1
Thank you guys very much everyone helped!!!!💪🏻🙏🏻
20th Nov 2020, 5:52 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
+ 1
it is working correct!
20th Nov 2020, 5:52 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
it didn’t help
20th Nov 2020, 5:05 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
i must write negative and positive figures by myself in console ,a then get a how many negative and positive
20th Nov 2020, 5:11 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
i dont need that console write 0 and 15 by itself
20th Nov 2020, 5:12 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
it works now but show in console suma istead of numbers
20th Nov 2020, 5:14 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
If your goal is to count how many positive numbers or negative numbers are in the array you just need to increment your sumNeg or sumPos given your if conditions. The code will be : #include <stdio.h> int main() { int a[5] = {5,-1,3,2,1}, sumPos = 0, sumNeg = 0; for (int i = 0; i < 5; i++) { if (a[i] < 0) sumNeg += 1; else sumPos +=1; } printf("Suma negative numbers: %d\n", sumNeg); printf("Suma positive numbers: %d", sumPos); return 0; }
20th Nov 2020, 5:45 PM
Funakoshi S
Funakoshi S - avatar