c++ - cannot display the result of each one of my function -
i wrote code computes sum of components of array randomly filled values between 0 , 1. have write 2 functions, 1 iterative, , other 1 recursive. both should same work. 2 functions wrote work fine when call 1 @ time. however, if try call 2 functions in main, can see result of 1 only, cannot see result other one. in addition, recursive function tends called 1 time. have noticed if put getch() comment in recursive_function(). know missing something, cannot figure out. help. here code. using dev-c++.
#include <iostream> #include<conio.h> #include <stdlib.h> using namespace std; //headers of 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 size of array"<<endl; cin>>size; //enter 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 value returned 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 iterative function"<<endl; cout<<"------"<<endl; for(i=0;i<size;i++) sum=sum+array[i]; cout<<"sum of array="<<sum<<endl; getch(); return 0; //exit 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 recursive function"<<endl; cout<<"------"<<endl; cout<<"new sum= "<< sum<<endl; getch(); return 0; }
#include <iostream> #include<conio.h>
<conio.h
not standard header, i.e. not available compilers, , don't need it.
to see result output of program:
run command line, or
in visual studio run via keypress [ctrl f5] (no debugging), or
set breakpoint on closing brace of
main
, , run under debugger (in visual studio e.g. via keypress [f5]).
#include <stdlib.h>
as far can see you’re not using header. however, provide symbolic constants exit_success
, exit_failure
, intended return
statement in main
. e.g., can more clear write exit_success
there write 0
, because many folks misunderstand 0
means in context.
using namespace std;
this ok short program or within namespace.
however, keep in mind short programs end not-so-short programs.
and using namespace std;
can cause name collisions, in particular name std::distance
.
//headers of 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 partly matter of preference, there no advantage in forward-declaring functions before main
, more work, , causes problems when forward declarations don’t quite match definitions – unnecessary redundancy, violations of dry principle (don’t repeat yourself).
instead, place function definitions before main
.
that way easier see refers what, because functions used others come before other functions.
int main() { int size;int array[size]; int sum=0;
this should not compile, because in c++ dynamically allocated array can have size unknown @ compile time.
however, c99 supports “variable length arrays” a.k.a. vlas above syntax, , language extension g++ compiler supports that.
on third , gripping hand, g++ language extension above declares array of indeterminate length, because size
variable has not been initialized , has indeterminate value.
with g++ compiler value 0, can other value.
to turn off g++ vla language extension, , other language extensions, use following g++ options:
-pedantic -std=c++0x -wall
for standard c++, instead of c99 vla should use c++ std::vector<int>
.
in order declaration of std::vector
class template, include standard library header <vector>
.
int index=0; cout<<"enter size of array"<<endl; cin>>size; //enter size ofthe array...
when you're using std::vector
, here, knowing size, place declare vector.
or, if declared earlier, here place resize it.
random_value(array, size);
this better function returned vector of random values.
you use initialize declared vector.
iterative_function (array, size, sum); recursive_function ( size, array, index, sum); getch();
regarding getch()
call, see above comments <conio.h>
.
return 0;
regarding value 0
here, see above comments <stdlib.h>
.
} int random_value(int array[], int size) { cout<<"here value returned rand()"<<endl; for(int i=0;i<size;i++) { array[i]=( rand() % (0-2));
here have undefined behavior, accessing elements of 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 iterative function"<<endl; cout<<"------"<<endl; for(i=0;i<size;i++) sum=sum+array[i];
here again invoking undefined behavior, called “ub”, accessing non-existing array elements.
in addition, if array had been of non-zero size, has not been initialized , contain zeroes or arbitrary values (by holy standard called “indeterminate values”).
cout<<"sum of array="<<sum<<endl; getch();
see above comment <conio.h>
.
return 0; //exit function. program terminated succesfully. }
there no point in letting above function return same value. information-theoretical perspective, return value carries 0 bits of information. instead let function’s result value 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 index
, non-idiomatic , therefore difficult spot experienced readers, use index + 1
in recursive call.
it idea add const
every declaration possible.
that would, example, have forced use index + 1
. :-)
cout<<"from recursive function"<<endl; cout<<"------"<<endl; cout<<"new sum= "<< sum<<endl; getch();
see above comment <conio.h>
.
return 0;
see above comment function returning same value.
}
summing up, undefined behavior happenstance if things appear work.
fix ub's (in particular replace c99 vla std::vector
) first of all, perhaps ask new question if still not work should. ;-)
Comments
Post a Comment