Question

In: Computer Science

Complete the task below C++ This header file declares the Comp315Array Class **************************/ //Include the libraries...

Complete the task below C++

This header file declares the Comp315Array Class

**************************/

//Include the libraries
#include <iostream> //Allow the use of cin and cout
//Declare namespace std for using cin and cout
using namespace std;

//Class declaration
class Comp315Array{
        
        //Public members
        public:
                /****************
                Default Constructor
                Task: assign 0 to the array size 
                *********/
                Comp315Array();

                /****************
                        Constructor
                        Task: Define an int array of size n 
                *********/
                Comp315Array(int n);
                
                /*****************
                        getSize method
                        Task: return the array size
                ******************/
                int getSize();
                
                /*****************
                        setValue method
                        Task: set array value in position p
                ******************/
                void setValue(int p, int val);
                
                /*****
                        sum method
                        Task: compute the sum of the n elements in the array
                        params:
                        @returns
                                sum of elements
                *******/
                int sum();
                
                /*****
                        sum method
                        Task: compute the sum of the elements in the array from position i to position j
                        @returns
                                sum of elements from position i to position j
                *******/
                int sum(int i, int j);
                
                /*****
                        greatest method
                        Task: returns the greatest element in the array
                        @returns
                                value of the greatest element in the array
                *******/
                int greatest();
                
                /*****
                        lowest method
                        Task: returns the lowest element in the array
                        @returns
                                value of the lowest element in the array
                *******/
                int lowest();

                /*****
                        average method
                        Task: the average of elements in the array
                        @returns
                                the average of elements in the array
                *******/
                double averageValue();

                /******
                        occurencesCount method
                        Task: count the number of occurrences in the array of a target value
                        params
                                 targetVal: target value
                        @returns
                                number of occurrences of the target value in the array
                *********/
                int occurencesCount(int targetVal);
                
                /******
                        firstOccurence method
                        Task: returns the position of the first occurrence of a target value
                        params
                                 targetVal: target value
                        @returns
                                position of the first occurrence of target value in the array
                                        if the target value is not in the array, the method must return a -1
                *********/
                int firstOccurence(int targetVal);


                /******
                        printArray method
                        Task: print elements in the array
                        @returns
                                
                *********/
                void printElements();

                /**************

                        Destructor
                ******/
                ~Comp315Array();


                


        //Private members
        private:
                //array size
                int arraySize;
                
                //actual array
                int *actualArray;
                
};

**************************/
This file implements the Comp315Array Class



//Include the header file
#include "Comp315Array.h" //Allow the use of cin and cout

                
        /****************
                Default Constructor
                Task: assign 0 to the array size 
        *********/
        Comp315Array::Comp315Array()
        {
                arraySize = 0;
        }
        
        /****************
                Destructor  
        *********/
        Comp315Array::~Comp315Array()
        {
                actualArray=0;
        }

        /****************
                Constructor
                Task: Define an int array of size n 
        *********/
        Comp315Array::Comp315Array(int n)
        {
                actualArray= new int[n];
                arraySize = n;
        }
        
        
        
        /*****************
                getSize method
                Task: return the array size
        ******************/
        int Comp315Array::getSize(){
                return arraySize;
        }
        

        /*****************
                setValue method
                Task: set array value in position p
        ******************/
        void Comp315Array::setValue(int p, int actualValue){
                        actualArray[p]=actualValue;
        }

        
        /*****
                sum method
                Task: compute the sum of the n elements in the array
                params:
                @returns
                        sum of elements
        *******/
        int Comp315Array::sum(){
                int res=0;
                
                return res;
        }
        
        /*****
                sum method
                Task: compute the sum of the elements in the array from position i to position j
                @returns
                        sum of elements from position i to position j
        *******/
        int Comp315Array::sum(int i, int j){
                int res=0;
                
                return res;
        }
        
        /*****
                greatest method
                Task: returns the greatest element in the array
                @returns
                        value of the greatest element in the array
        *******/
        int Comp315Array::greatest(){
                int res=actualArray[0];
                
                return res;
        }
        
        /*****
                lowest method
                Task: returns the lowest element in the array
                @returns
                        value of the lowest element in the array
        *******/
        int Comp315Array::lowest(){
                int res=actualArray[0];
                
                return res;
        }

        /*****
                average method
                Task: the average of elements in the array
                @returns
                        the average of elements in the array
        *******/
        double Comp315Array::averageValue(){
                int res=0;
                
                return res;
        }

        /******
                occurencesCount method
                Task: count the number of occurrences in the array of a target value
                params
                         targetVal: target value
                @returns
                        number of occurrences of the target value in the array
        *********/
        int Comp315Array::occurencesCount(int targetVal){
                int res=0;
                
                return res;
        }
        
        /******
                firstOccurence method
                Task: returns the position of the first occurrence of a target value
                params
                         targetVal: target value
                @returns
                        position of the first occurrence of target value in the array
                                if the target value is not in the array, the method must return a -1
        *********/
        int Comp315Array::firstOccurence(int targetVal){
                int res=-1;
                
                return res;
        }
        
