#include <stdio.h>
#include <stdlib.h>
int main() {
int size;
// Step 1: Get the size of the array from the user
printf("Enter the size of the array: ");
scanf("%d", &size);
// Step 2: Allocate memory for the array using malloc
int arr = (int )malloc(size * sizeof(int));
// Step 3: Check if memory allocation was successful
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1; // Exit the program with an error code
}
// Step 4: Initialize the array elements (optional)
for (int i = 0; i < size; ++i) {
arr[i] = i + 1; // Example initialization, you can modify as needed
}
// Step 5: Display the elements of the array
printf("Array elements: ");
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]);
}
// Step 6: Free the allocated memory
free(arr);
return 0; // Exit the program successfully
}