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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
//Outputs both unsorted and sorted array using list_display.
void list_display (const int [], int );
//average value of the numbers.
float list_average (const int list[], int l);
//highest value of the numbers.
int list_max (const int list[], int l);
//smallest value of the numbers.
int list_min (const int list[], int l);
//count the number of occurrences of some key value in list
int list_find(const int list[], int l, int number);
//sorts array in ascending order using a swap sort.
void list_sort(int list[], int l);
int main ()
{
const int l=20;
// 20 integer numbers between 1 and 20 are stored in an array called list
int list[l] = {1,4,6,8,9,10,3,5,7,9,1,9,2,8,3,7,4,5,6,7};
int number;
list_display (list,l);
cout <<list_average(list,l)<<endl;
cout<<list_min(list,l)<<endl;
cout<<list_max(list,l)<<endl;
// key value to find occurences
cin>>number;
cout<<list_find(list,l,number)<<endl;
cout<<list_sort (list,l)<<endl;
cout<<list_display(list,1)<<endl;
return 0;
}
float list_average (const int list[], int l)
{
float sum=0;
for(int i = 0; i<l; ++i) sum+=list[i];
return sum/l;
}
void list_display (const int list[], int l)
{
for(int i = 0; i<l; ++i)
cout << list[i]<<endl;
}
int list_max (const int list[], int l)
{
int largest=list[0];
for(int i = 0; i<l; ++i)
{
if (list[i]> largest)
{
largest=list[i];
}
}
return largest;
}
int list_min (const int list[], int l)
{
int smallest=list[0];
for(int i = 0; i<l; ++i)
{
if (list[i]< smallest)
{
smallest=list[i];
}
}
return smallest;
}
int list_find (const int list[], int l, int number)
{
int sum=0;
for(int i = 0; i<l; ++i)
{
if (list[i]== number)
{
sum+=1;
}
}
return sum;
}
void list_sort (int list[], int l)
{
int x;
for(int i=0; i<l-1; ++i)
for (int j=i+1; j<l;j++)
{
if (list[i]<list[j])
{
x=list[i];
list[i]=list[j];
list[j]=x;
}
}
}
|