This program uses a time seed to randomly generate 10 integers [-5 ~ 5]. The function is randData (). There is also a function ZeroSubarray () that calculates the subsequence as 0 ().
RandData () is as follows:
[Cpp]
Int arr [10];
Void randData (int a [], int start, int end)
{
Srand (time (NULL ));
For (int I = start; I <= end; ++ I)
A [I] = rand () % 10-5;
Printf ("generate new sequence :");
Print (a, start, end );
Printf ("\ n ");
}
ZeroSubarray () is as follows:
[Cpp]
Void ZeroSubarray (int a [], int size ){
If (size <= 0)
Perror ("error array size ");
Int sum = 0;
Int max =-(1 <31 );
Printf ("************* \ n ");
For (int cur = 0; cur <size; cur ++)
{
Sum = 0;
For (int j = cur; j <size; j ++)
{
Sum + = a [j];
If (sum = 0)
{
While (cur <= j)
{
Printf ("% d", a [cur ++]);
}
Printf ("\ n ************* \ n ");
}
}
}
}
Main function:
[Cpp]
Void main ()
{
RandData (arr, 0, 9 );
ZeroSubarray (arr, 10 );
Printf ("\ n ");
}
The running result is as follows: