Hello, I was wondering what a second pair of eyes (or several) might have for opinions on this code? This program is supposed to store a user generated array, and functions which tell the highest, lowest, etc.
Mainly, I need help with the "set cell" part, which is supposed to allow the user to access any cell in the array

Thanks very much

#include <iostream>
#include "NumberArray.h"

using namespace std;


NumberArray::NumberArray(int s)
{
    arrayPointer = new double [s];  // constructor
    size = s;
}


NumberArray::~NumberArray()
{
       delete [] arrayPointer;
       array=NULL;

     if(arrayPointer!=NULL)                   //destructor
    {
        cout << "inside destructor, just checking" << endl;
        system ("pause");
     }


NumberArray::void setCell(int i, double)    
{    
     return arrayPointer[i];
}

NumberArray::int getCell(int i)
{
    int i= arrayPointer[0];
    for (int i = 0; i < elem; i++)
    {
       i=arrayPointer[count];
    }
    return arrayPointer[count];
}

NumberArray::highest()
{
    double highest;
    highest = arrayPointer[0];
    for (int count = 1; count < elem; count++)
    {
        if (array[count] > highest)
            highest = arrayPointer[count];
    }
}

NumberArray::lowest()
{
    double lowest;
    lowest = arrayPointer[0];
    for (int count = 1; count < elem; count++)
    {
        if (arrayPointer[count] < lowest)
            lowest = arrayPointer[count];
    }
}

NumberArray::double average()
{
    float total=0;
    float average;
    for (int count = 0; count < elem; count++)
    {
        total += arrayPointer[count];
    }

    average = total/elem;
}

Dani AI

Generated

The posted NumberArray code contains several compile-time and logic problems that explain the strange behavior and failures seen in replies by . Many function definitions use the wrong syntax, identifiers are inconsistent (array vs arrayPointer, elem vs size, count vs i), several functions never return a value, and the destructor manipulates a name that doesn't exist and checks the pointer after deletion. 's remark about the correct member-function form is on target — start by fixing signatures in the header so implementations match.

A safer, minimal implementation pattern for accessors and mutators uses bounds checks and proper return types; mark non-modifying methods const and use size_t for indices:

void NumberArray::setCell(size_t index, double value) {
    if (index >= size) throw std::out_of_range("index");
    arrayPointer[index] = value;
}

double NumberArray::getCell(size_t index) const {
    if (index >= size) throw std::out_of_range("index");
    return arrayPointer[index];
}

Aggregate operations must return a value and handle empty arrays:

double NumberArray::highest() const {
    if (size == 0) throw std::runtime_error("empty array");
    double max = arrayPointer[0];
    for (size_t i = 1; i < size; ++i) if (arrayPointer[i] > max) max = arrayPointer[i];
    return max;
}

double NumberArray::average() const {
    if (size == 0) return 0.0;
    double sum = 0.0;
    for (size_t i = 0; i < size; ++i) sum += arrayPointer[i];
    return sum / size;
}

Memory-management notes: remove system("pause") from destructors, set the pointer to nullptr if desired, and either implement copy constructor/assignment (Rule of Three/Five) or—preferably—replace the raw array with std::vector<double> (RAII). Finally, ensure header prototypes exactly match implementations, compile with warnings enabled (e.g., -Wall), and test boundary cases (first, last, out-of-range).

Does this code actually compile? Your functions should like this:

returnType ClassName::FunctionName (Paramaters)
{
    // function code goes here
}

As far as your set cell function is concerned you could do it like

void NumberArray::setCell(int index, double value)
{
    arrayPointer[i] = value;
}

You should also make sure that you are accessing a valid location in the array but I'll leave that for you to code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.