/******
        printArray method
        Task: print elements in the array
        @returns
                
        *********/
        void Comp315Array::printElements(){
                for(int i=0;i<arraySize; i++){
                        cout<<actualArray[i]<<endl;
                }       
        }
**************************/ 
This file implements the Comp315Array Class


//Include the header file
#include "Comp315Array.h" //Allow the use of cin and cout


int menu()
{
        int op;
        
        op=0;
        while (op==0 || op>10)
        {
                cout<<"***********************************"<<endl;
                cout<<"Select one of the following options"<<endl;
                cout<<"***********************************"<<endl;
                cout<<"   1. Introduce array elements"<<endl;
                cout<<"   2. Compute elements sum"<<endl;
                cout<<"   3. Compute elements sum in a selected range"<<endl;
                cout<<"   4. Compute elements average value"<<endl;
                cout<<"   5. Find the greatest element"<<endl;
                cout<<"   6. Find the lowest element"<<endl;
                cout<<"   7. Count occurrences of an element"<<endl;
                cout<<"   8. Find the first occurrence of an element"<<endl;
                cout<<"   9. Print elements"<<endl;
                cout<<"   10. Exit"<<endl;
                cout<<"***********************************"<<endl;
                cin>>op;
        }
        cout<<"Selected option"<<op<<endl<<endl;
        return op;
}

                
int main(){
        
        int size, op, val;
        Comp315Array cArray;
        
        cout<<"Array size? ";
        cin>>size;
        
        cArray = Comp315Array(size);
        
        
        do{
                op=menu();      
                
                switch (op) {
                        case 1: for (int i=0; i<size; i++)
                                {
                                        cout<<"Element "<<i<<": ";
                                        cin>>val;
                                        cArray.setValue(i,val);
                                }
                                break;

                        case 2: cout<<"The total sum is "<< cArray.sum()<<endl;
                                break;
                        case 3: 
                                int initVal, endVal;
                                cout<<"Initial position: ";
                                cin>>initVal;
                                cout<<"Final position: ";
                                cin>>endVal;      
                                cout<<"The total sum is "<< cArray.sum(initVal, endVal)<<endl;
                                break;                  

                        case 4: cout<<"The aveage value is "<< cArray.averageValue()<<endl;
                                break;
                        case 5: cout<<"The greatest value is "<< cArray.greatest()<<endl;
                                break;
                        case 6: cout<<"The lowest value is "<<cArray.lowest()<<endl;
                                break;
                        case 7: cout<<"Target value: ";
                                cin>>val;
                                cout<<"Number of occurrences: " << cArray.occurencesCount(val) <<endl;
                                break;
                        case 8: cout<<"Target value: ";
                                cin>>val;
                                cout<<"The first occurrence is in position " << cArray.firstOccurence(val) <<endl;
                                break;
                        case 9: cArray.printElements();
                                break;
                                        
                }


        }while(op!=10);
        
        cout<<"Goodbye!"<<endl;

        return 0;
}

Solutions

Expert Solution

/* Comp315Array.h */

//Include the libraries
#include <iostream> //Allow the use of cin and cout
//Declare namespace std for using cin and cout
using namespace std;

