Write a programmer defined function to add the elements of two arrays of 5 integ
ID: 3810983 • Letter: W
Question
Write a programmer defined function to add the elements of two arrays of 5 integers each. Your main function should declare three integer arrays two for holding the elements to be added and 1 for holding the sum, and ask the user the enter the value for the first two arrays. Then, you need to call your programmer defined function and pass the three arrays to it. Now your function needs to compute the sum from the elements of the first two array and store in the third array. Display the elements of the sum array that in your main function.Explanation / Answer
Here is the C++ code.
You can run it in Xcode or devC++
#include <iostream>
using namespace std;
void add_array(int a[], int b[], int c[])
{
for(int i=0;i<5;i++)
{
c[i]=a[i]+b[i];
}
}
int main(int argc, const char * argv[])
{
int a[5],b[5],c[5];
int i,temp;
//initialise a
for(i=0;i<5;i++)
{
cout<<" Enter value for a: ";
cin>>temp;
a[i]=temp;
}
//initialise b
for(i=0;i<5;i++)
{
cout<<" Enter value for b: ";
cin>>temp;
b[i]=temp;
}
//add the data
add_array(a,b,c);
//output data
for(i=0;i<5;i++)
{
cout<<c[i]<<" ";
}
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.