0
Hi, anyone like to offer some extra eyes on this problem? I am having trouble with the array pointer and with the variables. I don't seem to have the pointer set up because the additional times the array is called it is empty. Also, if I don't use integers the program drops through.

Any ideas where I am going wrong?

Thanks you guys.

Question is in the code

[code]

/*
NumClass
Main.cpp

*******************************************************************************************
*         NumClass
*
*  Design a class that has an array of floating point numbers. The constructor
*  should accept an integer argument and dynamically allocate the array to hold that many
*  numbers. The destructor should free the memory held by the array.  In addition, there
*  should be member functions to perform the following operations:
*
*       *   Store a number in any element in of the array
*       *   Retrieve a number in any element of the array
*       *   Return the highest value stored in the array
*       *   Return the lowest value stored in the array
*       *   Return the average of all the numbers stored in the array
*
*  Demonstrate the class in a program
*
******************************************************************************************/


#include "NumClass.h"       // For the NumClass class
#include <iostream>
using namespace std;

// Main to run program
int main()
{
    //variables
    int a_size = 10;            // initialize
    NumClass myArray(a_size);   // name for instance of array
    int element;                // to pick an element from the array
    int size;


    size=a_size;
    // Get the array size.
    cout << "Enter the arrays's size: ";
    cin >> size;
    cout << endl;
    myArray.theArray[size];

    // ask for the numbers in the array
    for (int index =0; index < size; index++)
    {
        cout << "What is the number that is in " << index << " place of the array? \n";
        cin >> myArray.theArray[index];
    }

    // store the  in the instance of digits
    myArray.theArray[size];

    cout << "Pick an element in the array. \n";
    cin >> element;

    // Display the Array's data.
    cout << "\t Array Information \n";
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    cout << "\t Number from element " << element << " :" << myArray.theArray[element] << endl;
    cout << "\t Highest :      " << myArray.getHighest() << endl;
    cout << "\t Lowest:     " << myArray.getLowest() << endl;
    cout << "\t Average: " << myArray.getAverage() << endl;
    cout << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    cout << endl;

}



/*
 NumClass.cpp
 */



// Implementation file for the NumClass class
# include "NumClass.h"
#include <cstdlib>
#include <iostream>
using namespace std;

// The default constructor
NumClass::NumClass(int size)
{
    theArray = new int[size];       // points to a dynamically allocated array of integers
    numElements = size;             // number of elements in the array

    for (int index = 0; index < size; index++)
        theArray[index] = 0;        // set everyting to zero to start
}

NumClass::~NumClass()
{
    //destructor
    delete[]theArray;  //free memory
    theArray = 0;    //clear to prevent invalid memory address
}


// Accessor function for the highest number
// use const to tell complier not to change it
// only return value
double NumClass::getHighest() const
{
    int count;    // local to count in loop
    double highest;  // to hold highest

    // set the first array element to highest
    highest = theArray[0];

    // step through array size to compare
    for (count =1; count < numElements; count++)
    {
        if (theArray[count] > highest)
        {
            // stores the highest number
            highest = theArray[count];
        }
    }
    return  highest;
}

// Accessor function for the lowest
// use const to tell complier not to change it
// only return value
double NumClass::getLowest() const
{
    int count;    // local to count in loop
    double lowest;  // to hold lowest

    // set the first array element to lowest
    lowest = theArray[0];

    // step through array size to compare
    for (count = 1; count < numElements; count++)
    {
        if (theArray[count] < lowest)
        {
            // stores the lowest number
            lowest = theArray[count];
        }
    }
    return  lowest;
}

// Accessor function for the average
// use const to tell complier not to change it
// only return value
double NumClass::getAverage() const
{
    double total = 0;   // accumulator for function
    int count;          // local to count in loop
    double average;     // to hold average

    // step through array size to add up numbers
    for (count = 0; count < numElements; count++)
    {
        total =+ theArray[count];
    }
    average = (total/count);
    return average;

}

/*
 NumClass.h
 */

// Specification file for the NumClass class
#ifndef NumClass_H      // include guard to keep from being run twice
#define NumClass_H      // defines class


class NumClass      // class name
{
public:
    // private member variables
    // Dynamically allocate size of array
    int *theArray;  // pointer to the array
    int numElements;    // number of elements



    NumClass(int);      // constructor
    ~NumClass();    // destructor

    // Mutators used to alter variables
    void setHighest(double);        // set the highest value
    void setLowest(double);     // set the lowest value
    void setAverage(double);        // set the average value

    // Accessors used to get the variables
    //set as constant to avoid making changes to variable
    //int getElement() const;
    double getHighest() const;
    double getLowest() const;
    double getAverage() const;

};
#endif      // closes guard
[/code]

Edited by chubbyy.putto

3
Contributors
2
Replies
23
Views
3 Years
Discussion Span
Last Post by wrathness
0

Well for a start you have there an array of int not float/double.

In main you have this statement a number of times.
myArray.theArray[size];
Think what exactly you expect to happen here?

I believe nothing is happening, It's just saying "hey look at me, I'm an array index of a class member array, it's neither setting nor getting anything.

It might be that you want to call your "mutator" as you call them, member functions.

Sorry I don't have time to help modify, just providing something to think about in lieu of more time.

0

You should move the initialization of your NumClass to line 53 because it is being initialized with a default size of 10, so then what ever size the user enters isnt being created and can cause the array to go out of bounds.

This line "myArray.theArray[size];", is just trying to reference an element in the array, its not doing anything else.

In your average function block you have got "=+" instead of += other than that it all works fine.

Here is the working code:

#include "NumClass.h" // For the NumClass class
#include <iostream>
using namespace std;

// Main to run program
int main()
{

//variables
NumClass * myArray; //create a pointer to the numberClass
int element; // to pick an element from the array
int size;

// Get the array size.
cout << "Enter the arrays's size: ";
cin >> size;
cout << endl;

myArray = new NumClass(size); //call the class constructor initializing a new array with the user defined size

// ask for the numbers in the array
for (int index =0; index < size; index++)
{
cout << "What is the number that is in " << index << " place of the array? \n";
cin >> myArray->theArray[index]; //assign the values to the array indexes
}
// store the in the instance of digits

cout << "Pick an element in the array. \n";
cin >> element;
// Display the Array's data.
cout << "\t Array Information \n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "\t Number from element " << element << " :" << myArray->theArray[element] << endl;
cout << "\t Highest : " << myArray->getHighest() << endl;
cout << "\t Lowest: " << myArray->getLowest() << endl;
cout << "\t Average: " << myArray->getAverage() << endl;
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << endl;

delete myArray;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.