C Program to Find Largest Number Using Dynamic Memory Allocation

To understand this example, you should have the knowledge of the following C programming topics:


Example: Find the Largest Element

#include <stdio.h>
#include <stdlib.h>

int main() {

  int n;
  double *data;
  printf("Enter the total number of elements: ");
  scanf("%d", &n);

  // Allocating memory for n elements
  data = (double *)calloc(n, sizeof(double));
  if (data == NULL) {
    printf("Error!!! memory not allocated.");
    exit(0);
  }

  // Storing numbers entered by the user.
  for (int i = 0; i < n; ++i) {
    printf("Enter number%d: ", i + 1);
    scanf("%lf", data + i);
  }

  // Finding the largest number
  for (int i = 1; i < n; ++i) {
    if (*data < *(data + i)) {
      *data = *(data + i);
    }
  }
  printf("Largest number = %.2lf", *data);

  free(data);

  return 0;
}

Output

Enter the total number of elements: 5
Enter number1: 3.4
Enter number2: 2.4
Enter number3: -5
Enter number4: 24.2
Enter number5: 6.7
Largest number = 24.20

Explanation

In the program, we have asked the user to enter the total number of elements which is stored in the variable n. Then, we have allocated memory for n number of double values.

// Allocating memory for n double values
data = (double *)calloc(n, sizeof(double));

Then, we used a for loop to take n number of data from the user.

// Storing elements
for (int i = 0; i < n; ++i) {
  printf("Enter Number%d: ", i + 1);
  scanf("%lf", data + i);
}

Finally, we used another for loop to compute the largest number.

// Computing the largest number
for (int i = 1; i < n; ++i) {
  if (*data < *(data + i))
    *data = *(data + i);
  }
}

Note: Instead of calloc(), it's also possible to solve this problem using the malloc() function.

Did you find this article helpful?