In: Computer Science
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; }
/* 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 . . .
*/