New User Special Price Expires in

Let's log you in.

Sign in with Facebook


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


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

Week 7 Notes

by: Taylor Kahl

Week 7 Notes CSC 2310

Taylor Kahl
GPA 4.21

Preview These Notes for FREE

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

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Notes from lectures on February 22 and 24, on chapter 12: Recursion
Princliples of Computer Programming
Kebina Manandhar
Class Notes
25 ?




Popular in Princliples of Computer Programming

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.

Similar to CSC 2310 at GSU

Popular in ComputerScienence


Reviews for Week 7 Notes


Report this Material


What is Karma?


Karma is the currency of StudySoup.

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;


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

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

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."


"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


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


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:

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

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.