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.
1
Expert's answer
2011-12-29T08:10:59-0500
#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){
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments