Created
November 19, 2012 20:18
-
-
Save alexesDev/4113617 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <math.h> | |
| #include <stdlib.h> | |
| #include <time.h> | |
| int main(void) | |
| { | |
| int i = 0; | |
| int pairCount = 0; | |
| int size = 0; | |
| int *numbers; | |
| srand((unsigned)time(0)); | |
| size = (rand() % 151) + 50; | |
| numbers = (int *)malloc(size * sizeof(int)); | |
| // заполнение массива случайными числами и вывод начального массива | |
| for(; i < size; ++i) | |
| { | |
| *numbers = (rand() % 101) - 50; | |
| printf("%4d ", *numbers); | |
| ++numbers; | |
| } | |
| // переход к нулевому элементу массива | |
| numbers -= size; | |
| printf("\n\nResults:\n"); | |
| // перебор массива и подсчет количества пар соседних элементов, которые имеют противоположные знаки | |
| for(i = 0; i < size - 1; i += 1) | |
| { | |
| if((*numbers >= 0 && *(numbers + 1) < 0) || (*numbers < 0 && *(numbers + 1) >= 0)) | |
| { | |
| ++pairCount; | |
| } | |
| ++numbers; | |
| } | |
| printf("Array size: %d\n", size); | |
| printf("Count: %d\n", pairCount); | |
| // переход к нулевому элементу массива, с учетом того, что последний цикл не вышел за предел | |
| numbers -= size - 1; | |
| free(numbers); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment