#include <iostream>
// using namespace std; // avoid this, especially at global scope
// use whitespace to make it more readable
void print_array( const int arg[], int length, char seperator )
{
int i = 0 ;
for( ; i < length-1 ; ++i )
std::cout << arg[i] << seperator ;
if( length > 0 ) std::cout << arg[i] ; // print the last element
}
// seperate words in identifiers with an _ (or change of case)
int add_array( const int arg[], int length ) // make it const-correct
{
int sum = 0 ; // give this a more intuitive name
for( int i=0; i<length; ++i ) // get into the habit of using prefix increment/decrement
sum = sum + arg[i] ;
// the function should just return the sum and leave the decision about what the
// sum is to be used for to the caller.
return sum ;
}
int main()
{
const int first_array[] = {5,5,5,5}; // const-correct
const int second_array[] = {6,16,26,36,46,56}; // const-correct
// why should we hard code magic numbers
// when the compiler can calculate it for us?
const int sz_first = sizeof(first_array) / sizeof( first_array[0] ) ;
const int sz_second = sizeof(second_array) / sizeof( second_array[0] ) ;
print_array( first_array, sz_first, '+' ) ;
std::cout << " == " << add_array( first_array, sz_first ) << '\n' ;
print_array( second_array, sz_second, '+' ) ;
std::cout << " == " << add_array( second_array, sz_second ) << '\n' ;
// there is an implicit return 0 ; here
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKLy8gdXNpbmcgbmFtZXNwYWNlIHN0ZDsgLy8gYXZvaWQgdGhpcywgZXNwZWNpYWxseSBhdCBnbG9iYWwgc2NvcGUKCi8vIHVzZSB3aGl0ZXNwYWNlIHRvIG1ha2UgaXQgbW9yZSByZWFkYWJsZQp2b2lkIHByaW50X2FycmF5KCBjb25zdCBpbnQgYXJnW10sIGludCBsZW5ndGgsIGNoYXIgc2VwZXJhdG9yICkKewogICAgaW50IGkgPSAwIDsKICAgIGZvciggIDsgaSA8IGxlbmd0aC0xIDsgKytpICkKICAgICAgICBzdGQ6OmNvdXQgPDwgYXJnW2ldIDw8IHNlcGVyYXRvciA7CiAgICBpZiggbGVuZ3RoID4gMCApIHN0ZDo6Y291dCA8PCBhcmdbaV0gOyAvLyBwcmludCB0aGUgbGFzdCBlbGVtZW50Cn0KCi8vIHN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