Define and implement the class array.

PLEASE I NEED SOME HELP WITH THIS. THANKS :)


Define and implement the class array representing an array of real numbers with the following public methods:
-the constructor with the size of the array as a parameter,
-the destructor,
-size-returning the current size of the array from the user,
-read-reading the contents of the array from the user,
-print-printing the contents of the array,
-avg- returning the arithmetic mean of the values in the array,
-min- returning the smallest values in the array,
-max- returning the smallest values in the array,
reverse- reversing the order of the values in the array,
sort- returning a sorted copy of the array of the sorting
order depending on the value of the boolean parameter(true means the ascending orde).

This is not a homework site. We won't do your homework for you. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again. As it is impossible to find derivative of function without knowledge in arithmetic, you cannot do more complex tasks in programming without clear understanding of basics
Last edited on
ok thanks man, i am already trying to make an attempt.
Show us what you have and tell us where you're stuck.

Please use code tags when posting your code.
http://www.cplusplus.com/articles/jEywvCM9/
#include <iostream>
#include "ExamArray.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
This is the main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
[code]int main(int argc, char** argv) {
	//int size;
	int classSize;
	//cout<<"Please specify the size of the array"<<endl;
	//cin>>size;
	
	ExamArray array1(6);
	classSize = array1.getSize();
	
	cout <<"The elements of your array are: "<<endl;
	array1.readContent(6);
//	array1.displayContent(6);
	return 0;
[/code]

This is exam array.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[code]#include <iostream>
using namespace std;

class ExamArray {
		int arraySize;
		ExamArray();
	public:
		ExamArray(int arraysize);
		//int ExamArray[arraySize];
		void setSize (int ssize);
		int getSize();
		void readContent (int ssize);
		void displayContent (int ssize);
		
};

This is the Examarray.cpp.

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
#include <iostream>
#include "ExamArray.h"

using namespace std;

ExamArray::ExamArray(int _arraysize) {
	arraySize = _arraysize;
	int array[arraySize];
}

ExamArray::ExamArray() {
	arraySize = 0;
}

void ExamArray::setSize(int ssize){
	arraySize = ssize;
}

int ExamArray::getSize(){
	return arraySize;
}

void ExamArray::readContent(int ssize) {
	arraySize = ssize;
	int array[arraySize];
	
	for (int i = 0; i < arraySize; i++) {
		cin >>array[i];
	}
	cout<<"Your array is "<<endl;
	for (int i = 0; i < arraySize; i++) {
		cout << "ExamArray [" <<i<<"]: "<<array[i]<<endl;
	}
}

void ExamArray::displayContent(int ssize) {
//	arraySize = ssize;
//	int array[arraySize];
	
//	for (int i = 0; i < arraySize; i++) {
//		cin >>array[i];
//	}
//	
//	for (int i = 0; i < arraySize; i++) {
//		cout << "ExamArray [" <<i<<"]: "<<array[i]<<endl;
//	}
}

[/code]
i Have problem with displaying the contents of the array in the function void ExamArray.
Last edited on
I asked you to please use code tags. Please see the link I provided above and edit your post.
I will not respond further until you apply code tags.

What I will tell you at this point is that you've made array a local variable within readContent and your constructor. As such, the content of the array goes out of scope when those functions exit. array should be a member variable of your class. The catch is that you can't declare a member variable (array) with a variable size. You have a couple of choices. Make array bigger that any possible input value, or dynamically allocate array.
Last edited on
i have used the code tags now.
please any assitance , still stuck with displaying the contents of the array in the function void ExamArray.
And i don't know how to go about with:
Define and implement the class array representing an array of real numbers with the public methods.

-the constructor with the size of the array as a parameter,
-the destructor

THANKS.
Thanks for adding the code tags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//  examarray.h
class ExamArray 
{   int arraySize;
    int * array;    // dynamically allocated
    
    ExamArray();
public:
    ExamArray (int arraysize);
    ~ExamArray ();  //  We need a destructor
    void setSize (int ssize);
    int getSize();
    void readContent ();      // Don't need size as an argument
    void displayContent ();  // Don't need size as an argument
};


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
//  examarray.cpp
//  Dynamically allocate the array for the specified size
ExamArray::ExamArray(int _arraysize) 
{   arraySize = _arraysize;
    array = new int[arraySize];
}

//  Destructor to release the dynamic memory
ExamArray::~ExamArray ()
{   delete [] array;
}

ExamArray::ExamArray() 
{   arraySize = 0;
    array = NULL;
}

void ExamArray::setSize(int ssize)
{   arraySize = ssize;
    delete [] array;        //  Delete the old array
    array = new int[ssize]; //  Reallocate the array
}

int ExamArray::getSize()
{   return arraySize;
}

void ExamArray::readContent () 
{   for (int i = 0; i < arraySize; i++) 
    {   cin >>array[i];
    }
}

void ExamArray::displayContent() 
{   for (int i = 0; i < arraySize; i++) 
    {   cout << "ExamArray [" << i <<"]: " < <array[i] << endl;
    }
}


THANK VERY MUCH AbstractionAnon.

i have done all, but i still have some problem with:
sort- returning a sorted copy of the array of the sorting
order depending on the value of the boolean parameter(true means the ascending order).
Topic archived. No new replies allowed.