Skip to content
Solved Assignments

CSC 245 Recursion Lab

A lab assignment on recursion principles and algorithm design.

Preview 2 of 4 pages

See the first 2 pages exactly as they appear in the document.

  1. Page 1
    Read page 1 as text

    CSC 245 Recursion Lab For each of the following, write recursive functions to complete the stated tasks. Test your functions by letting your main function call the recursive function and report the results. 1. Given an array of n integer values, compute the sum of the values. Answer: /*** * Q1 * * @param n * @return */ public static int sum( int n) { if (n == 0) { return 0; } else return (n + sum(n - 1)); } 2. Output the following patterns: a. ********** ********** (an n x m rectangle) ********** ********** Answer: /*** * 2. a function * * @param n * @param m * @return */ public static String rectangle(int n, int m) { if (n == 0) { return " "; } for (int i = 0; i < m; i++) { System.out.print("*"); } System.out.println(); return rectangle(n - 1, m);

  2. Page 2

2 more pages in the full document

Unlock it once and it stays in your library, ready to chat with or turn into flashcards.

Unlock the full document
See all

Study this document with CramX

Once it is in your library, every tool can work from it.