C Program to Find Sum of two One-Dimensional Arrays using Malloc

This is a C Program to Compute the Sum of two One-Dimensional Arrays using malloc().

Problem Description

We have to write a program in C such that the program will allocate 2 one-dimensional arrays using malloc() call and then will do the addition and stores the result into 3rd array. The 3rd array is also allocated using a malloc() call.

Expected Input and Output

If we are entering 5 elements (N=5) in both arrays with arrays elements values as 23, 45, 67, 12, 90 and 87, 56, 90, 45, 10 respectively, then the resultant array will have values as 110, 101, 157, 57, 100.

Problem Solution

This program uses the C libary malloc() function which is used for memory allocation. The syntax of malloc is:

pointer_name = (cast-type*) malloc (byte-size)

advertisement
advertisement

For example, if you have to allocate space for 5 integers which is pointed to by a pointer variable ptr, then we do it as follows.

int *ptr = (int *) malloc (5 * sizeof(int));

The above malloc() will allocate 20 bytes of storage, where each integer is 4-bytes size.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

The sequence of steps for the solution will be as follows:
1. Take the size of an array as input from users. (Note: The size of all the three arrays should be same.)
2. Declare 3 one-dimensional arrays, let’s say a, b and c (a, b will be the arrays to be added and c will be the resultant array) and allocate space for them using malloc() call.
3. Now, take input for all the elements of the 2 arrays.
4. Running a for loop from 0 to array size, iterate over the elements of both the arrays and add the elements of the same indices and save them in the third array.
5. Print array elements of third array.

Program/Source Code

Here is source code of the C program to compute the sum of two one-dimensional arrays using malloc(). The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C program to find the sum of two one-dimensional arrays using
  3.  * Dynamic Memory Allocation
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <malloc.h>
  8. #include <stdlib.h>
  9.  
  10. void main()
  11. {
  12.  
  13.         int i, n;
  14. 	int *a, *b, *c;
  15.  
  16.         printf("Enter the size of the arrays\n");
  17.         scanf("%d", &n);
  18.  
  19.         a = (int *)malloc(n * sizeof(int));
  20.         b = (int *)malloc(n * sizeof(int));
  21.         c = (int *)malloc(n * sizeof(int));
  22.  
  23.         printf("Enter Elements of First List\n");
  24.  
  25.         for (i = 0; i < n; i++)
  26.         {
  27.   		scanf("%d", a + i);
  28. 	}
  29.  
  30.         printf("Enter Elements of Second List\n");
  31.  
  32.         for (i = 0; i < n; i++)
  33.         {
  34.  		scanf("%d", b + i);
  35. 	}
  36.  
  37.         for (i = 0; i < n; i++)
  38.         {
  39. 		*(c + i) = *(a + i) + *(b + i);
  40. 	}
  41.  
  42.         printf("Resultant List is\n");
  43.  
  44.         for (i = 0; i < n; i++)
  45.         {
  46. 		printf("%d\n", *(c + i));
  47. 	}
  48.         return 0;
  49. }
Program Explanation

1. Declare a variable (n), in which the size of the array(s) will be stored.
2. Declare 3 pointer variables of integer type, to hold starting address of 3 different arrays (a, b and c).
3. Using for loop running from 0 to arraySize-1, go at every location by adding the value of integer i(the iterator)to the starting address of the array(s) and then take the input by storing values to those locations.
4. Do the previous step for 2 arrays (i.e a and b).
5. Now, again using for loop, adding the value of i to the starting address of both the arrays (a and b), extract each element from both the arrays holdd by a and b, add the elements and store the result in the third array (i.e c).
6. Print the elements of array c.

Runtime Test Cases
advertisement

Here is the runtime output of the C program where the user is reading two arrays of 5 elements with values as “23, 45, 67, 12, 90” in array1 and “87, 56, 90, 45, 10” in array2. Subsequently, it is adding the elements of both the arrays and displaying the final result.

Enter the size of the arrays
5
Enter Elements of First List
23
45
67
12
90
Enter Elements of Second List
87
56
90
45
10
Resultant List is
110
101
157
57
100

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.