Define a class to declare an array of size 20 of double datatype, accept the elements into
the array and perform the following:
• Calculate and print the sum of all the elements.
• Calculate and print the highest value of the array.
Answers
The C++ program for the given question is,
#include<iostream>
using namespace std;
class some{
double arr[20];
public:
void init(){
cout<<"Start entering the elements ::::::\n";
for(int i=0;i<20;i++){
cout<<"\t";
cin>>arr[i];
}
}
void sum();
void high();
};
void some::sum(){
int sum_all;
for(int i=0;i<20;i++){
sum_all+=arr[i];
}
cout<<"\nThe sum of all the elements is "<<sum_all;
}
void some::high(){
int maximum = arr[0];
for(int i=1;i<20;i++){
if(arr[i] > maximum)
maximum = arr[i];
}
cout<<"\nHighest value of the array is "<<maximum;
}
int main(){
some object;
object.init();
object.sum();
object.high();
return 0;
}