Suppose you have a main() with three local arrays, all the same size and type (say
float). The first two are already initialized to values. Write a function called
addarrays() that accepts the addresses of the three arrays as arguments; adds the contents
of the first two arrays together, element by element; and places the results in the
third array before returning. A fourth argument to this function can carry the size of the
arrays. Use pointer notation throughout; the only place you need brackets is in defining
the arrays.
#include<iostream>
#include<string>
#define SIZE 10
using namespace std;
void addarrays(floatarr1[],float arr2[],float sum[],int n)
{
int i;
for (i=0;i<n;i++)
sum[i]=arr1[i]+arr2[i];
}
int main()
{
float arr1[SIZE]={1,2,3,4,5,6,7,8,9,0},
arr2[SIZE]={3,2,1,5,2,3,4,5,0,1},
arr3[SIZE];
int n=SIZE,i;
addarrays(arr1,arr2,arr3,n);
cout<<"Array 1:";
for(i=0;i<n;i++)
cout<<arr1[i]<<"";
cout<<endl<<"Array2:";
for(i=0;i<n;i++)
cout<<arr2[i]<<"";
cout<<endl<<"Array3:";
for(i=0;i<n;i++)
cout<<arr3[i]<<"";
return 0;
}
Comments
Leave a comment