ECE-C 301 Assignment 2: Recursion

Due: Tuesday Jan 22, 2008

I. Program 1 (35 pts)

Write a function writeZeroFill(int num, int zeros). What this function does is that it is similar to C++'s setfill combined with setw method. You tell it how man spaces the number, num, should take up with the parameter zeros. (NOTE: do not be afraid to design extra functions to facilitate the task)


System.out.println(writeZeroFill(-1234, 5);
System.out.println(writeZeroFill(-1234, 6);

Will print:



System.out.println(writeZeroFill(-1234, 2);

Will print:


IMPORTANT: Define this function recursively!!! Do not use a loop, you can only use recursion.

II. Program 2 (35 pts)

Write a function reverseString(String words) that returns a string. This function effectively reverses any string passed in and returns it.


System.out.println(reverseString("Hi Jasper");

Will print:

repsaJ iH



Will print:


This should be a special treat to those of you who are dyslexic.

IMPORTANT: Define this function recursively!!! Do not use a loop, you can only use recursion.

III. Program 3 (30 pts)

Choose one of the two provided:

1. Pascal Triangle (+30 bonus pts, which only counts before 1/22)

Define a recursive function that recursively builds the Pascal's triangle up to the N-th level. The function looks like: pascalTriangle(int n). You can find out more about the Pascal's Triangle formulation on Wikipedia.



Will print:


1 1

1 2 1

1 3 3 1

1 4 6 4 1

NOTE: N=0 will give you no output.
NOTE: You will need to use array, Vector, or some container to do this (this will be next week's lecture, which won't help you with this assignment).

2. Image analysis (no bonus awarded)

Write a function in psuedo-code that goes through an image starting at position (0,0) and returns the count of all pixels that are white. Make sure you use recursion in this process.

Example of psuedo-code:

// function will draw a square

void drawSquare(int size) {

for i = (0 to size) {

draw point at (0,i)
draw point at (size,i)
draw point at (i, 0)
draw point at (i, size)



When writing psuedo-code, make sure that you know that I know what you are talking about... If you got code that you think has questionable meaning, put in massive amounts of comments to tell me what it's doing.