-2

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.

user3411961
  • 125
  • 1
  • 8

3 Answers3

1

You could accept references as 2 additional arguments. You can then set sumA and sumB to the correct values.

void sumArray (int a[], int b[], int & sumA, int & sumB)
{
  sumA=0;
  sumB=0;
  for(int i=0; i<4; i++){
    sumA += a[i];
    sumB += b[i];
  }
}

Then you can use it like

int firstarray[4];
int secondarray[4];
// do your stuff here
int sumA, sumB;
sumArray(firstarray,secondarray, sumA, sumB);
cout<<"sum of array 1 is: "<<sumA<<endl;
cout<<"sum of array 2 is: "<<sumB<<endl;
Ceros
  • 491
  • 3
  • 14
0
std::tuple<int,std::string> return_two()
{
    return std::make_tuple(42, "don't panic");
}



auto sval = std::string{};
auto ival = 0;
std::tie(ival,sval) = return_two();
Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • 2
    That's a code-only answer without explanations. Also, it uses language features that OP doesn't even mention he could use, and you don't even include the headers necessary. – Marcus Müller Apr 12 '16 at 18:30
  • please try to add some description of your code. It will be helpful in understanding code – silwar Apr 13 '16 at 06:20
0

Why not just call that function twice?
Change your sumArray function to:

int sumArray(int array[]){
int sum=0;
for(int i=0; i<4; i++){
    sum += array[i];
}
return sum;
}


Then for the rest of your code:

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);

cout<<"sum of array 1 is: "<<sumArray(firstArray)<<endl;
cout<<"sum of array 2 is: "<<sumArray(secondArray)<<endl;

return 0;
}

If you can't do it that way, then you can return a pointer to the first part of an array. As seen here: Return array in a function

Community
  • 1
  • 1
Adam Forbis
  • 461
  • 3
  • 11