Write a C program to find the sum of ‘n’ numbers using dynamically allocating memory :
In this tutorial, we will learn how to find the sum of ‘n’ numbers by allocating memory dynamically. By allocating memory dynamically, we can use only the amount of memory that is required. If the user will enter 10 integers, we will allocate memory for 10 integers. Similarly, for 100 integers, we will allocate memory for 100 integers. We will use the malloc
method for dynamic memory allocation and free
method to free up unused memory.
How malloc()
works:
The malloc
function is used for memory allocation. We can pass the size of the memory block to allocate as its parameter and it will allocate that amount of memory. The syntax of the malloc
function is:
void *malloc(size_t size)
Where size
is the size of the memory to allocate in bytes. It will return a pointer to the allocated memory and we can cast that to the required type. It returns a NULL
pointer if the memory can’t be allocated.
For example:
int *p = (int*) malloc(10 * sizeof(int));
This code snippet will allocate memory for 10 integers. The returned value is casted to an integer pointer.
free()
method:
The free
method is used to de-allocate memory dynamically in C. We should always de-allocate memory if we are not using it. The syntax of the free
method is:
free(p)
It takes the pointer of the allocated memory as its parameter.
Method 1: C program to allocate memory dynamically and find the sum of ‘n’ numbers using for loop:
Let’s write a C program that allocates the memory dynamically for n
numbers to find the sum of these numbers.
#include <stdio.h>
#include <stdlib.h>
int main()
{
// 1
int i;
int count;
int *arr;
int sum = 0;
// 2
printf("Enter the total number of elements you want to enter : ");
scanf("%d", &count);
// 3
arr = (int *)malloc(count * sizeof(int));
// 4
for (i = 0; i < count; i++)
{
// 5
printf("Enter element %d : ", (i + 1));
scanf("%d", arr + i);
// 6
sum += *(arr + i);
}
// 7
printf("sum is %d \n", sum);
// 8
free(arr);
return 0;
}
Explanation :
The commented numbers in the above program denote the step numbers below:
- Initialize all the variables. The integer
i
is used in the loop,count
is used to store the total number of elements the user will enter,int *arr
is a pointer to use for the allocated memory, and thesum
is to hold the sum of the numbers. Thesum
variable is initialized as0
. - Ask the user to enter the total number of elements and assign this value to the variable count.
- Allocate memory to the int pointer variable. We are using
malloc
to allocate memory forcount
number of integers. - Use one for loop to read the integer values. On each iteration of the loop:
- Ask the user to enter each number. Read it and assign it to the
i
th position of the allocated memory space. - Add the number to the
sum
variable. Thesum
variable holds the total sum of all the user entered numbers.
- Ask the user to enter each number. Read it and assign it to the
- After the loop is completed, we have the total sum saved in the variable
sum
. Print out this value to the user. - At the end of the program, we need to use the
free
method to de-allocate the allocated memory.
Sample Output :
Enter the total number of elements you want to enter : 5
Enter element 1 : 1
Enter element 2 : 2
Enter element 3 : 3
Enter element 4 : 4
Enter element 5 : 4
sum is 14
Enter the total number of elements you want to enter : 10
Enter element 1 : 1
Enter element 2 : 2
Enter element 3 : 3
Enter element 4 : 4
Enter element 5 : 5
Enter element 6 : 6
Enter element 7 : 7
Enter element 8 : 8
Enter element 9 : 9
Enter element 10 : 10
sum is 55
Method 2: C program to allocate memory dynamically and find the sum of ‘n’ numbers with a while loop:
Similar to the above example, we can also use a while
loop to find the sum of n
numbers with dynamic memory allocation. The following program uses a while
loop:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
int count;
int *arr;
int sum = 0;
printf("Enter the total number of elements you want to enter : ");
scanf("%d", &count);
arr = (int *)malloc(count * sizeof(int));
while (i < count)
{
printf("Enter element %d : ", (i + 1));
scanf("%d", arr + i);
sum += *(arr + i);
i++;
}
printf("sum is %d \n", sum);
free(arr);
return 0;
}
- We have to initialize the variable
i
before the loop starts. It is initialized as 0. - The loop will run until the value of
i
is smaller thancount
. - At the end of each iteration of the loop, the value of
i
is incremented by 1.
If you run this program, it will give similar output as the previous program.
Method 3: By using a do-while loop:
We can also use a do..while
loop instead of while
loop. It works in a similar way. The do..while
loop runs the code in the do
block before it checks the condition of the while
block. The complete program is:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
int count;
int *arr;
int sum = 0;
printf("Enter the total number of elements you want to enter : ");
scanf("%d", &count);
arr = (int *)malloc(count * sizeof(int));
do
{
printf("Enter element %d : ", (i + 1));
scanf("%d", arr + i);
sum += *(arr + i);
i++;
} while (i < count);
printf("sum is %d \n", sum);
free(arr);
return 0;
}
It will give similar results.
You might also like:
- C program to find the third angle of a triangle if other two are given
- C program to separate even and odd numbers from an array
- C program to remove the vowels from a string
- C program to find the power of a number using loop
- C program to calculate the total number of lines in a file
- C program to check if a year is leap year or not