#include /* hasNeg: * Given a vector v and integer n, * return whether or not one of its first n elements is negative. * (We assume v has at least n elements.) */ bool hasNeg( int v[], int n ) { if (n == 0) return false; else { if (v[n-1] < 0) return true; else return hasNeg( v, n-1 ); } } /* main: * create a vector, fill it with values, then call hasNeg. */ int main() { const int vSize = 10; int vec[ vSize ]; int i = 0; while ( i < vSize ) { vec[ i ] = (i - 4) * (i - 7); i = i + 1; } if (hasNeg( vec, vSize )) printf( "Some element of vec is negative.\n" ); else printf( "No element of vec is negative.\n" ); return 0; } /* ----------- Although hasNeg's loop is written and indented logically, it looks awful, all those nested-ifs. Here is the more conventional way of indenting nested-ifs: if (n == 0) return false; else if (v[n-1] < 0) return true; else return hasNeg( v, n-1 ); */ /* -------------- Note that you can get yourself into trouble easily whenever the first branch of an if statment is another if: if (n==0) if (m == 0) return false; // how to indent these next two lines?: // else // return false; (It turns out, the rule is to match the else with the *nearest* unmatched if.) */