hi guys how ar you...please can anybody just sort it out,, that whats the source code to write the answer of arrays after addition.. in the below code i used 2 arrays as arguments by using functions,, and then add those arrays and it gives the answer correect,, but i want the answer in this format .. 5+5+5+5=20
6+6+6+6=24
OR the answer of your first array is =20
the answer of your second array is =24
Answer plzzzzzz???? helppppppp??????
#include <iostream>
using namespace std;
int addarray(int arg[], int length){
int r=0;
for (int i=0; i<length; i++)
r= r+arg[i];
cout<< r <<" ";
cout<<"\n";
return r;
}
int main()
{
int firstarray[]={5,5,5,5};
int secondarray[]={6,6,6,6};
addarray(firstarray,4);
addarray(secondarray,4);
return 0;
}
please i need your comments on this as soon as you people can.... I m the beginner
> but i want the answer in this format .. 5+5+5+5=20
Write another function print_array() to print out the elements of the array with a + in between.
And then call print_array() first, and then add_array()
#include <iostream>
// using namespace std; // avoid this, especially at global scope
// use whitespace to make it more readable
void print_array( constint 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( constint 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()
{
constint first_array[] = {5,5,5,5}; // const-correct
constint 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?
constint sz_first = sizeof(first_array) / sizeof( first_array[0] ) ;
constint 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
}
could you please give me some idea that why avoid using namespace std on global scope,, also give me idea about global scope please.. I am the beginner..
Also please what is STD::cout please guide me in this also
To simplify matters, something at global scope affects everything that follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
usingnamespace std ;
// the directive in now in effect for the rest of the program
int g = 78 ; // g is visible and accessible throughout the program
void foo()
{
// ...
}
void bar()
{
// ....
}
// ...
void foo()
{
usingnamespace std ;
// this is in effect only till the end of foo
// ...
}
// the using directive is no longer in effect
void foo()
{
int local = 8 ; // local is visible and accessible till end of foo
// ...
}
// local is not visible at this point
void bar()
{
// ....
}
// ...
In general, restrict programming constructs to the smallest scope possible.
> What is std::cout
It is the fully (well, almost fully) qualified name of the very same cout that you had used. It resides in a namespace called std.