Class Notes (1,100,000)
US (480,000)
Rutgers (10,000)
4:18 (400)
Lecture 28

# 01:198:111 Lecture 28: 12/10 - Final Exam ReviewPremium

Department
Computer Science
Course Code
01:198:111
Professor
GUNAWARDENA
Lecture
28

This preview shows half of the first page. to view the full 2 pages of the document.
Workshop problems for review
Suppose an array is given with some invalid entries(indicated by -999). Write a
program to remove the invalid entries and adjust the array while keeping the
order of the entries that are valid, and add 0 to the end of the array that is unfilled
E.g. if you remove one -999 then there should be at least one 0 at the end
of the array
How to solve this?
Different Approaches
Approach #1
Use an extra array of the same length
How?
Copy over any element that is not -999 over
into the new array
Any leftover elements would already be
defaulted to 0 when you create it
Try coding this yourself
Approach #2
Create a loop that edits the original array
How?
Anytime the loop goes through the -999 it
swaps the value with the next valid entry
that is not -999
Then change the -999’s that at the end into
0s
Try coding this yourself
Write recursive function isPalindrome(char[] a) which takes an array of
characters and return true if the characters represent a palindrome. You may use
a helper method to write the function if needed.
Approach
NOTE: Since this is not a string, you cant use string methods like
creating substrings
First, what is a palindrome?
A palindrome is a word or set of characters that can be
read the same forward and backwards
Now that we know what a palindrome is, what is the base case?
If the length of what you are checking is 1, then it must be
a palindrome
Now that we know the base case, how do we actually check if the
characters are the same?
Pretty easily actually, create a left bound and right bound
for the array and compare them
If they are the same, redo the function again but
change the bounds