When you’re working with lists of numbers in C, dynamic memory allocation is super handy. It lets you grab just the right amount of memory you need when your program runs. Let’s check out how to use dynamic memory allocation to find the biggest number in a list of integers. Say you have 5 numbers: 10, 25, 7, 15, and 30. Our program will show that 30 is the biggest.
Prerequisites for the C Program to Find Largest Number
Before diving into the code, you'll need to know a few things.
- How to use
mallocfor dynamic memory allocation - Basic understanding of pointers and arrays in C
- Using loops for iteration
- Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Find Largest Number Using Dynamic Memory Allocation
Below is the complete C program using dynamic memory allocation to find the largest number in an array.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, largest;
int *arr;
// Read the number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Memory allocation for n elements
arr = (int *)malloc(n * sizeof(int));
// Read n elements
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Finding the largest number
largest = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > largest)
largest = arr[i];
}
printf("The largest number is %d\n", largest);
// Freeing the allocated memory
free(arr);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Find Largest Number Program
Here's what to expect when you run the program. It asks for the total count, then each number, and finally prints the largest number.
Enter the number of elements: 5
Enter element 1: 10
Enter element 2: 25
Enter element 3: 7
Enter element 4: 15
Enter element 5: 30
The largest number is 30
Algorithm to Find the Largest Number in C
In this program, dynamic memory allocation helps manage the size of input at runtime. Here’s the detailed step-by-step approach we follow.
- Read the number of elements
nfrom the user. - Allocate memory for
nintegers usingmalloc. - Input all
nnumbers into the allocated array. - Assign the first element of the array to
largest. - Loop through the array and compare each element with
largest. - If an element is greater than
largest, updatelargest. - After the loop, print
largestas the result. - Free the allocated memory.
flowchart TD
A([Start]) --> B[/Enter the number of elements/]
B --> C["Allocate memory dynamically for n elements"]
C --> D[/Enter n elements/]
D --> E[largest = arr[0]]
E --> F["i = 1"]
F --> G{"i < n?"}
G -->|Yes| H{"arr[i] > largest?"}
H -->|Yes| I[largest = arr[i]]
H -->|No| J
I --> J["i++"]
J --> G
G -->|No| K([End])How the C Program to Find Largest Number Works
Let's break down the code to understand its working better. Each key part is explained below.
- The
#include <stdlib.h>is crucial for dynamic memory functions likemalloc. int *arrdeclares a pointer to store the address of dynamically allocated memory.scanf("%d", &n)reads the number of integers.- The line
arr = (int *)malloc(n * sizeofallocates memory for) nintegers. - A loop with
for (i = 0; i < n; i++)takes user input to fill the array. - Initially, the first array element is stored in
largest. - Another loop checks if any array element is greater than
largestand updates if needed. free(arr)is used to release the allocated memory once its job is done.
Quick Quiz — test your understanding
Tap an option to check your answerWhat function is used for dynamic memory allocation in C?
Time and Space Complexity of the C Program
Understanding the efficiency of the program is crucial, especially when working with dynamic memory.
Time Complexity: O(n). The loop iterates through the array once, with for (i = 0; i < n; i++) handling each element. If you input 10 elements, there are exactly 10 comparisons — 100 elements would give 100 comparisons.
Space Complexity: O(n). The space is required for storing n integers in the array arr. The variables n, i, and largest all are simple integer types.
Common mistake:Forgetting to free allocated memory with free can lead to memory leaks.
Key takeaway:Dynamic memory allocation is key for handling variable input sizes in C, but always manage the memory with care!
FAQs About Finding Largest Number Using Dynamic Memory Allocation
Here are some common questions students have about this program.
free, it can lead to memory leaks, which waste resources.calloc initializes the allocated memory to zero, unlike malloc.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Multiply Two Matrices Using Multi-dimensional Arrays
- C Program to Multiply Two Matrices Using Functions
- C Program to Add Two Matrices Using Multi-dimensional Arrays
- C Program to Find Transpose of a Matrix with Example
- C Program to Find Largest Element in an Array
Conclusion
Dynamic memory allocation is a powerful way to handle different data sizes in C. You gotta avoid common mistakes like memory leaks by freeing your pointers after you're done. Once you get the hang of this, try writing a program that sorts data using similar ideas. Managing memory well now will save you time and headaches later.
Test your C skills more with our C language quiz, or practice in the online compiler.