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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:28:25+00:00 2026-05-31T01:28:25+00:00

I wrote a code that computes the sum of components of an array which

  • 0

I wrote a code that computes the sum of components of an array which is randomly filled with values between 0 and 1. I have to write two functions, one is iterative, and the other one is recursive. Both should do the same work. The two functions I wrote work fine when I call only one at the time. However, if i try to call the two functions in the main, I can see the result of one only, but cannot see the result from the other one. In addition, my recursive function tends to get called one extra time. I have noticed that if I put getch() as comment in recursive_function(). I know I am missing something, but I cannot figure that out. Thanks for your help. here is the code. i am using Dev-C++.

#include <iostream>
#include<conio.h>
#include <stdlib.h>

using namespace std;

//headers of the thre functions
int random_value(int array[], int size);
int iterative_function (int array[], int size, int sum);
int recursive_function ( int size, int array[], int index, int sum);



int main()
{     
   int size;int array[size]; int sum=0;
   int index=0;
   cout<<"enter the size of the array"<<endl;
   cin>>size;                //enter the size ofthe array...
   random_value(array, size);
   iterative_function (array, size, sum); 
   recursive_function ( size, array, index, sum);
   getch();
   return 0;
}

int random_value(int array[], int size)
{  cout<<"here is the value returned by rand()"<<endl;
   for(int i=0;i<size;i++)
   { array[i]=( rand() % (0-2));
     cout<<array[i]<<endl;
   }
}

int iterative_function (int array[], int size, int sum)
{
   int i,j, number, value; i=0;
   cout<<"from the iterative function"<<endl;
   cout<<"------"<<endl;
   for(i=0;i<size;i++)
   sum=sum+array[i];
   cout<<"sum of the array="<<sum<<endl;           
   getch();
   return 0;      //exit the function. Program terminated succesfully.
}

