Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3334728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:56:14+00:00 2026-05-17T23:56:14+00:00

I had a homework assignment that asked for a function that uses direct recursion

  • 0

I had a homework assignment that asked for a function that uses direct recursion to find the index of the left-most, lowest, negative integer in an array. Additional requirements were for the parameters of the function to be the array and the size and that the return value for no valid value was -999.

I came up with this:

int LowIndexMinNeg(int src[], int size)
{
    if (size == 0)
       return -999;
    int index = LowIndexMinNeg(src, size - 1);

    if (index >= 0)
       return (src[size - 1] < src[index]) ? (size - 1) : index;
    else
       return (src[size - 1] < 0) ? (size - 1) : index;
} 

It works, satisfies the requirements, and got me full credit. Can this be implemented with tail recursion?

It seems to me that since you have to take the result from the recursive call to use in a comparison to decide if you pass that one on or update it that it wouldn’t be possible but recursion still ties my brain in knots a it so there might be something obvious that I’m missing.

Note: My homework assignment was already turned in and graded.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:56:15+00:00Added an answer on May 17, 2026 at 11:56 pm

    You need to store the lowest number found so far somewhere. With your function you’re using the stack
    to store that.

    With a tail recursive function you’ll need to store the lowest number found so far elsewhere.
    e.g:

    • As a global variable (ugh..).
    • As a parameter to the function itself.
    • As a member variable

    The requirement you have for your function probably rules out all those, so you’re left with something like the code you have, which cannot be written to be tail-recursive.

    To get an idea of e.g. the 2 last point:

      int LowIndexMinNeg(int src[], int size,int current_lowest = 0,int lowest_index = 0) {
         if(size == 0)
            return current_lowest == 0 ? -999 : lowest_index;
         int val = src[size - 1] ;
         if(val < 0 && val  < current_lowest) {
            current_lowest = val;
            lowest_index = size -1;
         }
          return LowIndexMin(src,size - 1,current_lowest,lowest_index);
       }
    

    And

    struct find_smallest {
      int current_lowest = 0;
      int lowest_index = 0
    
       int LowIndexMinNeg(int src[], int size) {
         if(size == 0)
            return current_lowest == 0 ? -999 : lowest_index;
         int val = src[size - 1] ;
         if(val < 0 && val  < current_lowest) {
            current_lowest = val;
            lowest_index = size - 1;
         }
          return LowIndexMin(src,size - 1);
       }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a homework assignment and I am thoroughly stuck. I
First of all: this is not a homework assignment, it's for a hobby project
Today i was asked this question about sharing data from a thread t1 that
Had a quick look here, couldn't find a duplicate (correct me if im wrong).
Had finally gotten all the bugs out of this and now they said Oh,
Had there been no System.Random class, how would have you generated a random number?
Had to deploy some PHP code onto a shared server with only PHP5.2.8. All
Had this working well in Emacs 23.1.x but it appears to have broke in
Had to deploy some PHP code onto a shared server with only PHP5.2.8. All
I've attempted the question different ways, check my profile for the other two questions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.