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 7458663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:28:38+00:00 2026-05-29T13:28:38+00:00

Here’s a simple program I wrote to find all the non-decreasing-digit numbers of length

  • 0

Here’s a simple program I wrote to find all the non-decreasing-digit numbers of length <=L whose digits sum upto N. The code works fine, but when I try to clock the run time using clock() from ctime it shows a weird behaviour.

#include<iostream> 
#include<vector> 
#include<ctime>
using namespace std;
typedef long long int LL;
    int Sum(LL S){
        int sum=0;
        for(;S;S/=10)
                sum+=S%10;
        return sum;
}
void Generate(LL S, int len, int N, int L, vector<LL> &V){
        if(len<L)
                for(int i=0;i<=9;++i)
                        if(i>=S%10)
                                Generate(S*10+i, len+1, N, L, V);
        int sum = Sum(S);
        if(sum!=N)
                return;
        else if(sum == N && len == L){
                V.push_back(S);
                cout << S << endl; //Line 4
                return;
        }
}
int main(){
        int N,L;
        vector<LL> V;
        LL S;
        cin >> N >> L;
        clock_t start=clock(); //Line 1
        Generate(S, 0, N, L, V);
        //clock_t start=clock();  //Line 2
        clock_t end = clock();
        for(int i=0;i<V.size();++i)
                cout << V[i] << " ";
        cout << endl;
        cout << "Run time: " << (double)(end-start)/CLOCKS_PER_SEC;
        return 0;
}

I record the no. of clock ticks elapsed before calling the “Generate” function at //Line 1 and I do the same afterwards at //Line 2, the difference of which I believe should give me the no. of clock ticks elapsed in generating the required numbers.

But if I do so my function “Generate”‘s processing somehow gets affected! It won’t output the numbers to stdout(from //Line 4) and even if I pass a vector to store up the generated numbers, it won’t store any!

However, if I use clock() at //Line 2 my output on stdout is fine and the referenced vector V gets filled up with the desired result. but clock() on //Line 2 is of no use.

What I fail to understand is How can a call to clock() affect some processing in ‘Generate’ function, unless of course I’ve some obscure Bug! or the clock() is not supposed to be used in this kind of recursive setup?

Please help me Debug this.

  • 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-29T13:28:39+00:00Added an answer on May 29, 2026 at 1:28 pm

    I get variable S is being used without being initialized in your code. Really you need to initialize it to 0 in the very beginning. From then on the behavior is quite unpredictable – thus from time to time you might get the correct answers, but I doubt it. It does not depend on the calls of clock().

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

Sidebar

Related Questions

here is how i call the following function: List<decimal> numbers = new List<decimal>(); numbers.Add(210m);
Here's my code: // Not all headers are relevant to the code snippet. #include
here is my code, SiteMember class @OneToMany(mappedBy = member,cascade=CascadeType.ALL) private List<MemberThread> memberThread = new
Here's the code I'm running. Basically I scrape data, and place them into simple
Here a simple question : What do you think of code which use try
Here's what I'm trying to do... It's quite simple and obviously there must be
Here is another spoj problem that asks how to find the number of distinct
Here's the logic I'm trying to write, but I can't find the proper Ruby
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>

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.