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

  • Home
  • SEARCH
  • 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 4383184
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:52:29+00:00 2026-05-21T12:52:29+00:00

I have memoized the factorial function in C as follows: int fact(int n) {

  • 0

I have memoized the factorial function in C as follows:

int fact(int n)
{
int temp;
int lookup_table[n];
if(lookup_table[n])
    return lookup_table[n]; 
else{
    if(n == 0 || n == 1)
        return 1;
    else
        temp = n * fact(n-1);
        lookup_table[n] = temp;
        return temp;
    }
}

But then wehn I input n = 5 ,it ouputs
-1! = 134514064
Can someone please explain what is happening?

  • 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-21T12:52:30+00:00Added an answer on May 21, 2026 at 12:52 pm

    int lookup_table[n] should be marked static (actually it can’t, you need a constant there, but it doesn’t have to be too big as factorials grow very quickly), but its not really why you are getting a wrong answer. Instead, lookup_table is initialized to indeterminate junk, instead of 0’s.

    However, theres no reason to initialize it to zero when you make it static; that will be done automatically.

    Oh, and as others have pointed out, you have an out of bounds error because you need to exchange int lookup_table[n] to use a constant rather than n.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a memoized Django model manager method as follows: class GroupManager(models.Manager): def get_for_user(self,
I have a decorated function (simplified version): class Memoize: def __init__(self, function): self.function =
Suppose I have a class in Ruby: class Test def method(arg1, arg2) return arg1+arg2
I sometimes have the need to get the function itself, not the value, of
Let's say I have a referentially transparent function. It is very easy to memoize
Hey. I have a function I want to memoize, however, it has too many
I'm aware of this question , but the asker seems to have been content
I'm trying to memoize a member function of a class, but every time the
I have a simple jQuery function that resizes text areas, and I want it
Say I have a class method like + (double)function:(id)param1 :(id)param2 { // I want

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.