קוד:
// split.c : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
void split(int **,int **,int *,int *,int *,int);
void main()
{
int arr[100],size=0,*ppos=NULL,*pneg=NULL,pos_cnt,neg_cnt,i;
puts("Please, Fill up to 100 natural numbers, finish by zero");
do{ //filling the array (until 100 or input 0)
scanf("%d",&arr[size++]);
}while(size<100 && arr[size-1]);
size-=1;
split(&ppos,&pneg,&pos_cnt,&neg_cnt,arr,size);
puts("The positives numbers are:");
for(i=0;i<pos_cnt;i++)
printf("%d ",ppos[i]);
puts("\n\nThe negatives numbers are:");
for(i=0;i<neg_cnt;i++)
printf("%d ",pneg[i]);
free(ppos);
free(pneg);
fflush(stdin);
getchar();
}
void split(int **p_ppos,int **p_pneg,int *p_pos_cnt,int *p_neg_cnt,int *v,int sz)
{
int i,j,k,temp;
int *p_buff,*n_buff;
//how many pos/neg nums in were filled in arr
for(*p_pos_cnt=*p_neg_cnt=i=0 ; i<sz ; i++)
{
if(v[i]<0)
(*p_neg_cnt)++;
else
(*p_pos_cnt)++;
}
//allocation for ppos/pneg (in main), for positives numb
p_buff=(int *)malloc(*p_pos_cnt*sizeof(int *));
if(p_buff!=NULL)
*p_ppos=p_buff;
n_buff=(int *)malloc(*p_neg_cnt*sizeof(int *));
if(n_buff!=NULL)
*p_pneg=n_buff;
//Sorting arr
for(i=sz-1;i>0;i--)
for(j=0;j<i;j++)
if(v[j]>v[j+1])
{
temp=v[j];
v[j]=v[j+1];
v[j+1]=temp;
}
// Filling ppos and pneg
for(i=j=k=0 ; i<sz ; i++)
{
if(v[i]>=0)
(*p_ppos)[j++]=v[i];
else
(*p_pneg)[k++]=v[i];
}
}