This is a C Program to Compute the Sum of two One-Dimensional Arrays using malloc().
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.
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.
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)
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.
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.
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.
/*
* C program to find the sum of two one-dimensional arrays using
* Dynamic Memory Allocation
*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main()
{
int i, n;
int *a, *b, *c;
printf("Enter the size of the arrays\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));
}
return 0;
}
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.
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.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Watch Advanced C Programming Videos
- Buy C Books
- Apply for Computer Science Internship
- Practice BCA MCQs
- Buy Computer Science Books