logo CodeStepByStep logo


Language/Type: C++ recursion
Author: Marty Stepp (on 2016/06/16)

Write a recursive function zigzag that accepts an integer parameter n and that prints out n characters as follows. The middle character of the output should always be an asterisk ("*"). If you are asked to write out an even number of characters, then there will be two asterisks in the middle ("**"). Before the asterisk(s) you should write out less-than characters ("<"). After the asterisk(s) you should write out greater-than characters (">"). For example, the following calls produce the following output:

Call Output
zigzag(1); *
zigzag(2); **
zigzag(3); <*>
zigzag(4); <**>
zigzag(5); <<*>>
zigzag(6); <<**>>
zigzag(7); <<<*>>>
zigzag(8); <<<**>>>

Your function should throw an int exception if passed a value less than 1. Note that the output does not advance to the next line.

Type your C++ solution code here:

This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.