1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
//ProcessArrayData.cpp
//#
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void printArray(const int a[],int size,const int NUM_PER_LINE); //function prototypes
void setArray(int a[],int size);
int getSumArray(const int a[],int size);
int getSmallestArrayElement(const int a[],int size);
int getHighestArrayElement(const int a[],int size);
double getAverageArrayElements(const int a[],int size);
int main(){
const int MAX=12;
const int NUM_PER_LINE=6;
int array[MAX]={}; //array elements initialized to 0
cout<<"Enter "<<MAX<<" numbers\n"<<endl;
setArray(array,MAX);
cout<<"\nArray elements are:\n";
printArray(array,MAX,NUM_PER_LINE);
cout<<"Sum: "<<getSumArray(array,MAX)<<endl;
cout<<"Average: "<<getAverageArrayElements(array,MAX)<<endl;
cout<<"Highest: "<<getHighestArrayElement(array,MAX)<<endl;
cout<<"Smallest: "<<getSmallestArrayElement(array,MAX)<<endl;
return 0; //indicates success
}//end main
void printArray(const int array[],int size,const int NUM_PER_LINE){
for(int index=0;index<size;index++){
cout<<array[index]<<((index+1)%NUM_PER_LINE==0?'\n':' ');
}//end for
cout<<endl; //new line
}//end function printArray
void setArray(int array[],int size){
for(int index=0;index<size;index++){
cout<<"Enter value #"<<index+1<<':';
cin>>array[index];
}//end for
}//end function setArray
int getSumArray(const int array[],int size){
int sum=0;
for(int index=0;index<size;index++)
sum+=array[index];
return sum;
}//end function getSumArray
double getAverageArrayElements(const int array[],int size){
double average=0;
int sum;
sum=getSumArray(array,size);
average=static_cast<double>(sum)/size;
return average;
}//end function getAverageArrayElements
int getSmallestArrayElement(const int array[],int size){
int smallest=array[0];
for(int index=1;index<size;index++)
if(array[index]<smallest) smallest=array[index];
return smallest;
}//end function getSmallestArrayElement
int getHighestArrayElement(const int array[],int size){
int highest=array[0];
for(int index=1;index<size;index++)
if(array[index]>highest) highest=array[index];
return highest;
}//end function getLargestArrayElement
|