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; }
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;
+ 1
Thank you guys very much
everyone helped!!!!💪🏻🙏🏻
+ 1
it is working correct!
0
it didn’t help
0
i must write negative and positive figures by myself in console ,a then get a how many negative and positive
0
i dont need that console write 0 and 15 by itself
0
it works now but show in console suma istead of numbers
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;
}