Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m in need of designing an algorithm for this problem involving classes. Pleas

ID: 3663430 • Letter: I

Question

I'm in need of designing an algorithm for this problem involving classes. Please help me! (C++ Preferred)

Here is a description of the class that you must design:

The class must have a method that allows a user to enter a sequence of floating-point numbers, one value at a time. Since the user can call this method any number of times, the length of the sequence of floating-point values that the user will enter is unknown. This data-entry method must take an argument of type double, which represents a value in the sequence, and adds this value to the data stored in the class.

After the class has received some sequence of values, its job is to supply the user with the following statistics associated with that sequence:

-Find : the number of values in the sequence

-Find the value of the last number in the sequence

-Find the sum of all of the values in the sequence:

-Find the arithmetic mean of all of the values in the sequence. The arithmetic mean is given by:

-Find the minimum (smallest) value in the sequence

-Find the maximum (largest) value in the sequence

-Clear the sequence so that statistics can be calculated on a new sequence

-Each of these must be implemented in the class as a separate method that will return (supply) that statistic to the user.

As you specify this class, be sure that you:

-Give the class and the class's methods appropriate names (notice that I have not told you what to call anything)
-Give the class a default constructor that initializes all data members to appropriate values
-Give the class's methods appropriate operation contracts (more on this below)
-Note that the method that returns the length of the sequence can be called on an object that has no values in the sequence. The length of an empty sequence is zero. The same holds for the sum of the values in the sequence—the sum of an empty sequence is zero. Also, an empty sequence can be cleared of all of its values—nothing changes.

However, all other methods of the class cannot be called on empty sequences. These operations should state that they cannot be called on empty sequences in their operation contracts, and they should protect against being executed if they are called on an empty sequence. Think about how you might do this.

Explanation / Answer

#include <cstdlib>
#include <iostream>

using namespace std;
// class declaration
class operations
{
// these are data members
public:
int length_of_sequence;
double value;
double *a;
int count;
// default constructorto initialize data variables
operations()
{
   length_of_sequence=0;
    value=0;
    count=0;       

}

// data entry function which first needs no of values user want to store and then ask for sequence of values
void data_entry(double value)
{
cout<<"Enter number of values you want to stored ";
cin>>length_of_sequence;

a = new double[length_of_sequence];   // this is dynamic array
int i;
for(i=0;i<length_of_sequence;i++)  
     {
      cout<<"Enter value to be stored ";
      cin>>value;
      a[i]=value;                               
     ++count; // It is used to store no of values user enter
     }


}   
// Will gives length of sequence
int no_of_values()
{
   return count;
  
}

//Will give last element of sequence
double lastElement()
{
     
return a[count-1];

}
// Will give total of sequence
double sum()
{
int i;
double total=0;
   
for(i=0;i<count;i++)
{
total=total+a[i];


}  
return total;     
}
// Will give arthmetic mean of sequence
double mean(double tot)
{

return tot/count;


}
// Will give minimum of sequence
double minimum()
{
double min=a[0];
int i;

   
for(i=0;i<count;i++)
{
if(a[i]<=min)
min=a[i];

}        
     
return min;
}


// Will give maximum of sequence
double maximum()
{
double max=a[0];
int i;

   
for(i=0;i<count;i++)
{
if(a[i]>=max)
max=a[i];

}        
     
return max;
}

};

//I have commented the main function because you ask for Algorithm. So if you nedd you can remove comments.

/* --------------------------you can delete this line if you want to use main function------------------------------
// Main function for execution.
int main(int argc, char *argv[])
{
// Object creation. It will automatically call default constructor
    operations ob;
// calling of methods begin
    ob.data_entry(ob.value);
  
    cout<<"The number of elements are "<<ob.no_of_values()<<endl;
    double tot=ob.sum();
    cout<<"The sum of sequence is "<<tot<<endl;
// this condition will check whether length of sequence is 0 or not. It will not call below mentioned function if length is 0.
if(ob.count!=0)
    {
      cout<<"The last element of sequence is "<<ob.lastElement()<<endl;
        cout<<"The arthmetic mean of sequence is "<<ob.mean(tot)<<endl;
        cout<<"The minimum of sequence is "<<ob.minimum()<<endl;
          cout<<"The maxmum of sequence is "<<ob.maximum()<<endl;
    }
// It will clear the sequence.  
    free(ob.a);
    system("PAUSE");
    return EXIT_SUCCESS;
}


/* --------------------------you can delete this line if you want to use main function------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote