C Program to Find Largest Number Using Dynamic Memory Allocation
Depending upon the number of elements, the required size is allocated which prevents the wastage of memory. If no memory is allocated, error is displayed and the program is terminated.
Example: Find Largest Element Using Dynamic Memory Allocation – calloc()
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, num;
float *data;
printf("Enter total number of elements(1 to 100): ");
scanf("%d", &num);
// Allocates the memory for 'num' elements.
data = (float*) calloc(num, sizeof(float));
if(data == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
printf("\n");
// Stores the number entered by the user.
for(i = 0; i < num; ++i)
{
printf("Enter Number %d: ", i + 1);
scanf("%f", data + i);
}
// Loop to store largest number at address data
for(i = 1; i < num; ++i)
{
// Change < to > if you want to find the smallest number
if(*data < *(data + i))
*data = *(data + i);
}
printf("Largest element = %.2f", *data);
return 0;
}
Output
Enter total number of elements(1 to 100): 10 Enter Number 1: 2.34 Enter Number 2: 3.43 Enter Number 3: 6.78 Enter Number 4: 2.45 Enter Number 5: 7.64 Enter Number 6: 9.05 Enter Number 7: -3.45 Enter Number 8: -9.99 Enter Number 9: 5.67 Enter Number 10: 34.95 Largest element: 34.95
a step-by-step explanation of a C program that finds the largest number using dynamic memory allocation:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, max;
printf("Enter the number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int)); // dynamically allocate memory for n integers
if (ptr == NULL) { // check if memory allocation was successful
printf("Memory allocation failed.");
return 1; // indicate failure
}
printf("Enter %d integers:\n", n);
for (i = 0; i < n; ++i) { // loop through each integer
scanf("%d", ptr + i); // read integer and store it in the dynamically allocated memory
}
max = *ptr; // set the maximum to the first element of the array
for (i = 1; i < n; ++i) { // loop through each element of the array starting from the second element
if (*(ptr + i) > max) { // check if the current element is greater than the maximum
max = *(ptr + i); // set the maximum to the current element
}
}
printf("The largest number is %d", max);
free(ptr); // free the dynamically allocated memory
return 0; // indicate successful completion of the program
}
Let’s break it down step-by-step:
We include the necessary header files:
stdio.h
for input/output functions andstdlib.h
for memory allocation functions.We declare integer variables
n
,i
,max
, and a pointer variableptr
to int.We prompt the user to enter the number of elements they want to input using
printf()
, and read it usingscanf()
.We use
malloc()
to dynamically allocate memory forn
integers and store the address of the first integer inptr
. Themalloc()
the function takes in one argument: the number of bytes to allocate. We allocaten * sizeof(int)
bytes, which is the size of an integer multiplied by the number of integers we want to allocate.We check if
malloc()
was successful by checking ifptr
isNULL
. If it isNULL
, we print an error message usingprintf()
and return 1 to indicate failure.We prompt the user to enter
n
integers usingprintf()
, and use afor
loop to loop through each integer. Inside the loop, we usescanf()
to read in the integer and store it in the dynamically allocated memory usingptr + i
.We set
max
to the first element of the array using*ptr
.We use another
for
loop to loop through each element of the array starting from the second element. Inside the loop, we check if the current element is greater than the maximum using*(ptr + i) > max
. If it is, we setmax
it to the current element.Once the loop is complete, we print the largest number using
printf()
.We free the dynamically allocated memory using
free(ptr)
.Finally, we return
0
to indicate successful completion of the program.