Sum Two Arrays Element by Element in C++

In this exercise, you will implement a function that sums two arrays element by element. This means that for two given arrays of the same size, the function will return a new array where each element is the sum of the corresponding elements in the original arrays.

This exercise will help you practice working with arrays, understanding element-wise operations, and utilizing loops to process data in C++. It's a useful operation in many algorithms, especially in tasks that involve merging or combining data.

Group

Arrays and Vectors in C++

Objective

1. Define two arrays of the same size.
2. Implement a function that iterates through both arrays and adds their corresponding elements.
3. Store the result in a new array.
4. Output the new array containing the summed elements.
5. Test the program with different arrays to ensure it works correctly.

Develop a function that sums two arrays element by element.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include iostream for input and output
using namespace std; // Use the standard namespace

// Function to sum two arrays element by element
void sumArrays(int arr1[], int arr2[], int result[], int size) {
    // Loop through each element of the arrays
    for (int i = 0; i < size; i++) {
        result[i] = arr1[i] + arr2[i]; // Add corresponding elements of arr1 and arr2, store in result
    }
}

int main() {
    // Define two arrays to sum
    int arr1[] = {1, 2, 3, 4, 5};
    int arr2[] = {5, 4, 3, 2, 1};
    int size = sizeof(arr1) / sizeof(arr1[0]); // Calculate the size of the arrays
    int result[size]; // Create an array to store the result

    // Call the function to sum the arrays
    sumArrays(arr1, arr2, result, size);

    // Output the result array
    cout << "Summed array: ";
    for (int i = 0; i < size; i++) {
        cout << result[i] << " "; // Print each element of the summed array
    }
    cout << endl;

    return 0; // End of program
}

 Output

Summed array: 6 6 6 6 6

Share this C++ Exercise


More C++ Programming Exercises of Arrays and Vectors in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Remove Duplicates from an Array in C++

    In this exercise, you will create a program that removes duplicates from an array of numbers. The function will iterate over the array and remove any duplicate values, leaving only...

  • Check if an Array is Symmetric in C++

    In this exercise, you will create a function to determine if an array is symmetric. An array is considered symmetric if it reads the same forward and backward, meaning the first el...

  • Sort an Array of Numbers in Ascending Order in C++

    In this exercise, you will implement a program that sorts an array of numbers in ascending order. Sorting is one of the most fundamental operations in programming, and there are se...

  • Sum of Array Elements in C++

    In this exercise, you will create a program that calculates the sum of all the elements in an array. Summing the elements of an array is a common task in programming, which is ofte...

  • Search for a Number in an Array in C++

    In this exercise, you will create a program that searches for a specific number in an array. Searching through an array is a common task in programming, which is used in various ap...

  • Calculate the Average of Array Elements in C++

    In this exercise, you will create a function that calculates the average of the elements in an array. This is a common task when working with arrays, especially in data analysis, w...