Class Array

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
*

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 #ifndef T_H		// include guard to keep from being run twice
#define T_H		// defines class


class T		// class name
{
public:
    // private member variables
    // Dynamically allocate size of array
    int *theArray;	// pointer to the array
    int numElements;	// number of elements
    
	
    
    T(int);		// constructor
    ~T();	// 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
double T::getHighest() const
{
	int count;
	double 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 T::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;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include "T.h"

main()
{
	//variables
	int a_size = 10;			// initialize
	Q242 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;
    
}


Body code

1
2
3
4
5
6
7
8
9
::T(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
}

1
2
3
4
5
6
T::~T()
{
	//destructor
	delete[]theArray;  //free memory
	theArray = 0;	 //clear to prevent invalid memory address
}
Last edited on
T is a horrible name for a class. It is not descriptive at all, and it conflicts with the conventional use of T as the name of a template parameter.

What is Q242?

I can't tell what code I am supposed to be looking at.
Here is question:

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
*
Here is problem i have:

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.

Q242 is T idk why xcode change it
I have to create a class head file and body file (.cpp)
Chubby wrote:
27
28
	// store the  in the instance of digits
	myArray.theArray[size];
???

Do you know how arrays work?
I am confuse. What do you mean by that?
I have kinda problem with calling the class member but not with the array.
oops i forget to delete it.
Topic archived. No new replies allowed.