Javatpoint Logo
Javatpoint Logo

How to Add two Array in C

Arrays are a fundamental data structure in programming languages that allow for efficient storage and manipulation of multiple values of the same data type. In a program, arrays can be used to store and organize large amounts of data, such as a list of customers, a collection of images, or a set of numerical values. They can also be used to store the results of computations, such as the elements of a matrix or the values of a function at specific points.

Arrays are often used in real-life applications as well. For example, in image processing, an array can be used to represent a digital image as a 2D array of pixels, where each element of the array represents the color of a specific pixel. In scientific computing, arrays are used to store and manipulate large sets of data, such as the results of simulations or experiments. In databases, arrays can be used to store and retrieve large amounts of data, such as a list of customer records or a collection of products. Additionally, arrays are also used in control systems, such as robotics, to store and process sensor data.

Overall, arrays are a powerful tool for storing and manipulating large amounts of data in a way that is both efficient and easy to understand. They are widely used in a variety of applications, from image processing and scientific computing to databases and control systems.

C Code

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 program defines a constant, ARRAY_SIZE, which is used to specify the size of the array. In this case, ARRAY_SIZE is set to 1000000, which creates an array of one million integers.

The program then uses a for loop to populate the array with values. In this case, each element of the array is set to its corresponding index value, so the first element is 0, the second element is 1, and so on.

Add two Array in C

Here is an example of a C program that adds the elements of two arrays of integers and stores the result in a third array:

C Code

Output

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

Explanation:

This program defines a constant, ARRAY_SIZE, which is used to specify the size of the arrays. In this case, ARRAY_SIZE is set to 5, which creates arrays of five integers. The program then initializes two arrays, array1 and array2, with some fixed values. In this case, array1 is set to {1, 2, 3, 4, 5} and array2 is set to {5, 4, 3, 2, 1}.

The program then uses a for loop to add the elements of the two arrays, element by element, and stores the result in the resultArray. Finally, the program uses another for loop to print the elements of the resultArray. The loop iterates from 0 to ARRAY_SIZE - 1, and for each iteration, it prints the value of the current element, along with its index in the array.

It is important to note that the arrays must have the same size in order to add them element by element. If the size of the arrays is different, the program will result in an error or unexpected behavior.

Also, the program assumes that the arrays are already initialized with values, otherwise it would result in undefined behavior.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA