There is a task:

An array С of 8 real elements is specified. Count in it the number of positive, negative numbers and zeros.

As I understand it, here you need to define 2 variables (gender and neg. Numbers) to display them as a result. And to count result in the cycle for? In this case, you need to reset the counter.

Tell me how the cycle will look like? Here I wrote, but something does not display the result ... Tell me what is wrong?

 #include <stdio.h> #include <stdlib.h> #include <math.h> void main() { int otrec = 0, poloj = 0, zero = 0; double A[8], i = 0; for (i = 0; i < 8; i++) { printf("Vvedite element %d\n", i); scanf ("%lf", &A[i]); if (A[i] < 0) otrec++; else if (A[i] > 0) poloj++; else if (A[i] = 0) zero++; } printf("otrec=%d\n poloj=%d\n zero=%d\n", otrec, poloj, zero); getch(); } 
  • To health! - avp
  • one
    You declared array A [] of type int, but you need double, and otrec, poloj should be. int, not float. Another tip - use double, not float - there will be no heaps of misunderstandings. float only to save memory (in arrays and if you are sure that you will not lose significant digits). And all input-output, calculations, transfer of parameters is in double. - avp
  • Thanks for the advice!) I will know everything thinks perfectly, the situation. and negative ... But there is a problem can not count the zeros? how to write? problem fixed in a post) - Timi
  • else if (A [i] = 0)? !!!!!!!!! Is this a comparison or assignment ??? - 3JIoi_Hy6

3 answers 3

 int pol = 0, otr = 0, zero = 0; int arr = [1, 4, -3, 0, ...]; for (int i = 0; i < 8; i++) if (arr[i] > 0) pol++; //Положительное else if(arr[i] < 0) otr++; //Отрицательное else zero++; //Ноль 
     for (cnt_pos = cnt_neg = cnt_z = i = 0; i < 8; i++) { if (C[i] == 0.0) cnt_z++; else if (C[i] > 0.0) ... 

    etc.

      Here is a cycle for the idea described by you:

       int otrec = 0; int poloj = 0; for (int i = 0; i < 8; i++) { if (mas[i] < 0) otrec++; else if (mas[i] > 0) poloj++; } 
      • And who will count the zeros? Although it is obvious that if we know the number of N elements and we calculated the number of negative otrec and positive numbers poloj , then the number of zeroes will be zero = N - otrec - poloj - gecube
      • he asked himself - he answered, the author described this in the question - Kozlov Sergei
      • If the author cannot solve this, I do not know what will happen to him when the matrices begin. There> or <you will not manage. - Goldy
      • It's exactly ... - 3JIoi_Hy6