arrays addition 111

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()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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
}

http://ideone.com/tmSH56
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
@junaidkhan
Also please what is STD::cout please guide me in this also



STD::cout is almost the same as std::cout but written incorrectly.:)
i know but please give me some idea about this.. that why we use it....
> could you please give me some idea that why avoid using namespace std on global scope

Read this first: http://www.parashift.com/c++-faq/using-namespace-std.html

There is also discussion about using namespace std ; at global scope in this thread: http://v2.cplusplus.com/forum/general/72248/


> also give me idea about global scope

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
using namespace 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()
{
    // ....
}

// ... 


Doing something in a local scope does not:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void foo()
{
       using namespace 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.

Somewhat akin to:
unqualified file name: readme.txt
fully qualified filename: /home/junaid/readme.txt
Last edited on
Topic archived. No new replies allowed.