int recursive_function ( int size, int array[], int index, int sum)
{
  if(size>index)
  {  
    sum=sum+array[index];
    index++;
    recursive_function( size, array, index, sum); 
  }
  cout<<"from the recursive function"<<endl;
  cout<<"------"<<endl;
  cout<<"new sum= "<< sum<<endl;

  getch();
  return 0;

}
  • 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-31T01:28:27+00:00Added an answer on May 31, 2026 at 1:28 am
    #include <iostream>
    #include<conio.h>
    

    <conio.h is not a standard header, i.e. it is not available with all compilers, and you don’t need it.

    To see the result output of your program:

    • run it from the command line, or

    • in Visual Studio run it via keypress [Ctrl F5] (no debugging), or

    • set a breakpoint on the closing brace of main, and run it under a debugger (in Visual Studio e.g. via keypress [F5]).

    #include <stdlib.h>
    

    As far as I can see you’re not using anything from this header. However, it does provide the symbolic constants EXIT_SUCCESS and EXIT_FAILURE, which are intended for return statement in main. E.g., it can be more clear to write EXIT_SUCCESS there than to write 0, because many folks misunderstand what 0 means in this context.

    using namespace std;
    

    This is OK for a short program or within a namespace.

    However, keep in mind that short programs very often end up as not-so-short programs.

    And then a using namespace std; can easily cause name collisions, in particular with the name std::distance.

    //headers of the thre functions
    int random_value(int array[], int size);
    int iterative_function (int array[], int size, int sum);
    int recursive_function ( int size, int array[], int index, int sum);
    

    Although it is partly a matter of preference, there is no advantage in forward-declaring the functions before main, it is more work, and it sometimes causes problems when the forward declarations don’t quite match the definitions – as with any unnecessary redundancy, violations of the DRY principle (Don’t Repeat Yourself).

    Instead, just place the function definitions before main.

    That way it is also much easier to see what refers to what, because functions that are used by others then necessarily come before those other functions.

    int main()
    {     
       int size;int array[size]; int sum=0;
    

    This should not compile, because in C++ only a dynamically allocated array can have a size that is unknown at compile time.

    However, C99 supports “variable length arrays” a.k.a. VLAs with the above syntax, and as a language extension the g++ compiler supports that.

    On the third and gripping hand, even with the g++ language extension the above declares an array of indeterminate length, because the size variable has not been initialized and has an indeterminate value.

    With the g++ compiler that value is most likely 0, but it can easily be any other value.

    To turn off the g++ VLA language extension, and some other language extensions, use the following g++ options:

    -pedantic -std=c++0x -Wall
    

    For standard C++, instead of a C99 VLA you should use a C++ std::vector<int>.

    In order to get a declaration of the std::vector class template, include the standard library header <vector>.

       int index=0;
       cout<<"enter the size of the array"<<endl;
       cin>>size;                //enter the size ofthe array...
    

    When you’re using a std::vector, then here, knowing its size, would be the place to declare that vector.

    Or, if declared earlier, here would be the place to resize it.

       random_value(array, size);
    

    This would better be a function that returned a vector of random values.

    You would then use that to initialize the declared vector.

       iterative_function (array, size, sum); 
       recursive_function ( size, array, index, sum);
       getch();
    

    Regarding the getch() call, see the above comments about <conio.h>.

       return 0;
    

    Regarding the value 0 here, see the above comments about <stdlib.h>.

    }
    
    int random_value(int array[], int size)
    {  cout<<"here is the value returned by rand()"<<endl;
       for(int i=0;i<size;i++)
       { array[i]=( rand() % (0-2));
    

    Here you have Undefined Behavior, accessing elements of a possibly zero-size array.

         cout<<array[i]<<endl;
       }
    }
    
    int iterative_function (int array[], int size, int sum)
    {
       int i,j, number, value; i=0;
       cout<<"from the iterative function"<<endl;
       cout<<"------"<<endl;
       for(i=0;i<size;i++)
       sum=sum+array[i];
    

    Here you are again invoking Undefined Behavior, often called just “UB”, by accessing non-existing array elements.

    In addition, even if the array had been of non-zero size, it has not been initialized and so would contain just zeroes or arbitrary values (by the Holy Standard called “indeterminate values”).

       cout<<"sum of the array="<<sum<<endl;           
       getch();
    

    See the above comment about <conio.h>.

       return 0;      //exit the function. Program terminated succesfully.
    }
    

    There is no point in letting the above function always return the same value. From an information-theoretical perspective, that return value carries zero bits of information. Instead just let the function’s result value be void.

    int recursive_function ( int size, int array[], int index, int sum)
    {
      if(size>index)
      {  
        sum=sum+array[index];
        index++;
        recursive_function( size, array, index, sum); 
      }
    

    Instead of incrementing the index, which is non-idiomatic and therefore difficult to spot for experienced readers, just use index + 1 in the recursive call.

    It is a good idea to add const to just about every declaration where it is possible.

    That would, for example, have forced you to use index + 1. 🙂

      cout<<"from the recursive function"<<endl;
      cout<<"------"<<endl;
      cout<<"new sum= "<< sum<<endl;
    
      getch();
    

    See the above comment about <conio.h>.

      return 0;
    

    See the above comment about function always returning the same value.

    }
    

    Summing up, with all the Undefined Behavior it is just happenstance if things appear to work.

    Fix the UB’s (in particular replace C99 VLA with std::vector) first of all, then perhaps ask new question if it still does not work as it should. 😉

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

Sidebar

Related Questions

I write simple C++ code that compute array reduction sum, but with OpenMP reduction
I recently wrote some javascript code that filled a drop down list based on
I am running some code that I have written in C which calls the
So I wrote buggy code that occasionally crash ... and creates a stackdump file.
I was thinking about a code that I wrote a few years ago in
I am testing some weird-looking CSS code that I wrote (I'm using a mix
I've got the following bit of code (that another developer wrote) that I need
I've got some code that lies in a browser, and wrote C++ plugins for
I wrote some code with a lot of recursion, that takes quite a bit
I wrote some naive code(in the sense that it's synchronous calls) for a tableview

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.