Question 1: 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 three arrays as arguments; adds the contents of the first two together, element by element; and places the result in the third array before returning. A fourth argument to this function can carry the size of the arrays. Use the pointer notation throughout; the only place you need brackets is in defining the arrays.
#include <iostream>
using namespace std;
void addarrays(int*
f, int* s, int* th, int arrsize){
for(int i = 0; i < arrsize;
++i)
th[i] = f[i] + s[i];
}
int main(int argc, char*
argv[]){
const int SIZE = 5;
int first[5] = {19,72,3,41,5};
int second[5] = {5,22,35,4,4};
int third[5];
cout <<
"\nArray 1: ";
for(int i = 0; i < SIZE; ++i){
cout <<
first[i] << " ";
}
cout << "\nArray 2: ";
for(int i = 0; i < SIZE; ++i){
cout << second[i] << "
";
}
addarrays(first,second,third,SIZE);
cout
<< "\nSum of arrays: ";
for(int i = 0; i < SIZE; ++i){
cout << third[i] << " ";
}
char c;
cin
>> c;
}
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
C++