×

### Let's log you in.

or

Don't have a StudySoup account? Create one here!

×

or

by: Taylor Kahl

25

1

4

# Week 7 Notes CSC 2310

Taylor Kahl
GSU
GPA 4.21

Get a free preview of these Notes, just enter your email below.

×
Unlock Preview

Notes from lectures on February 22 and 24, on chapter 12: Recursion
COURSE
Princliples of Computer Programming
PROF.
Kebina Manandhar
TYPE
Class Notes
PAGES
4
WORDS
KARMA
25 ?

## Popular in ComputerScienence

This 4 page Class Notes was uploaded by Taylor Kahl on Saturday February 27, 2016. The Class Notes belongs to CSC 2310 at Georgia State University taught by Kebina Manandhar in Winter 2016. Since its upload, it has received 25 views. For similar materials see Princliples of Computer Programming in ComputerScienence at Georgia State University.

×

## Reviews for Week 7 Notes

×

×

### What is Karma?

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 02/27/16
Chapter 12: Recursion  Recursion – defining an operation in terms of itself o solve a large problem by solving smaller instances of the same problem  Recursion in programming – writing methods that call themselves o a good substitute for loops  A non-programming example: find out how many people are sitting directly behind you, if you can’t see all the way to the back of the room o rather than 1 person solving the entire problem (like with a loop), each person solves a small part (recursion) o ask the person behind you if someone is sitting behind him – for everyone who answers “yes” add 1; stop when the final person answers “no”  Recursion algorithms have 2 cases: 1. Base case – simple occurrence, directly answerable. Usually when a number n = 0 or 1 2. Recursive case – not directly answerable. Can apply to any number n  Some problems will have multiple base or recursive cases. You need at least 1 of each  Example: a method that prints a line of n stars, without using loops: o Base case – if n = 0, print nothing and end output. o Recursive case – if n > 0, print a star and call the method again for n-1, over and over until n = 0 public static void printStars(int n) { if (n == 0) { //base case, n=0, ends output System.out.print(); } else { //recursive case, prints 1 more star System.out.print(“*”); printStars(n-1); //n passed to the method is decreased by 1 } } This method calls itself and prints a star for each integer from n to 1, then when n = 0 the method ends. ***I noticed that this method will result in an infinite loop if n is a negative number. It doesn’t make sense to enter a negative number, but anyway, this can be fixed by changing line 2 to: if (n <= 0) {  Here’s a trace of what happens when printStars(3); is called: printStars(3); //n>0, execute else statements System.out.print(“*”); printStars(2); //n>0, execute else statements System.out.print(“*”); printStars(1); //n>0, execute else statements System.out.print(“*”); printStars(0); //n==0, execute if statement System.out.println();  Infinite recursion – writing a method that never finishes executing (like an infinite loop – not good!) o Example: public static void writeStars(int n) { writeStars(n); }  What is the output of calling the following code? mystery(648); public static int mystery(int n) { if (n < 10) { return n; } else { int a = n / 10; int b = n % 10; return mystery(a + b); } } mystery(648) // 648 > 0, execute else statements a = 64; b = 8 return mystery (72); // 72 > 0, execute else statements a = 7; b = 2 return mystery(9); // 9 < 0, execute if statement return 9.  What is the output of the following code? mystery(348); public static int mystery(int n) { if (n < 10) { return (10 * n) + n; } else { int a = mystery(n / 10); int b = mystery(n % 10); return (100 * a) + b; } mystery(348); // 348 > 10, execute else statements a = mystery (348 / 10); // 34 > 10, execute else statements a = mystery(34 / 10); // 3 < 10, execute if statement return 10 * 3 +3; // a = 33 b = mystery(34 % 10); // 4 < 10, execute if statement return 10 * 4 +4; // b = 44 return (100 * 33) +44; // a = 3344 b = mystery(348 % 10); // 8 < 10, execute if statement return 10 * 8 + 8; // b = 88 return (100 * 3344) + 88;  A recursive method pow that accepts an integer base and integer exponent and returns the base raised to that exponent o Base step – any number raised to the 0 power = 1 o Multiply the base times itself exponent number of times public static int pow(int base, int exponent) { if (exponent == 0) { return 1; } else { return base * pow(base, exponent – 1); } } A trace of pow(2, 3); pow(2, 3); // 3 > 0, execute else statement return 2 * pow(2, 2); // 2 > 0, execute else statement return 2 * pow (2, 1); // 1 > 0, execute else statement return 2* pow(2, 0) // 0 == 0, execute if statement return 1; return 2; return 4; return 8;

×

×

### BOOM! Enjoy Your Free Notes!

×

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

Bentley McCaw University of Florida

#### "I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Allison Fischer University of Alabama

#### "I signed up to be an Elite Notetaker with 2 of my sorority sisters this semester. We just posted our notes weekly and were each making over \$600 per month. I LOVE StudySoup!"

Jim McGreen Ohio University

Forbes

#### "Their 'Elite Notetakers' are making over \$1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!
×

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com