ARRAY OF OBJECTS

Hello everyone,please i need help with this question.I have already written my code, its compiling but the answers are totally wrong, i dont know what the problem is. Here it is

Question:
Write down a statistics class which will have the following members;
• Real array with 90 elements;
• Real max, min and av,
• Integer arraysize
And the following member functions:
• Constructor ()
Will receive arraysize as a parameter with default value of 40
• Calculates the average of the array elements and saves in av
• Find and locates the min and max items in the array
• Destructor
And here is my own 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
#include <iostream>
using namespace std;

class statistics
{
private:
	double max, min, av;
	int arraysize,e;
public:
	statistics(){arraysize=4;e=1;}
	statistics(double,double,double,int);
	double average(double []);
	double range(double []);
	void display();
	void getelems(double elems){e=elems;}
	~statistics();
};

const int ARRAY_SIZE=10;
statistics::statistics(double mx,double mn, double ave,int arrs)
{
	max = mx;
	min = mn;
	av = ave;
	arraysize = arrs;
}

double statistics :: average(double array[])
{
	double total=0;
	for (int i=0;i<ARRAY_SIZE;i++)
	{
		total +=array[i];
	}
	av = total/i;
	return av;
	
}

double statistics :: range (double array[])
{
	max = array[0];
	int count;
	for( count=0;count<ARRAY_SIZE;count++)
	{
		if (array[count]>max)
			max = array[count];
	}
	return max;
	min = array[0];
	for ( count=0; count<ARRAY_SIZE;count++)
	{
		if (array[count]<min)
			min = array[count];
		return min;
	}
}
void statistics :: display()
{
	cout<<"The maximum element is "<<max;
	cout<<"And the minimum element is "<<min;
}
statistics::~statistics()
{
	cout<<"destroyed";
}
int main()
{
	int index;
	statistics an[ARRAY_SIZE];
	double elems[ARRAY_SIZE];

	cout<<"Enter the elements";
	
	for(index=0;index<ARRAY_SIZE;index++)
	{	
		double elems;
		cin>>elems;
		an[index].getelems(elems);

	}	
	cout<<"Their average is"<<an[index].average(elems);
	;

	cout<<"The range is"<<an[index].range(elems);
	
	an[index].display();

	return 0;
}

ANY HELP OF ANY KIND WILL BE SINCERELY APPRECIATED
For me, your code doesn't compile. You need to declare i outside of the for loop at line 31.

After that... you need an array in statistics, with 90 elements, an array of preferably doubles (maybe publicly accessible?). After that, you don't need an array of statistics objects, and you won't need your elems in main(). You'd just have to do an.array[i] for your read in.

Also, keep arraysize. If you modify it at the start if the program and your loops run depending on its size, then you can keep a 90 element array but have it also "act" like a 20 element array.

-Albatross
Last edited on
thanks
Topic archived. No new replies allowed.