How To Add Two Array In C

Overall, arrays serve as a robust mechanism for storing and managing substantial volumes of data efficiently while maintaining clarity. They find extensive application across various fields such as image processing, scientific computation, database management, and control system operations.

C Code

Example

#include <stdio.h>
#define ARRAY_SIZE 10

int main() {
    int i;
    int largeArray[ARRAY_SIZE];
    
    // Populate the array with values
    for (i = 0; i < ARRAY_SIZE; i++) {
        largeArray[i] = i;
    }

    // Print the elements of the array
    for (i = 0; i < ARRAY_SIZE; i++) {
        printf("largeArray[%d] = %d\n", i, largeArray[i]);
    }

    return 0;
}

Output

Output

largeArray[0] = 0
largeArray[1] = 1
largeArray[2] = 2
largeArray[3] = 3
largeArray[4] = 4
largeArray[5] = 5
largeArray[6] = 6
largeArray[7] = 7
largeArray[8] = 8
largeArray[9] = 9

Explanation:

This code establishes a constant named ARRAYSIZE, which determines the size of the array. Here, ARRAYSIZE is assigned a value of 1000000, resulting in an array containing one million integers.

The software then employs a for loop to fill the array with values. Here, each item in the array is assigned its respective index value, meaning the initial item is 0, the succeeding item is 1, and so forth.

Add two Array in C

Here is an illustration of a C program that sums up the elements of two integer arrays and saves the outcome in a separate array:

C Code

Example

#include <stdio.h>
#define ARRAY_SIZE 5

int main() {
    int i;
    int array1[ARRAY_SIZE] = {1, 2, 3, 4, 5};
    int array2[ARRAY_SIZE] = {5, 4, 3, 2, 1};
    int resultArray[ARRAY_SIZE];

    // Add the elements of the two arrays and store the result in the resultArray
    for (i = 0; i < ARRAY_SIZE; i++) {
        resultArray[i] = array1[i] + array2[i];
    }

    // Print the elements of the resultArray
    for (i = 0; i < ARRAY_SIZE; i++) {
        printf("resultArray[%d] = %d\n", i, resultArray[i]);
    }

    return 0;
}

Output

Output

resultArray[0] = 6
resultArray[1] = 6
resultArray[2] = 6
resultArray[3] = 6
resultArray[4] = 6

Explanation:

This script establishes a constant, ARRAYSIZE, to indicate the size of arrays. Here, ARRAYSIZE is designated as 5, resulting in arrays consisting of five integers. Subsequently, the script initializes two arrays, array1 and array2, with specific predetermined values. In this instance, array1 is initialized with {1, 2, 3, 4, 5} and array2 with {5, 4, 3, 2, 1}.

The software subsequently employs a for loop to combine the components of the two arrays incrementally and saves the outcome in the resultArray. Subsequently, another for loop is utilized to display the elements within the resultArray. The iteration spans from 0 to ARRAY_SIZE - 1, and during each cycle, it showcases the current element's value alongside its corresponding index in the array.

It is crucial to emphasize that the arrays need to be of identical size to perform element-wise addition. When the arrays have varying sizes, the program is likely to encounter errors or exhibit unpredictable behavior.

Additionally, the software expects that the arrays have been initialized with values beforehand; otherwise, it may lead to unpredictable behavior.

Input Required

This code uses input(). Please provide values below: