C++ program to add the contents of two arrays:
In this post, we will learn how to add the contents of two arrays in C++. The program will create these arrays with user-input values, calculate the sum of its contents and print the result. We will also learn how to use a separate function and separate class to write this program.
Example 1: C++ program to add the contents of two arrays:
Let’s take a look at the below example program:
#include <iostream>
using namespace std;
int main()
{
int arrSize;
cout << "Enter the size of the arrays: " << endl;
cin >> arrSize;
int firstArr[arrSize];
int secondArr[arrSize];
int resultArr[arrSize];
cout << "Enter the numbers for the first array: " << endl;
for (int i = 0; i < arrSize; i++)
{
cout << "Enter for index " << i << " :" << endl;
cin >> firstArr[i];
}
cout << endl;
cout << "Enter the numbers for the second array: " << endl;
for (int i = 0; i < arrSize; i++)
{
cout << "Enter for index " << i << " :" << endl;
cin >> secondArr[i];
}
for (int i = 0; i < arrSize; i++)
{
resultArr[i] = firstArr[i] + secondArr[i];
}
cout << "Result Array: " << endl;
for (int i = 0; i < arrSize; i++)
{
cout << resultArr[i] << " ";
}
}
Here,
- It asks the user to enter the size of the array first. It is assigned to the arrSize variable.
- It created three arrays of size arrSize. The array firstArr will hold the numbers of the first array, secondArr will hold the numbers of the second array and resultArr will hold the numbers of the final result array.
- The first two for-loops are used to read the inputs for the arrays. It reads the inputs and assigns the values to the arrays.
- The third for loop is used to find the result of the array numbers. These values are assigned to the final result array resultArra
If you run this program, it will print the output as below:
Enter the size of the arrays:
5
Enter the numbers for the first array:
Enter for index 0 :
1
Enter for index 1 :
2
Enter for index 2 :
3
Enter for index 3 :
4
Enter for index 4 :
5
Enter the numbers for the second array:
Enter for index 0 :
2
Enter for index 1 :
3
Enter for index 2 :
4
Enter for index 3 :
5
Enter for index 4 :
6
Result Array:
3 5 7 9 11
Example 2: C++ program to add the contents of two arrays by using separate functions:
Let’s use separate functions to write the above program. We will use three functions to read the array, find the sum of two arrays and print the content of an array.
#include <iostream>
using namespace std;
void readArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << "Enter for index " << i << " :" << endl;
cin >> arr[i];
}
}
void findSum(int arr1[], int arr2[], int result[], int size)
{
for (int i = 0; i < size; i++)
{
result[i] = arr1[i] + arr2[i];
}
}
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
}
int main()
{
int arrSize;
cout << "Enter the size of the arrays: " << endl;
cin >> arrSize;
int firstArr[arrSize];
int secondArr[arrSize];
int resultArr[arrSize];
cout << "Enter the numbers for the first array: " << endl;
readArray(firstArr, arrSize);
cout << endl;
cout << "Enter the numbers for the second array: " << endl;
readArray(secondArr, arrSize);
findSum(firstArr, secondArr, resultArr, arrSize);
cout << "Result Array: " << endl;
printArray(resultArr, arrSize);
cout << endl;
}
In this program,
- The readArray function is used to read the contents for an array. It reads the numbers and assigns the numbers to a given array.
- The findSum function finds the sum of the numbers of two arrays and assigns the result to a different array.
- The printArray function prints the content of an array.
- All of these functions are called from the main function and it will print output similar to the previous example.
Enter the size of the arrays:
3
Enter the numbers for the first array:
Enter for index 0 :
1
Enter for index 1 :
2
Enter for index 2 :
3
Enter the numbers for the second array:
Enter for index 0 :
5
Enter for index 1 :
6
Enter for index 2 :
8
Result Array:
6 8 11
You can see that this program is clean than the previous program and code quantity is also reduced. We can make it even better by using a separate class.
Example 3: C++ program to add the contents of two arrays by using a separate class:
The below program creates a separate class to read and add the contents of two arrays:
#include <iostream>
using namespace std;
class ArraySum
{
public:
void readArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << "Enter for index " << i << " :" << endl;
cin >> arr[i];
}
}
void findSum(int arr1[], int arr2[], int result[], int size)
{
for (int i = 0; i < size; i++)
{
result[i] = arr1[i] + arr2[i];
}
}
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
}
};
int main()
{
int arrSize;
ArraySum arrSum;
cout << "Enter the size of the arrays: " << endl;
cin >> arrSize;
int firstArr[arrSize];
int secondArr[arrSize];
int resultArr[arrSize];
cout << "Enter the numbers for the first array: " << endl;
arrSum.readArray(firstArr, arrSize);
cout << endl;
cout << "Enter the numbers for the second array: " << endl;
arrSum.readArray(secondArr, arrSize);
arrSum.findSum(firstArr, secondArr, resultArr, arrSize);
cout << "Result Array: " << endl;
arrSum.printArray(resultArr, arrSize);
cout << endl;
}
You might also like:
- 3 ways to find the exponent without using pow() method in C++
- 3 different C++ programs to find electricity bill with user given units
- 2 different C++ program to check if a number is spy number or not
- C++ program to insert an element to an array at any position
- C++ program to find the volume of a cylinder in 3 different ways
- C++ program to find the sum of two numbers using friend function
- C++ program to print the days of the week by using switch case
- C++ program to find the area of a rectangle