Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Stack overflow
Author Message
username123

Posted: Sun Nov 01, 2015 11:32 am   Post subject: Stack overflow

When I try to run this code, I always get a stack overflow, and I don't know why. It works when I get rid of triangle on line 13 though, but I need this to be recursive.
 Java: public int triangle(int rows) {   if(rows == 0)   {     return 0;   }   else if(rows == 1)   {     return 1;   }   else   {     return triangle(rows * (rows + 1)/2);   } }
Sponsor
Sponsor

Insectoid

Posted: Sun Nov 01, 2015 12:46 pm   Post subject: RE:Stack overflow

A stack overflow happens when your program recurses too many times in a row. In this case, it's probably because rows never reaches 0 or 1, so it's recursing infinitely. Does line 13 ever resolve rows to 0 or 1?
username123

Posted: Sun Nov 01, 2015 1:42 pm   Post subject: Re: RE:Stack overflow

Insectoid @ Sun Nov 01, 2015 12:46 pm wrote:
A stack overflow happens when your program recurses too many times in a row. In this case, it's probably because rows never reaches 0 or 1, so it's recursing infinitely. Does line 13 ever resolve rows to 0 or 1?

That's what I tried to do with lines 3 - 10, but I didn't do it properly. I made it work though.
 Java: public int triangle(int rows) {   if(rows == 0)   {     return 0;   }   return rows + triangle(rows - 1); }
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 3 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: