Can anyone help me with this C++ problem regarding classes? * You MUST create a
ID: 3700034 • Letter: C
Question
Can anyone help me with this C++ problem regarding classes?
* You MUST create a class header file, class source file and driver program in order for this program to work!*
1) Make one class that contains an array of float number values.
- Your default constructor must be of a dynamic array consisting of 10 elements
- Your overloaded constructor needs to accept integer arguments and allocate the array dynamically so that it is able to store those numbers.
- You must include a destructor which must free the memory stored by your array.
- Also, there needs to be a set of member functions to execute performance to:
a.) Hold numbers in any element related to the array (Your declared function must take the index as an argument)
b.) Retrieve one number from any element of the array (Again, the function has to take the index as an argument)
c.) Return back the greatest value inside the array
d.) Return the smallest value inside the array
e.) Return the average of all the numbers within the array
f.) Finally, make a driver program that incorporates the class and be sure to test that every function performs correctly.
Explanation / Answer
#include <iostream>
using namespace std;
class ArrayOperation {
float* array;//array pointer
int length = 10;
public:
ArrayOperation() { //default constructor
array = 0;
}
ArrayOperation(int s) {//parameterised constructor to initialize the values
array = new float[s];
for (int i = 0; i < s; i++) {
array[i] = 0;
}
}
void add(int index) {//to add the values to the array
if (index >= length) {
cout << "Element not inserted. index is greater than 10. ";
} else {
cout << " Enter the element to inserted in " << index << " : ";
cin >> array[index];
}
}
void printArray() {//to display the elements in the array
for (int i = 0; i < length; i++) {
cout << array[i]<<" ";
}
}
float elementAt(int index){//to return the element in the given position
if (index >= length) {
cout << " Element not inserted. index is greater than 10. ";
} else {
return array[index];
}
}
float greatest(){//returns the greatest number in the array
float temp =0;
for(int i =0; i<length; i++){
if(temp<array[i]){
temp = array[i];
}
}
return temp;
}
float smallest(){//returns the smallest number in the array
float temp =array[0];
for(int i =1; i<length; i++){
if((temp>array[i])&&(array[i]!=0)){
temp = array[i];
}
}
return temp;
}
float average(){//returns the average numbers of the element
float temp =0;
for(int i =0; i<length; i++){
temp = temp+array[i];
}
return (temp/(length+1));
}
};
int main() {
int size = 10;
ArrayOperation a(size);
//assign the values to the array
a.add(0);
a.add(1);
a.add(2);
a.add(5);
a.add(6);
a.add(10);
a.add(7);
a.add(8);
a.add(9);
//print the array
a.printArray();
//retrive an element
int n;
cout<<" Enter the position of the element to be retrived :";
cin>>n;
cout<<" Element at "<<n<<" is :"<<a.elementAt(n);
//to print the greatest element
cout<<" The greatest element in the array is :"<<a.greatest();
//to print the smallest element
cout<<" The smallest element in the array is :"<<a.smallest();
//to print the average value
cout<<" The average of numbers in the array is:"<<a.average();
return 0;
}
sample run:
Enter the element to inserted in 0 : 1.5
Enter the element to inserted in 1 : 6.5
Enter the element to inserted in 2 : 7.2
Enter the element to inserted in 5 : 3
Enter the element to inserted in 6 : 4
Element not inserted. index is greater than 10.
Enter the element to inserted in 7 : 6
Enter the element to inserted in 8 : 9
Enter the element to inserted in 9 : 10.5
1.5 6.5 7.2 0 0 3 4 6 9 10.5
Enter the position of the element to be retrived :9
Element at 9 is :10.5
The greatest element in the array is :10.5
The smallest element in the array is :1.5
The average of numbers in the array is:4.33636
RUN SUCCESSFUL (total time: 44s)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.