Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C program to dynamically make array and print elements sum
In C programming, dynamic memory allocation allows us to create arrays of variable size at runtime. This is useful when we don't know the array size at compile time. We use malloc() or calloc() functions from the stdlib.h header to allocate memory dynamically.
So, if the input is like n = 6, and array elements 9, 8, 7, 2, 4, 3, then the output will be 33 because sum of 9 + 8 + 7 + 2 + 4 + 3 = 33.
Syntax
int *arr = (int*) malloc(size * sizeof(int)); int *arr = (int*) calloc(size, sizeof(int));
Algorithm
To solve this, we will follow these steps −
Initialize sum := 0
Read the size n from input
Dynamically allocate memory for an array of size n
Read n elements into the array
Calculate sum of all array elements
Display the sum and free allocated memory
Example 1: Using malloc()
Let us see the following implementation using malloc() function −
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
/* Allocate memory for n integers */
arr = (int*) malloc(n * sizeof(int));
/* Check if memory allocation was successful */
if (arr == NULL) {
printf("Memory allocation failed!<br>");
return 1;
}
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
/* Calculate sum */
for (int i = 0; i < n; i++) {
sum += arr[i];
}
printf("Sum of array elements: %d<br>", sum);
/* Free allocated memory */
free(arr);
return 0;
}
Enter number of elements: 6 Enter 6 elements: 9 8 7 2 4 3 Sum of array elements: 33
Example 2: Using calloc()
Here's the same program using calloc() which initializes allocated memory to zero −
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
/* Allocate memory for n integers (initialized to 0) */
arr = (int*) calloc(n, sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!<br>");
return 1;
}
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n; i++) {
sum += arr[i];
}
printf("Sum of array elements: %d<br>", sum);
free(arr);
return 0;
}
Enter number of elements: 4 Enter 4 elements: 5 10 15 20 Sum of array elements: 50
Key Points
- malloc() allocates uninitialized memory, while calloc() initializes memory to zero.
- Always check if memory allocation was successful by comparing with
NULL. - Always call
free()to release allocated memory and prevent memory leaks. - Use
sizeof(int)to ensure correct memory size calculation.
Conclusion
Dynamic memory allocation in C using malloc() or calloc() provides flexibility to create arrays of runtime-determined sizes. Remember to always check for allocation success and free the memory after use.