//Class declaration
class Comp315Array{
  
//Public members
public:
/****************
Default Constructor
Task: assign 0 to the array size
*********/
Comp315Array();

/****************
Constructor
Task: Define an int array of size n
*********/
Comp315Array(int n);
  
/*****************
getSize method
Task: return the array size
******************/
int getSize();
  
/*****************
setValue method
Task: set array value in position p
******************/
void setValue(int p, int val);
  
/*****
sum method
Task: compute the sum of the n elements in the array
params:
@returns
sum of elements
*******/
int sum();
  
/*****
sum method
Task: compute the sum of the elements in the array from position i to position j
@returns
sum of elements from position i to position j
*******/
int sum(int i, int j);
  
/*****
greatest method
Task: returns the greatest element in the array
@returns
value of the greatest element in the array
*******/
int greatest();
  
/*****
lowest method
Task: returns the lowest element in the array
@returns
value of the lowest element in the array
*******/
int lowest();

/*****
average method
Task: the average of elements in the array
@returns
the average of elements in the array
*******/
double averageValue();

/******
occurencesCount method
Task: count the number of occurrences in the array of a target value
params
targetVal: target value
@returns
number of occurrences of the target value in the array
*********/
int occurencesCount(int targetVal);
  
/******
firstOccurence method
Task: returns the position of the first occurrence of a target value
params
targetVal: target value
@returns
position of the first occurrence of target value in the array
if the target value is not in the array, the method must return a -1
*********/
int firstOccurence(int targetVal);


/******
printArray method
Task: print elements in the array
@returns
  
*********/
void printElements();

/**************

Destructor
******/
~Comp315Array();


//Private members
private:
//array size
int arraySize;
  
//actual array
int *actualArray;
  
};

/* Comp315Array.cpp */

#include "Comp315Array.h" //Allow the use of cin and cout

  
/****************
Default Constructor
Task: assign 0 to the array size
*********/
Comp315Array::Comp315Array()
{
arraySize = 0;
}
  
/****************
Destructor
*********/
Comp315Array::~Comp315Array()
{
actualArray=0;
}

/****************
Constructor
Task: Define an int array of size n
*********/
Comp315Array::Comp315Array(int n)
{
actualArray= new int[n];
arraySize = n;
}
  
  
  
/*****************
getSize method
Task: return the array size
******************/
int Comp315Array::getSize(){
return arraySize;
}
  

/*****************
setValue method
Task: set array value in position p
******************/
void Comp315Array::setValue(int p, int actualValue){
actualArray[p]=actualValue;
}

  
/*****
sum method
Task: compute the sum of the n elements in the array
params:
@returns
sum of elements
*******/
int Comp315Array::sum(){
int res=0;
for(int i = 0; i < arraySize; i++){
  
   res = res + actualArray[i];
                  
               }
return res;
}
  
/*****
sum method
Task: compute the sum of the elements in the array from position i to position j
@returns
sum of elements from position i to position j
*******/
int Comp315Array::sum(int i, int j){
int res=0;
for(int start = i; start <= j; start++){
   res+=actualArray[start];
               }
return res;
}
  
/*****
greatest method
Task: returns the greatest element in the array
@returns
value of the greatest element in the array
*******/
int Comp315Array::greatest(){
int res=actualArray[0];
for(int i = 0; i < arraySize; i++){
   if(res < actualArray[i]){
       res = actualArray[i];
                   }
               }
return res;
}
  
/*****
lowest method
Task: returns the lowest element in the array
@returns
value of the lowest element in the array
*******/
int Comp315Array::lowest(){
int res=actualArray[0];
for(int i = 0; i < arraySize; i++){
   if(res > actualArray[i]){
       res = actualArray[i];
                   }
               }
return res;
}

/*****
average method
Task: the average of elements in the array
@returns
the average of elements in the array
*******/
double Comp315Array::averageValue(){
double res=0;
res = ((double)sum())/arraySize;
return res;
}

/******
occurencesCount method
Task: count the number of occurrences in the array of a target value
params
targetVal: target value
@returns
number of occurrences of the target value in the array
*********/
int Comp315Array::occurencesCount(int targetVal){
int res=0;
for(int i = 0; i < arraySize; i++){
   if(actualArray[i] == targetVal){
       res++;
                   }
               }
return res;
}
  
