Please help solve the problem. An array of integers is given. Determine how many times the sign changes in the longest sequence of numbers that does not contain zeros. C language. Thank you very much.

int main() { const int N = 10; int mas[N], i; setlocale(LC_ALL, "Rus"); for (i = 0; i < N; i++) { printf_s("\tmas[%d] = ", i); scanf_s("%d", &mas[i]); } printf_s("\t"); for (i = 0; i < N; i++) printf_s("%d ", mas[i]); printf_s("\n"); int count = 0; for (i = 0; i < N - 1; i++) if (mas[i] > 0 && mas[i + 1] < 0 || mas[i] < 0 && mas[i + 1] > 0) count++; printf_s("\t%d", count); _getch(); return 0; } 
  • Show how you solved this problem. - freim
  • # include <stdio.h> # include <conio.h> # include <locale.h> int main () {const int N = 10; int mas [N], i; setlocale (LC_ALL, "Rus"); for (i = 0; i <N; i ++) {printf_s ("\ tmas [% d] =", i); scanf_s ("% d", & mas [i]); } printf_s ("\ t"); for (i = 0; i <N; i ++) printf_s ("% d", mas [i]); printf_s ("\ n"); int count = 0; for (i = 0; i <N - 1; i ++) if (mas [i]> 0 && mas [i + 1] <0 || mas [i] <0 && mas [i + 1]> 0) count ++ ; printf_s ("\ t% d", count); _getch (); return 0; } - Elena
  • This is impossible to understand. Just edit your answer and paste the program as code. - freim
  • Tell me, please, how to do it. - Elena
  • Under your first message there is a edit button. You can click on it, and in the opened editor insert your code, select it and press Ctrl-K on the keyboard or {} on the toolbar. - mymedia

1 answer 1

 ... int maxCount = 0; int maxLength = 0; int count = 0; int length = 0; for (i = 0; i < N - 1; i++) { if (mas[i] == 0) { if (length > maxLength) { maxLength = length; maxCount = count; } length = 0; count = 0; } else { length++; } if ((mas[i] > 0 && mas[i + 1] < 0) || (mas[i] < 0 && mas[i + 1] > 0)) count++; } if (length > maxLength) { maxLength = length; maxCount = count; } // print maxLength and maxCount 

https://onlinegdb.com/rk0ofYIjQ

  • Maybe there is some other solution? Because my answer for any combination of numbers turns out to be zero. - Elena
  • @ Elena Maybe there is. I think you inaccurately copied the code to yourself: onlinegdb.com/rk0ofYIjQ - Igor
  • Thank you very much, you helped me a lot. Everything is working. - Elena