214 Intermediate C Programming
{7
i f (( arr [ ind ] % 2) == 0)8
{9
return ;10
}11
}12
for ( ind = 0; ind < length - 1; ind ++)13
{14
printf (" %d + " , arr [ ind ]) ;15
}16
printf (" %d n" , arr [ length - 1]) ;17
}18
To check whether the numbers form an increasing sequence:
void print Partit ion ( in t * arr , int length )1
{2
int ind ;3
for ( ind = 0; ind < length - 1; ind ++)4
{5
i f ( arr [ ind ] >= arr [ ind + 1]) // not i ncreasing6
{7
return ;8
}9
}10
for ( ind = 0; ind < length - 1; ind ++)11
{12
printf (" %d + " , arr [ ind ]) ;13
}14
printf (" %d n" , arr [ length - 1]) ;15
}16
However, checking before printing is inefficient because many invalid partitions have al-
ready been generated. Instead, a more efficient solution does not generate invalid partitions.
This section explains how to generate valid partitions satisfying one of the following restric-
tions: (i) using odd numbers only, (ii) using increasing numbers, and (iii) using alternating
odd and even numbers.
14.3.1 Using Odd Numbers Only
The function partition generates only partitions that meet the criteria. It is thus much
faster than an approach where all partitions are generated and then “filtered” before being
printed. If only odd numbers are used, val can be an odd number only.
void partition ( i n t * arr , i nt ind , i nt left )1
{2
int val ;3
i f ( left == 0)4
{5
prin t Partit ion ( arr , ind );6
return ;7
}8
for ( val = 1; val <= left ; val += 2) // odd n u mbers only9