/******
firstOccurence method
Task: returns the position of the first occurrence of a target value
params
targetVal: target value
@returns
position of the first occurrence of target value in the array
if the target value is not in the array, the method must return a -1
*********/
int Comp315Array::firstOccurence(int targetVal){
int res=-1;
for(int i = 0; i < arraySize; i++){
   if(actualArray[i] == targetVal){
       return i;
                   }
               }
return res;
}
  
/******
printArray method
Task: print elements in the array
@returns
  
*********/
void Comp315Array::printElements(){
for(int i=0;i<arraySize; i++){
cout<<actualArray[i]<<endl;
}   
}
/***************************/

/* main.cpp */

//Include the header file
#include "Comp315Array.cpp" //Allow the use of cin and cout


int menu()
{
int op;
  
op=0;
while (op==0 || op>10)
{
cout<<"***********************************"<<endl;
cout<<"Select one of the following options"<<endl;
cout<<"***********************************"<<endl;
cout<<" 1. Introduce array elements"<<endl;
cout<<" 2. Compute elements sum"<<endl;
cout<<" 3. Compute elements sum in a selected range"<<endl;
cout<<" 4. Compute elements average value"<<endl;
cout<<" 5. Find the greatest element"<<endl;
cout<<" 6. Find the lowest element"<<endl;
cout<<" 7. Count occurrences of an element"<<endl;
cout<<" 8. Find the first occurrence of an element"<<endl;
cout<<" 9. Print elements"<<endl;
cout<<" 10. Exit"<<endl;
cout<<"***********************************"<<endl;
cin>>op;
}
cout<<"Selected option"<<op<<endl<<endl;
return op;
}

  
int main(){
  
int size, op, val;
Comp315Array cArray;
  
cout<<"Array size? ";
cin>>size;
  
cArray = Comp315Array(size);
  
  
do{
op=menu();
  
switch (op) {
case 1: for (int i=0; i<size; i++)
{
cout<<"Element "<<i<<": ";
cin>>val;
cArray.setValue(i,val);
}
break;

case 2: cout<<"The total sum is "<< cArray.sum()<<endl;
break;
case 3:
int initVal, endVal;
cout<<"Initial position: ";
cin>>initVal;
cout<<"Final position: ";
cin>>endVal;
cout<<"The total sum is "<< cArray.sum(initVal, endVal)<<endl;
break;

case 4: cout<<"The aveage value is "<< cArray.averageValue()<<endl;
break;
case 5: cout<<"The greatest value is "<< cArray.greatest()<<endl;
break;
case 6: cout<<"The lowest value is "<<cArray.lowest()<<endl;
break;
case 7: cout<<"Target value: ";
cin>>val;
cout<<"Number of occurrences: " << cArray.occurencesCount(val) <<endl;
break;
case 8: cout<<"Target value: ";
cin>>val;
cout<<"The first occurrence is in position " << cArray.firstOccurence(val) <<endl;
break;
case 9: cArray.printElements();
break;
  
}


}while(op!=10);
  
cout<<"Goodbye!"<<endl;

return 0;
}

/* PLEASE UPVOTE */

/* OUTPUT */

