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


March 12, 2022, Learn eTutorial
2273

To find the sum of Arrays using Dynamic Memory Allocation, we need to know more about its properties.

What is dynamic memory allocation?

Dynamic memory allocation is defined as the allocation of memory dynamically during the run time from the heap by using dynamic memory allocation functions.

For example, If arrays are 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, in such a condition we allocate memory dynamically during the run time. There are four library functions used to allocate memory dynamically. They are:

  1. malloc()
  2. calloc()
  3. free()
  4. realloc()

How to find the sum of two one-dimensional arrays using dynamic memory allocation in a C program?

Here, we have to create two arrays using dynamic memory allocation and calculate the sum of elements in arrays and store it in another dynamic array.

In this C program, we use three arrays a, b & c, where arrays a and b contain the user input element lists which are to be added. Now we add each element of the array a and array b and store the sum in array c using a for loop. Then we will display array c as the result using for loop.

ALGORITHM

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

STEP 2: Declare the variables i, n, *a, *b, *c of type int.

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 array and save it into the position a+i using for loop.

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

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

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


To find the sum of arrays using dynamic memory allocation, we need to know below topics in C programming, please refer to those for a better understanding

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