Question: Write a function sumArray() that has as parameters two arrays A and B and calculates and stores the sum of corresponding elements of the arrays. Include any additional parameter(s) you consider necessary. Write a main() function that uses the function inputArray() in the previous slide to perform the input of two integer arrays X1 and X2 and uses sumArray() to calculate the sum of corresponding values of X1 and X2. It then displays the calculated values.
I was searching a way to return multiple values from a function but still it is not working even after trying some examples from SO. can anyone help me with it i just start learning it.
Problem: i can't figure out how to get the values of sumA and sumB in main().
Here are my codes:
#include <iostream>
using namespace std;
int sumArray(int a[], int b[]){
int sumA=0, sumB=0;
for(int i=0; i<4; i++){
sumA += a[i];
sumB += b[i];
}
return sumA,sumB;
}
void inputArray(int arg[], int n){
for(int i=0; i<n; i++){
cin>>arg[i];
}
}
int main(){
int firstarray[4];
int secondarray[4];
int l=4;
cout<<"Input 4 values for the array 1: ";
inputArray(firstarray,l);
cout<<"Input 4 values for the array 2: ";
inputArray(secondarray,l);
sumArray(firstarray,secondarray);
cout<<"sum of array 1 is: "<<firstarray<<endl;
cout<<"sum of array 2 is: "<<secondarray<<endl;
return 0;
}
This is the only way i have learned it.