/*

Array size? 5
***********************************
Select one of the following options
***********************************
1. Introduce array elements
2. Compute elements sum
3. Compute elements sum in a selected range
4. Compute elements average value
5. Find the greatest element
6. Find the lowest element
7. Count occurrences of an element
8. Find the first occurrence of an element
9. Print elements
10. Exit
***********************************
1
Selected option1

Element 0: 5
Element 1: 5
Element 2: 4
Element 3: 3
Element 4: 2
***********************************
Select one of the following options
***********************************
1. Introduce array elements
2. Compute elements sum
3. Compute elements sum in a selected range
4. Compute elements average value
5. Find the greatest element
6. Find the lowest element
7. Count occurrences of an element
8. Find the first occurrence of an element
9. Print elements
10. Exit
***********************************
2
Selected option2

The total sum is 19
***********************************
Select one of the following options
***********************************
1. Introduce array elements
2. Compute elements sum
3. Compute elements sum in a selected range
4. Compute elements average value
5. Find the greatest element
6. Find the lowest element
7. Count occurrences of an element
8. Find the first occurrence of an element
9. Print elements
10. Exit
***********************************
3
Selected option3

Initial position: 1
Final position: 3
The total sum is 12
***********************************
Select one of the following options
***********************************
1. Introduce array elements
2. Compute elements sum
3. Compute elements sum in a selected range
4. Compute elements average value
5. Find the greatest element
6. Find the lowest element
7. Count occurrences of an element
8. Find the first occurrence of an element
9. Print elements
10. Exit
***********************************
4
Selected option4

The aveage value is 3.8
***********************************
Select one of the following options
***********************************
1. Introduce array elements
2. Compute elements sum
3. Compute elements sum in a selected range
4. Compute elements average value
5. Find the greatest element
6. Find the lowest element
7. Count occurrences of an element
8. Find the first occurrence of an element
9. Print elements
10. Exit
***********************************
5
Selected option5

The greatest value is 5
***********************************
Select one of the following options
***********************************
1. Introduce array elements
2. Compute elements sum
3. Compute elements sum in a selected range
4. Compute elements average value
5. Find the greatest element
6. Find the lowest element
7. Count occurrences of an element
8. Find the first occurrence of an element
9. Print elements
10. Exit
***********************************
6
Selected option6

The lowest value is 2
***********************************
Select one of the following options
***********************************
1. Introduce array elements
2. Compute elements sum
3. Compute elements sum in a selected range
4. Compute elements average value
5. Find the greatest element
6. Find the lowest element
7. Count occurrences of an element
8. Find the first occurrence of an element
9. Print elements
10. Exit
***********************************
7
Selected option7

Target value: 5
Number of occurrences: 2
***********************************
Select one of the following options
***********************************
1. Introduce array elements
2. Compute elements sum
3. Compute elements sum in a selected range
4. Compute elements average value
5. Find the greatest element
6. Find the lowest element
7. Count occurrences of an element
8. Find the first occurrence of an element
9. Print elements
10. Exit
***********************************
8
Selected option8

Target value: 5
The first occurrence is in position 0
***********************************
Select one of the following options
***********************************
1. Introduce array elements
2. Compute elements sum
3. Compute elements sum in a selected range
4. Compute elements average value
5. Find the greatest element
6. Find the lowest element
7. Count occurrences of an element
8. Find the first occurrence of an element
9. Print elements
10. Exit
***********************************
9
Selected option9

5
5
4
3
2
***********************************
Select one of the following options
***********************************
1. Introduce array elements
2. Compute elements sum
3. Compute elements sum in a selected range
4. Compute elements average value
5. Find the greatest element
6. Find the lowest element
7. Count occurrences of an element
8. Find the first occurrence of an element
9. Print elements
10. Exit
***********************************
10
Selected option10

Goodbye!

--------------------------------
Process exited after 74.96 seconds with return value 0
Press any key to continue . . .

*/


Related Solutions

Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Your primary task for this exercise is to complete header file by writing three functions with...
Your primary task for this exercise is to complete header file by writing three functions with its description below: removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list. insertAt function - to insert an item...
please complete the header file that contains a class template for ADT Queue and complete all...
please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly. queue.h #ifndef _QUEUE #define _QUEUE #include"Node.h" template<class ItemType> class Queue { private:    Node<ItemType> *backPtr;    Node<ItemType> *frontPtr; public:    Queue(); //Default constructor    Queue(const Queue<ItemType> &aQueue);    bool isEmpty() const;    bool...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
You are required to come up with a single header file (IntList.h) that declares and implements...
You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement more than 1...
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library....
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library. Your C code //SHOULD access your routines using these exact function // prototypes typedef struct RW_lock_s { } RW_lock_t; void RW_lock_init(RW_lock_t *lock); /* This routine should be called on a pointer to a struct variable of RW_lock_t to initialize it and ready it for use. */ void RW_read_lock(RW_lock_t *lock); /* This routine should be called at the beginning of a READER critical section */...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT