Arrays

Hi so I am having trouble passing through my array into Functions. The functions would be the sum, average, min and max of the array.. this what I have so far...
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
   3 #include <iostream>
  4 #include <cmath>
  5 using namespace std;
  6 
  7 int main()
  8 {
  9     int array[5];
 10 
 11     cout << "Welcome to the World of Arrays!!!!!" << endl;
 12 
 13     // Array    
 14     for (int i = 0; i < 5; i++)
 15     {
 16 
 17         cout << "Enter element  " << i + 1 << "\t";
 18         cin >> array[i];
 19     }
 20 
 21     cout << "You have entered:  " << "\n";
 22     cout << "Position    Value" << "\n";
 23     cout << "-------------------" << endl;
 24 
 25     // Array Displayed in List 
 26     for (int i = 0; i < 5; i++)
 27     {
 28 
 29         cout << i << "  \t\t" << array[i] << endl;
 30 
 31     }
 32 
 33     cin.ignore();
 34     cin.get();
 35 
 36 
 37     system("pause");
 38     return 0;
 39 }
1
2
3
using namespace std;
// ...
int array[5];


Stop asking for trouble. See:
http://www.cplusplus.com/forum/beginner/205160/
Last edited on
There is no ambiguity here:
Using the unqualified name array at block scope would refer to the name array declared at block scope (in main)
It hides the name array (ie. std::array) declared at namespace scope.

Arrays are not copy assignable objects. To pass an array to a function, we have two options:
a. pass the array by reference (size known at compile-time)
b. pass a pointer to the first element of the array, and also the size of the array.

For example:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>

using namespace std;

const int ARRAY_SZ = 5 ;

// a covenient alias for the type: 'array of ARRAY_SZ int'
using array_type = int[ARRAY_SZ] ;

// option one: pass the array by reference
long long sum( const array_type& array )
{
    long long s = 0 ;
    // range-based loop see: http://www.stroustrup.com/C++11FAQ.html#for
    for( int v : array ) s += v ;
    return s ;
}

// option two: pass a pointer to the first element of the array and the size of the array
int min( const int array[], int array_size ) // invariant: num_items > 0
{
    int min_val = array[0] ;

    // iterate through items starting at position 1
    for( int i = 1 ; i < array_size ; ++i )
        if( array[i] < min_val ) min_val = array[i] ;

    return min_val ;
}

int main()
{
    int array[ARRAY_SZ ]; // or equivalently: array_type array ; 

    cout << "Welcome to the World of Arrays!!!!!" << endl;

    for (int i = 0; i < ARRAY_SZ ; ++i )
    {
        cout << "Enter element  " << i + 1 << "\t";
        cin >> array[i];
    }

    cout << "You have entered:  " << "\n";
    cout << "Position    Value" << "\n";
    cout << "-------------------" << endl;

    // Array Displayed in List
    for (int i = 0; i < ARRAY_SZ ; ++i )
    {
        cout << i << "  \t\t" << array[i] << endl;
    }

    // pass the array by reference
    std::cout << "the sum is: " << sum(array) << '\n' ;

    // pass a pointer to the first element and number of elements
    // see Array-to-pointer decay: http://en.cppreference.com/w/cpp/language/array
    std::cout << "the minimum value is: " << min( array, ARRAY_SZ ) << '\n' ;

    cin.get();
}
you taught me more in a few minutes than I have learned all semester.. Thank you!
Topic archived. No new replies allowed.