pointer and type failure with array in a class

Hello, I am new to this forum. Please help me out here thankz.

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

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

/*
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 
Bump?
 
myArray.theArray[size];

What are you trying to do here? This access the value at position size in the array but you don't do anything with it so it's a bit pointless.
and why are you doing this:
 
NumClass myArray(a_size);	// name for instance of array 

before you've asked the user how big they want the array?

The whole point of your class is to keep things hidden/encapsulated away from the user, so why have you declared you internal array to be public? You've even added a comment to say they'll be private..
Last edited on
Topic archived. No new replies allowed.