// ex4.cppDownload Example 4
//
#include <iostream>
using namespace std;
#define maxitems 1000
typedef int vector[maxitems];
void addvectors(vector & temp,vector & v1,vector & v2) {
for (int index =0;index < maxitems;index++)
temp[index]= v1[index] + v2[index];
}
int main(int argc, char* argv[])
{
vector a,b,c;
int index;
for( index=0; index < maxitems; index++){
a[index]=10+index;
b[index]= index*1000;
}
addvectors(c,a,b) ;
cout << "c[0]= " << c[0] << "\nc[999]= " << c[999] << endl;
return 0;
}
The output from this is
c[0]= 10This passes references to three arrays, each holding 1,000 ints and defined by the typedef vector. It loops through all elements, adding each of the elements and storing the result in the third.
c[999]= 1000009
- Learn more about references in the C++ Reference Tutorial.
On the Next Page : A more complex example.

