Answer to Question #259631 in C++ for jye

Question #259631

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


1
Expert's answer
2021-11-01T08:28:23-0400
#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] << " ";
	}
}

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS