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
|
#include <iostream>
// This function takes an array of array_size elements and adds value to each element.
// prototypes, these are all equivalent:
void add( int array[], unsigned array_size, int value );
void add( int* array, unsigned array_size, int value );
void add( int array[5], unsigned array_size, int value ) ;
void add( int array[10], unsigned array_size, int value ) ;
// Add value to each element of array.
void add(int array[], unsigned array_size, int value )
{
for ( unsigned i=0; i<array_size; ++i )
array[i] += value ;
}
// Print each element of array.
void print( int* array, unsigned array_size )
{
std::cout << "{ " ;
for ( unsigned i=0; i<array_size; ++i )
std::cout << array[i] << ' ' ;
std::cout << "}\n" ;
}
int main()
{
const unsigned aSize =5;
int array[aSize] = { 1, 2, 3, 4, 5 } ;
print(array, aSize) ;
add(array, aSize, 10) ;
print(array, aSize) ;
}
|