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
|
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
//prototypes
void read(int f, int c);
double average(int s);
void show(int f, int c);
double averageDigits(int n);
//global definitions
int i,j,A[100][100],n,m,x,sum=0;
double avg,xdigits=0;
int main()
{
cout<<"Type number of rows---->";
cin>>n;
cout<<"Type number of columns---->";
cin>>m;
read(n,m);
cout<<"\n\nThe Average of the Elements Is---->" <<average(sum)<<endl;
show(n,m);
cout<<"\nThe Avererage of the Digits of Each Element--->"<<averageDigits<<A[i][j]<<" ";
cout<<endl;
cin.get();
return 0;
}
void read(int f,int c)
{
for(i=0;i<f;i++){
for(j=0;j<c;j++){
cout<<"A["<<i<<"]["<<j<<"]:";
cin>>A[i][j];
sum +=A[i][j];
if(A[i][j]<=999)
continue;
else
cout<<"\nInvalid Option...Try Again";
exit(EXIT_FAILURE);
}
}
}
double averageDigits(int x)
{
if (x < 0) x = -x; // quick n=abs(n)
if (x < 10) return (double) x; //average of a one digit number
int sum=0, xdigits = 0;
do {
++xdigits; // count digits
sum += x % 10; // add low digit of n to sum
x /=10; // delete low digit of n
} while (x > 0);
return (double)sum / xdigits;
}
double average(int s)
{
avg=s/(n*m);
return avg;
}
void show(int f,int c)
{
cout<<"\n\tINITIAL MATRIX\n";
for(i=0;i<f;i++){
cout<<endl;
for(j=0;j<c;j++)
cout<<setw(10)<<A[i][j];
}
cout<<endl;
}
|