Menu
Coddy logo textTech

Pointer and Dynamic Array

Part of the Logic & Flow section of Coddy's C++ journey — lesson 56 of 56.

challenge icon

Challenge

Easy

Create a program that demonstrates dynamic memory management by building a flexible number storage and processing system. This challenge will test your understanding of dynamic arrays, memory allocation, array manipulation, and proper memory cleanup.

The following inputs will be provided:

  • An integer size representing the number of integers to store
  • Then size integers representing the numbers to be stored in the dynamic array

Your program should:

  1. Read the size from input
  2. Dynamically allocate an array of integers using new[] with the specified size
  3. Read the integers from input and store them in the dynamically allocated array
  4. Calculate the sum of all numbers in the array
  5. Find the maximum value in the array
  6. Print the results in the specified format
  7. Properly deallocate the memory using delete[]

Use the following exact output format:

Array size: [size]
Sum: [sum_of_all_numbers]
Maximum: [maximum_value]

Remember that dynamic memory allocation allows you to create arrays whose size is determined at runtime based on user input. Use new int[size] to allocate the array and access elements using array notation array[index]. After you're finished using the array, you must call delete[] array to free the allocated memory and prevent memory leaks. The array exists on the heap and will persist until you explicitly deallocate it.

Try it yourself

#include <iostream>
using namespace std;

int main() {
    // Read the size of the array
    int size;
    cin >> size;
    
    // TODO: Write your code here
    // 1. Dynamically allocate an array using new[]
    // 2. Read the integers and store them in the array
    // 3. Calculate the sum of all numbers
    // 4. Find the maximum value
    // 5. Don't forget to deallocate memory using delete[]
    
    // Output the results (replace with actual variables)
    cout << "Array size: " << size << endl;
    cout << "Sum: " << /* sum variable */ << endl;
    cout << "Maximum: " << /* max variable */ << endl;
    
    return 0;
}

All lessons in Logic & Flow