Passing an array into a function as a parameter

How do i write this so i pass an array into into a function additional to main as a parameter.

in other words how do i pass the array into a function and return a the value. what the program does is remove duplicates from an array. Thanks!

int main ( )
{ const int SIZE = 10 ;
int integers [ SIZE ] ;
int index ;
cout << "Please enter " << SIZE << " integers, hitting return after each one:" << endl ;
for ( index = 0 ; index < SIZE ; ++index )
cin >> integers [ index ] ;

int count = 0 ;
for ( int i = 0 ; i < SIZE ; ++i )
{ bool seen = false ;
for ( int j = 0 ; ! seen && j < count ; ++j )
if ( integers [ j ] == integers [ i ] )
seen = true ;
if ( ! seen )
integers [ count++ ] = integers [ i ] ;
}

cout << "You entered " << count << " distinct numbers:" << endl ;
for ( index = 0 ; index < count ; ++index )
cout << integers [ index ] << " " ;
cout << endl ;
return 0 ;
}
Hi and welcome!

Please use [code] [/code] tags when posting code.

http://cplusplus.com/reference/algorithm/unique/

This function does what you want. Insert your items into a std::vector<integer> and use as shown in the example.
Hi,

If we define a function which could operate on Array the problem is we cannot copy an array and when we use the name of an array it is automatically converted to a pointer to the first element.

And if we cannot copy an array we can not write a function with array type parameter simply it will convert to the pointer to the first element. So function dealing with array is manipulating pointers to elements in the array indirectly.

1
2
3
4
// all the definitions are same
     void arr(int*) { /* ... */ }
     void arr(int[]) { /* ... */ }
     void arr(int[20]) { /* ... */ }


Despite appearances, a parameter that uses array syntax is treated as if we had written a pointer to the array element type.

Regards
Topic archived. No new replies allowed.