Recap: Dynamic Array
Part of the Logic & Flow section of Coddy's C journey — lesson 37 of 63.
Challenge
EasyWrite a C program that creates a dynamic array based on user input, processes the data, and demonstrates proper memory management. Your program should:
- Read an integer
nfrom the user representing the number of elements to store - Use
malloc()withsizeof()to dynamically allocate memory for an array ofnintegers - Check if the memory allocation was successful - if it fails, print
Memory allocation failed!and terminate the program - If allocation succeeds, print
Array of size [n] created successfully! - Read
ninteger values from the user and store them in the allocated array - Calculate and print the sum of all elements in the format:
Sum: [value] - Find and print the minimum value in the format:
Minimum: [value] - Count how many elements are greater than the average and print in the format:
Elements above average: [count] - Print the total memory used in bytes in the format:
Memory used: [bytes] bytes - Use
free()to deallocate the memory - Print
Memory successfully freed!to confirm proper cleanup
Remember to include the <stdlib.h> header to use malloc() and free(). This challenge tests your complete understanding of dynamic memory management, from allocation through usage to proper cleanup.
For example, if the input is:
5
12 8 15 3 20Your output should be:
Array of size 5 created successfully!
Sum: 58
Minimum: 3
Elements above average: 2
Memory used: 20 bytes
Memory successfully freed!This comprehensive challenge demonstrates the complete lifecycle of dynamic memory management while performing meaningful data processing operations on the allocated array.
Try it yourself
#include <stdio.h>
#include <stdlib.h>
int main() {
// Read the number of elements
int n;
scanf("%d", &n);
// TODO: Write your code below
// 1. Allocate memory for n integers using malloc()
// 2. Check if allocation was successful
// 3. Read n integers into the array
// 4. Calculate sum, find minimum, count elements above average
// 5. Print results and free memory
return 0;
}All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions