In: Computer Science
You are required to write an interactive program that prompts the user for ten (10) real numbers and performs the following tasks:
Program requirements:
- The program must utilize a class, an object and all the necessary functions to perform the above tasks.
- The program must be fully documented.
- You must submit a hard copy of the source and a properly labeled output.
- You should submit a digital copy of source code.
- Test your program for different values using real numbers.
#include <iostream>
using namespace std;
class Array{
private:
int *A; // pointer type array aka dynamic array
int size; // size of array
public:
Array(){ //default constructor sets size to 10
size=10;
A= new int[size]; // allocate memory of 10 blocks for array A
}
Array(int siz){ // if user gives size
size=siz; // set size
A= new int[size]; // allocate memory of size entered
}
// mutators of array and size
void setArray(int *arra){
A=arra;
}
void setSize(int siz){
size=siz;
}
// accessors of array and size
int *getArray(){
return A;
}
int getSize(){
return size;
}
void readData(){ // input array data
cout<<"Enter values for your array: ";
for(int i=0;i<size;i++){ // loops iterate till size
cin>>A[i]; // read and store into array
}
}
void print(){ // printing data
cout<<"::::::: ARRAY DATA ::::::::"<<endl;
for(int i=0;i<size;i++){
cout<<A[i]<<" ";
}
cout<<endl;
}
int calculateSum(){
int sum=0; // intialize to 0
for(int i=0;i<size;i++){
sum=A[i]+sum; // update sum
}
return sum;
}
int calculateAverage(int sum){ // sum will be passed
return sum/size; //average of array data
}
void printSumAndAverage(int sum, int average){ // we will be
passing sum and average
//print them
cout<<"SUM: "<<sum<<endl;
cout<<"AVERAGE: "<<average<<endl;
}
};
int main()
{
Array a; // by default size of 10 as told
a.readData();// read and store data
a.print(); // print them
cout<<endl;
// sum and average will be calculated before printSumAndAverage
program will run
a.printSumAndAverage(a.calculateSum(),a.calculateAverage(a.calculateSum()));
return 0;
}
OUTPUT:
IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP