CSCI 159: exam practice questions

These are just meant as a way to get some practice answering exam-style questions with pen/paper. I'd encourage students to attempt them with minimal references to their notes, but for the final exam you will be permitted one double-sided sheet of notes.

There is one question on each side of the page.

After you've attempted the question, a good way to evaluate your answer while also getting additional practice is to coding and compiling/running the answer you came up with.

Question 1: Basic array use in a function

Write a function that takes two parameters: an array of floats and its size, n, and returns the sum of all the positive values in the array.

Question 2: Arrays and recursion [challenging]

I'll typically have one question per exam where I combine two different topics in a way you haven't seen so far: this is a good example of such a question.

Try solving question 1 again, but this time using recursion instead of a loop to go through the array.

Suggested recursive structure for your function:
if n is less than 0 you can simply return 0,
otherwise
   1. have your sum function call itself
          passing it the same array, but passing n-1 as the size,
      thus the result it returns should be the sum of the first N-1 positive elements
   2. if arr[n-1] is a positive number then
         add it to the result from step 1
   3. return the result