Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- int Separare(int dim, int* vector, int* dimPozitive, int** vectorPozitive, int* dimNegative, int** vectorNegative)
- {
- *vectorPozitive = (int**)calloc(dim + 1, sizeof(int*));
- if (!*vectorPozitive)
- return 0;
- *vectorNegative = (int**)calloc(dim + 1, sizeof(int*));
- if (!*vectorNegative)
- {
- free(*vectorPozitive);
- return 0;
- }
- for (int i = 0; i < dim; i++)
- if (vector[i] > 0)
- (*vectorPozitive)[(*dimPozitive)++] = vector[i];
- else
- if (vector[i] < 0)
- (*vectorNegative)[(*dimNegative)++] = vector[i];
- return 1;
- }
- void main()
- {
- int* vector, dim;
- int* vectorPozitive, dimPozitive = 0;
- int* vectorNegative, dimNegative = 0;
- scanf_s("%d", &dim);
- vector = (int*)calloc(dim + 1, sizeof(int));
- if (!vector)
- return 0;
- for (int i = 0; i < dim; i++)
- scanf_s("%d", &vector[i]);
- if (Separare(dim, vector, &dimPozitive, &vectorPozitive, &dimNegative, &vectorNegative))
- {
- printf("Pozitive: ");
- for (int i = 0; i < dimPozitive; i++)
- printf("%d ", vectorPozitive[i]);
- puts("");
- printf("Negative: ");
- for (int i = 0; i < dimNegative; i++)
- printf("%d ", vectorNegative[i]);
- free(vectorPozitive);
- free(vectorNegative);
- }
- free(vector);
- }
Add Comment
Please, Sign In to add comment