(C++) (Fix my code, please) CODE: #include <iostream> using namespace std; //Cla
ID: 3839396 • Letter: #
Question
(C++) (Fix my code, please)
CODE:
#include <iostream>
using namespace std;
//Class definition
class NumberArray
{
//Private instance variables
private:
int arrSize;
float *numbers;
public:
//Constructor
NumberArray(int aSize)
{
int i;
//Storing size
arrSize = aSize;
//Allocating memory
numbers = new float[arrSize];
//Iterating over array
for(i=0; i<arrSize; i++)
{
//Reading and storing value
cout << " Enter value for numbers[" << i << "] : ";
cin >> numbers[i];
}
}
//Destructor
~NumberArray()
{
delete numbers;
}
//Function that stores the value at specified index
void store(int index, float value)
{
//Validating index provided and assigning value
if(index >= 0 && index < arrSize)
{
numbers[index] = value;
}
else
{
cout << " Invalid Index!!! ";
}
}
//Function that retrieve the value at specified index
float retrieve(int index)
{
//Validating index provided and returning value
if(index >= 0 && index < arrSize)
{
return numbers[index];
}
else
{
cout << " Invalid Index!!! ";
return -1;
}
}
//Function that returns highest element in the array
float highest()
{
int i;
float high;
//Initially assign first element as highest value
high = numbers[0];
//Iterating over array
for(i=0; i<arrSize; i++)
{
//Comparing for highest value
if(numbers[i] > high)
{
//Updating highest value
high = numbers[i];
}
}
//Return highest value
return high;
}
//Function that returns lowest element in the array
float lowest()
{
int i;
float low;
//Initially assign first element as lowest value
low = numbers[0];
//Iterating over array
for(i=0; i<arrSize; i++)
{
//Comparing for lowest value
if(numbers[i] < low)
{
//Updating lowest value
low = numbers[i];
}
}
//Return lowest value
return low;
}
//Function that returns average of numbers stored in array
float average()
{
int i;
float sum=0, avg;
//Iterating over array
for(i=0; i<arrSize; i++)
{
//Updating lowest value
sum = sum + numbers[i];
}
//Calculating average
avg = sum / (float)arrSize;
//Return average value
return avg;
}
};
//Main function
int main()
{
int n;
//Reading n value
cout << " Enter number of values: ";
cin >> n;
//Creating class object
NumberArray arr(n);
//Testing functions
cout << " Value at index 2: " << arr.retrieve(2);
cout << " Assigning Value 20 to index 2: ";
arr.store(2, 20);
cout << " After update Value at index 2: " << arr.retrieve(2);
cout << " Highest Value: " << arr.highest();
cout << " Lowest Value: " << arr.lowest();
cout << " Array Average: " << arr.average();
cout << " ";
return 0;
}
(ERROR CODE:)
10. Number Array Class 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 private data members of the class should include the integer argument in a variable to hold the size of the arra and a pointer to float type to hold the address of the first element in the arra 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 of the array Retrieve a number from 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 SAMPLE RUN #0 /Dynamic-Number-Class-Array o show Highlighted only Highlight: None Interactive Session Hide Invisibles Creating a Float Class Array .Object with a size of .5.Using.setI.function with calls 0,1.4), (1, 2.3), 2.3.13 3,6.66 4,7.711) to load array. Using getI with all legitmate index values o print array. The value of .fArray1[0 1.4J The value of .fArray1[1 2.3 The value of.fArray 112 3.13 The value of fArray113 6.66 The value of .fArray1[4 -7.711 Calling getHi returns highest value i array as 7.711 Calling getLow.returns alue .in. array as 1.4J Calling getAvg returns the average of the array as 4.2402Explanation / Answer
NOTE:- code is running properly and also giving the right results.Please try it in upgraded compiler like GCC 4.9.2 or more
CODE:-
#include <iostream>
using namespace std;
//Class definition
class NumberArray
{
//Private instance variables
private:
int arrSize;
float *numbers;
public:
//Constructor
NumberArray(int aSize)
{
int i;
//Storing size
arrSize = aSize;
//Allocating memory
numbers = new float[arrSize];
//Iterating over array
for(i=0; i<arrSize; i++)
{
//Reading and storing value
cout << " Enter value for numbers[" << i << "] : ";
cin >> numbers[i];
}
}
//Destructor
~NumberArray()
{
delete numbers;
}
//Function that stores the value at specified index
void store(int index, float value)
{
//Validating index provided and assigning value
if(index >= 0 && index < arrSize)
{
numbers[index] = value;
}
else
{
cout << " Invalid Index!!! ";
}
}
//Function that retrieve the value at specified index
float retrieve(int index)
{
//Validating index provided and returning value
if(index >= 0 && index < arrSize)
{
return numbers[index];
}
else
{
cout << " Invalid Index!!! ";
return -1;
}
}
//Function that returns highest element in the array
float highest()
{
int i;
float high;
//Initially assign first element as highest value
high = numbers[0];
//Iterating over array
for(i=0; i<arrSize; i++)
{
//Comparing for highest value
if(numbers[i] > high)
{
//Updating highest value
high = numbers[i];
}
}
//Return highest value
return high;
}
//Function that returns lowest element in the array
float lowest()
{
int i;
float low;
//Initially assign first element as lowest value
low = numbers[0];
//Iterating over array
for(i=0; i<arrSize; i++)
{
//Comparing for lowest value
if(numbers[i] < low)
{
//Updating lowest value
low = numbers[i];
}
}
//Return lowest value
return low;
}
//Function that returns average of numbers stored in array
float average()
{
int i;
float sum=0, avg;
//Iterating over array
for(i=0; i<arrSize; i++)
{
//Updating lowest value
sum = sum + numbers[i];
}
//Calculating average
avg = sum / (float)arrSize;
//Return average value
return avg;
}
};
//Main function
int main()
{
int n;
//Reading n value
cout << " Enter number of values: ";
cin >> n;
//Creating class object
NumberArray arr(n);
//Testing functions
cout << " Value at index 2: " << arr.retrieve(2);
cout << " Assigning Value 20 to index 2: ";
arr.store(2, 20);
cout << " After update Value at index 2: " << arr.retrieve(2);
cout << " Highest Value: " << arr.highest();
cout << " Lowest Value: " << arr.lowest();
cout << " Array Average: " << arr.average();
cout << " ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.