Step 1: Start
Step 2: Declare integers n, i, *ptr, sum
Step 3: Prompt user to enter the number of elements
Step 4: Allocate memory using calloc
Step 5: Check if memory allocation was successful
Step 6: If not, display error and exit
Step 7: Input elements into dynamically allocated array
Step 8: Calculate sum of array elements
Step 9: Display the elements and the sum
Step 10: Free the allocated memory
Step 11: End
/* Program for Dynamic Memory allocation using calloc function */ #include <stdio.h> #include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) calloc(n, sizeof(int)); if (ptr == NULL) { printf("Sorry! unable to allocate memory"); exit(0); } printf("Enter elements of array: "); for (i = 0; i < n; i++) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("\nSum of"); for (i = 0; i < n; i++) { printf("\n %d", *(ptr + i)); } printf("\n------"); printf("\n %d", sum); free(ptr); return 0; }
Enter number of elements: 4 Enter elements of array: 10 20 30 40 Sum of 10 20 30 40 ------ 100