 # C Program to find sum of two one-dimensional arrays using dynamic memory allocation

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

In this c program, we have to calculate the sum of two one-dimensional arrays using dynamic memory allocation. For that, we have to know more about dynamic memory allocation, and its properties.

## What is dynamic memory allocation?

Dynamic memory is allocated from the heap by using the dynamic memory allocation functions such as malloc(), calloc(), or realloc(). If arrays to be used in a c program, we have to declare the size of the array initially. But if the arrays declared here are insufficient to hold the data or more than required. In such a condition, we allocate memory dynamically during the run time. There are four library functions included in dynamic memory allocation. They are:

1. malloc()

2. calloc()

3. free()

4. realloc()

In this c program, we have to calculate the sum of two one-dimensional arrays using dynamic memory allocation. An array is a kind of data structure that can store a fixed sequential collection of elements of the same type. Arrays are classified into different types as per the arrangement of elements in the arrays. They are one-dimensional arrays, two-dimensional arrays.

In this c program, we use three, one-dimensional arrays a, b, c. Array a and b contain the element which is to be added, and the array c has the sum. Then we will display list c as the resultant list by using `for loop`.

### ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare the variable i, j, *a, *b, *c .

STEP 3: Read the number of elements into the variable n.

STEP 4: Allocate space for a, b, c using the malloc function.

STEP 5: Read the elements of the first list and save it into the position a+i using `for loop`.

STEP 6: Read the elements of the second list and save it into the position b+i using `for loop`.

STEP 7: By using `for loop` calculate the sum of the list a and b like *(c+i) = *(a+i) + *(b+i).

STEP 8: Display the resultant list as *(c+i) using for loop and `printf` function.

## C Source Code

```                                          ```#include <stdio.h>
#include <stdlib.h>

void main() {
int i, n;
int * a, * b, * c;
printf("How many Elements in each array...\n");
scanf("%d", & n);
a = (int * ) malloc(n * sizeof(int));
b = (int * ) malloc(n * sizeof(int));
c = (int * ) malloc(n * sizeof(int));
printf("Enter Elements of First List\n");
for (i = 0; i < n; i++) {
scanf("%d", a + i);
}
printf("Enter Elements of Second List\n");
for (i = 0; i < n; i++) {
scanf("%d", b + i);
}
for (i = 0; i < n; i++) {
*(c + i) = * (a + i) + * (b + i);
}
printf("Resultant List is\n");
for (i = 0; i < n; i++) {
printf("%d\n", *(c + i));
}
} /* End of main() */```
```

## OUTPUT

```How many Elements in each array...
4

Enter Elements of First List
1
2
3
4

Enter Elements of Second List
6
7
8
9

Resultant List is
7
9
11
13
```
VIEW ALL
VIEW ALL
##### OtherTutorials
VIEW ALL 