Write a function that sums each pair of elements of 2 arrays (plus correspondingly by index), resulting in a third array. Arrays have the same number of elements. Write the main program using the upper function and display the results to the screen
#include <iostream>
using namespace std;
float* sum(const float* arr1,const float* arr2, int size){
float* ans = new float[size];
for(int i = 0; i < size;i++){
ans[i] = arr1[i] + arr2[i];
}
return ans;
}
int main(int argc, char *argv[]){
int size;
cout << "enter size ";
cin >> size;
float* arr1 = new float[size];
float* arr2 = new float[size];
cout << "enter the first array: ";
for(int i = 0; i < size; i++){
cin >> arr1[i];
}
cout << "enter the second array: ";
for(int i = 0; i < size; i++){
cin >> arr2[i];
}
float* out;
out = sum(arr1, arr2, size);
cout << "output array: ";
for(int i = 0; i < size; i++){
cout << out[i] << " ";
}
}
Comments
Leave a comment