Deleting dynamic array causing an abort error

44 Views Asked by At

My code bellow is supposed to take take the average of all numbers entered in a user defined array. Everything is working fine except I need to delete nums at the end of the program. When I run the program it crashes/aborts with "heap corruption detected." If I comment out the delete statement it works and this does not happen but I need to have it in the program. Can someone explain why this is happening?

#include <iostream>
using namespace std;

int main()
{
    cout << "Hi, Let me find the Average! \n \n";
    
    int SIZE = 0;
    double *nums = new double[SIZE];
    int i = 0;
    double total = 0.0;
    double num = 0.0;
    double average = 0.0;
    cout << "Please enter how many numbers to be entered: ";
    cin >> SIZE;
    do{
        cout << "Please enter the " <<i+1<< "st/th number : ";
        cin >> num;
        nums[i] = num;
        num = 0;
        i++;
        if (i == SIZE) break;
    } while (i < SIZE);
    cout << "Here are the numbers you entered: \n";
    for (int n = 0; n < SIZE; n++) {
        cout << nums[n] << "\n";
    }
    for (int n = 0; n < SIZE; n++) {
        total += nums[n];
    }
    cout << "You chose to enter " << SIZE << " numbers. \n";
    cout << "The total of all the numbers you entered is : " << total << "\n";
    average = total / double(SIZE);
    cout << "The average of all the numbers that you added is: " << average << "\n";
    //delete[] nums; // Here is the issue
}
2

There are 2 best solutions below

2
Kate Rainey On
#include <iostream>
using namespace std;

int main()
{
    cout << "Hi, Let me find the Average! \n \n";
    
    int SIZE = 0;
    int i = 0;
    double total = 0.0;
    double num = 0.0;
    double average = 0.0;
    cout << "Please enter how many numbers to be entered: ";
    cin >> SIZE;
    double* nums = new double[SIZE];
    do{
        cout << "Please enter the " <<i+1<< "st/th number : ";
        cin >> num;
        nums[i] = num;
        num = 0;
        i++;
        if (i == SIZE) break;
    } while (i < SIZE);
    cout << "Here are the numbers you entered: \n";
    for (int n = 0; n < SIZE; n++) {
        cout << nums[n] << "\n";
    }
    for (int n = 0; n < SIZE; n++) {
        total += nums[n];
    }
    cout << "You chose to enter " << SIZE << " numbers. \n";
    cout << "The total of all the numbers you entered is : " << total << "\n";
    average = total / double(SIZE);
    cout << "The average of all the numbers that you added is: " << average << "\n";
    delete[] nums;
}
0
Thomas Matthews On

You don't need an array, you can calculate a running average.
Here's an example:

int main()
{
    double sum = 0.0;
    double number = 0.0;
    double quantity = 0.0;
    std::cout << "Enter number (floating point ok): ";
    while (std::cin >> number)
    {
        sum += number;
        quantity += 1.0;
        std::cout << "Enter number (floating point ok): ";
    }
    if (quantity > 0.0)
    {
        const double average = sum / quantity;
        std::cout << "Average: " << average << "\n";
    }
    else
    {
        std::cout << "No average, no numbers entered.\n";
    }
    return EXIT_SUCCESS;
}

The running average eliminates issues caused by using arrays.