Create a class called numbers which should hold 10 integer numbers in an array. The array should be private. Now include the following member functions in the class (in addition to any other function that you may deem necessary): (a) sum() which will find the sum of the member elements. (b) average() which will find the average (c) max() which returns the maximum value (d) sort_desc() which will sort the array in descending order.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
/*
Create a class called numbers which should hold 10 integer numbers in an array.
The array should be private. Now include the following member functions in the class
(in addition to any other function that you may deem necessary):
(a) sum() which will find the sum of the member elements.
(b) average() which will find the average
(c) max() which returns the maximum value
(d) sort_desc() which will sort the array in descending order.
*/
class Number
{
public:
int Nums[10];
void GetNumbers(int *n, int l)
{
int a;
for(a=0;a<l;a++)
{
Nums[a] = *(n+a);
cout<<"\n\tNum["<<a+1<<"] = "<<Nums[a];
}
}
void Sum(int l)
{
int S=0,a;
for(a=0;a<l;a++) S = S + Nums[a];
cout<<"\n\tSum of Numbers = "<<S;
}
void Average(int l)
{
int a;
float Avg=0;
for(a=0;a<l;a++) Avg = Avg + Nums[a];
cout<<"\n\tAverage of Numbers = "<<(Avg/l);
}
void Max(int l)
{
int Max;
Max = Nums[0];
int S=0,a;
for(a=0;a<l;a++)
{
if(Max<=Nums[a]) Max = Nums[a];
}
cout<<"\n\tLargest Numbers = "<<Max;
}
void swap(int *p,int *q)
{
unsigned int t;
t=*p;
*p=*q;
*q=t;
}
void Sort_Desc(int n)
{
int i,j;
int temp;
for(i = 0;i < n-1;i++)
{
for(j = 0;j < n-i-1;j++)
{
if(Nums[j] > Nums[j+1]) swap(&Nums[j],&Nums[j+1]);
}
}
cout<<"\nThe sorted numbers are: ";
// for(i = 0;i < n;i++) cout<<a[i]<<", ";
for(i = n-1;i >=0;i--) cout<<Nums[i]<<", ";
}
};
int main()
{
int N[10] = {1,3,5,2,4,6,8,9,10,7};
class Number Num;
Num.GetNumbers(&N[0],10);
Num.Sum(10);
Num.Average(10);
Num.Max(10);
Num.Sort_Desc(10);
return(0);
}
Comments
Leave a comment