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;
}
<conio.his 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]).As far as I can see you’re not using anything from this header. However, it does provide the symbolic constants
EXIT_SUCCESSandEXIT_FAILURE, which are intended forreturnstatement inmain. E.g., it can be more clear to writeEXIT_SUCCESSthere than to write0, because many folks misunderstand what0means in this context.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 namestd::distance.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.
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
sizevariable 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:
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::vectorclass template, include the standard library header<vector>.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.
This would better be a function that returned a vector of random values.
You would then use that to initialize the declared vector.
Regarding the
getch()call, see the above comments about<conio.h>.Regarding the value
0here, see the above comments about<stdlib.h>.Here you have Undefined Behavior, accessing elements of a possibly zero-size array.
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”).
See the above comment about
<conio.h>.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.Instead of incrementing the
index, which is non-idiomatic and therefore difficult to spot for experienced readers, just useindex + 1in the recursive call.It is a good idea to add
constto just about every declaration where it is possible.That would, for example, have forced you to use
index + 1. 🙂See the above comment about
<conio.h>.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. 😉