Design a class that has an array of floating-point numbers. Assignment should ha
ID: 3708414 • Letter: D
Question
Design a class that has an array of floating-point numbers. Assignment should have seperate header file, class cpp file and driver file.
The default constructor should create a dynamically allocated array of 10 elements.
The overloaded 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 function to perform the following operations:
Store a number in an y element of the array. The function should take the index as an argument Inside the function check to make sure the index is not outside the size of the array. Retrieve a number from any element of the array.
The function should take the index as an argument. Inside the function check to make sure the index is not outside the size 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. Create a driver program that uses the class and test that each function works properly.
Explanation / Answer
#include <iostream>
using namespace std;
int n; //variable declaration
float arr[1];
class numop
{
public:
numop() //non parameterised contructor
{
n=10; //setting array size to 10
}
numop(int size) //parameterised contructor
{
n=size; //setting array size with given value
}
void insertnum(int pos)
{
float num;
cout<<"Enter a number you want to enter :";
cin>>num; //asking the number
cout<<endl;
arr[pos]=num; //inserting at particular position
cout<<"Number inserted"<<endl;
}
void partnum(int pos)
{ //Displaying number from a particular postion
cout<<"The Number is : "<<arr[pos]<<endl;
}
float highest() //Calculating the maximum
{
int i;
float max;
for(i=0;i<n;i++)
{
if(arr[i]>max)
{
max=arr[i]; //storing the maximum
}
}
return max; //returning the value
}
float lowest() //Calculating the minimum
{
int i;
float min;
min=arr[0];
for(i=0;i<n;i++)
{
if(arr[i]<min)
{
min=arr[i]; //storing the minimum
}
}
return min; //returning the value
}
float average() //Calculating the average
{
int i;
float total,avg;
for(i=0;i<n;i++)
{
total=total+arr[i];
}
avg=total/n;
return avg; //returning the value
}
~numop(); //destructor
};
int main()
{
int si;
cout<<"Enter the size of the array :"<<endl;
cin>>si;
numop obj(si); //object of the class
int ch,ind,in; //variable declaration
float ma,mi,av;
do
{
cout << "1.Insert num at particular position"<<endl;
cout << "2.Display num from particular position"<<endl;
cout << "3.Calculate the maximum"<<endl;
cout << "4.Calculate the minimum"<<endl;
cout << "5.Calculate the average"<<endl;
cout << "6.Exit"<<endl;
cout << "Enter your choice"<<endl;
cin>>ch; //Accepting choice from user
if(ch==1)
{
cout<<"Enter the position :"<<endl;
cin>>ind;
obj.insertnum(ind); //calling the insertnum function
}
else if(ch==2)
{
cout<<"Enter the position :"<<endl;
cin>>in;
obj.partnum(in); //calling the partnum function
}
else if(ch==3)
{
ma=obj.highest(); //calling The highest function
cout<<"The maximum is : "<<ma<<endl;
}
else if(ch==4)
{
mi=obj.lowest(); //calling The lowest function
cout<<"The minimum is : "<<mi<<endl;
}
else if(ch==5)
{
av=obj.average(); //calling The average function
cout<<"The average is : "<<av<<endl;
}
else
exit(0);
}while(ch!=6);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.