Put malloc, realloc, and free to use! You'll read numbers one by one and grow your array dynamically, then report the sum and average.
YOUR TASK
Write a C program that:
Reads an integer n (how many numbers to store)
Uses malloc to allocate an array of n integers
Reads n integers into the array
Prints the sum and average (to 2 decimal places), then frees memory
Need a hint?
Step-by-step:
1. #include <stdlib.h> for malloc/free
2. Read n with scanf("%d", &n);
3. Allocate: int *arr = malloc(n * sizeof(int));
4. Loop and fill: scanf("%d", &arr[i]);
5. Sum in a loop, average = sum / (float)n
6. Print: printf("Sum: %d\nAverage: %.2f\n", sum, avg);
7. Always free(arr); at the end
Example (input: 3, then 10 20 30):
Sum: 60
Average: 20.00
Code Editor
Ready
Output
Console
Console initialized...
Ready for Project 3: Dynamic Number List
> Test your code with the "Check My Code" button
> When it passes, the Complete button will unlock!