1. Write a program in C programming to dynamically allocate memory for an array of integers and display the sum of all elements.
Objective
The fundamental objective of this program is to show how to dynamically allocate memory for an array of integers in c language and to compute the sum of all elements of the array.
Purpose
The purpose of this program is to explain the concept of dynamic memory allocation in c language and how it is used to work with arrays of integers.
with the help of dynamic memory allocation a program can adjust the size of an array at run time making it more flexible and effectual.
Real life applications
Dynamic memory allocation is generally used in programming for effective memory management mainly when the size of the data structure is not known in advance.
Real life applications of dynamic memory allocation contain managing large data sets implementing data structures such as linked lists, trees and optimizing memory utilization in resource constrained environments.
C program to allocate memory dynamically for an array of integers and calculate the sum of all elements.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int size, *arr, sum = 0;
// prompt or ask user to enter the size of the array
printf("\n Enter the size of the array\n ");
scanf("%d", &size);
// allocate memory for the array dynamically
arr = (int *)malloc(size * sizeof(int));
// Test whether memory allocation is successful
if (arr==NULL)
{
printf("\n Unable to allocate memory!");
return 1;
}
// display message to user to enter the elements of the array
printf("\nEnter %d elements\n", size);
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
// Compute the sum of all elements in the array
for (int i=0;i< size;i++)
{
sum +=arr[i];
}
// Display the sum of all elements
printf("\nThe sum of all elements in the array is= %d\n", sum);
// Free the dynamically allocated memory
free(arr);
return 0;
}
Output
Enter the size of the array
5
Enter 5 elements
11
12
13
14
15
The sum of all elements in the array is=65
Program Explanation
The start of the program includes the standard library required for input output and memory allocation operations.
The main function is the entry or departure point of the program.
The program declares and defines array sizes and variables * arr and sum, respectively storing the sum of objects.
The program declares and defines the sum of the numeric size and variable * arr.
This will ask the user to enter the combined size using the printf function.
The malloc function is used to combine memory based on user input.
(arr = = NULL) To check if memory allocation is successful, check if arr is NULL.
The program displays the "Can't allocate memory!" error message and ends when the memory allocation fails.
The program then asks the user to enter the marble element.
The sum of all the elements in the column is calculated by repeating each element and adding it to the total.
This program displays the sum of all the elements in the array.
Use the "Free" feature to dynamically release allocated memory and return it to system memory.
The program returns 0 for success.
Previous :-->> Storage class Assignments in c.
-->> NEXT:
2. Write a program in C that dynamically allocates memory for a string and reverses the string in place.
-->>ALL
Dynamic Memory Allocation assignments in c