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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include <iostream>
#include <cmath>;
using namespace std;
/****************************
PRTOTYPE FUNXTION
**************************/
int findSmallestRemainingElement (signed int array[], int size, int index);
void swap (signed int array[], int first_index, int second_index);
void sort (signed int array[], int size);
void displayArray (signed int array[], int size);
int average (signed int array [],int size);
int sum (signed int array[], int size);
/****************************
MAIN FUNCTION
****************************/
int main ()
{
// DECLARE THE ARRAY
signed int array[ 5 ];
// FOR LOOP TO KEEP ASKING FOR IMPUT
for ( int i = 0; i < 5; i++ )
{
cout << "Enter value " << i << ": ";
cin >> array[ i ];
}
for(int j=0;j<5; j++)
{
if(array[j]<=0)
{
cout<<"Array element "<<j<<" is either negative or zero."<<endl
<<"Element is : "<<array[j]<<endl;
}
}
//PRINT ORHINAL ARRAY
cout << "Original array: ";
displayArray( array, 5 );
cout << '\n';
//SORT ORGINAL ARRAY
sort( array, 5 );
// DISPLAY SORTED ARRAY
cout << "Sorted array: ";
displayArray( array, 5 );
cout << '\n';
//sum of number in array
signed int sum = 0,avg=1;
for (int j = 0; j < 5; ++j)
{
sum=sum + array[j];
}
avg=sum/5;
cout << "\n The mean is\t" << avg<< "\n"
<<"And the sum is : "<<sum<<endl;
}
/*********************************
SORT FUNCTION
********************************/
void sort (signed int array[], int size)
{
for ( int i = 0; i < size; i++ )
{
int index = findSmallestRemainingElement( array, size, i );
swap( array, i, index );
}
}
/*******************************
FIND SMALLEST NUMBER
******************************/
int findSmallestRemainingElement (signed int array[], int size, int index)
{
int index_of_smallest_value = index;
for (int i = index + 1; i < size; i++)
{
if ( array[ i ] < array[ index_of_smallest_value ] )
{
index_of_smallest_value = i;
}
}
return index_of_smallest_value;
}
/**************************
SWAP FUNCTION
*************************/
void swap (signed int array[], int first_index,int second_index)
{
int temp = array[ first_index ];
array[ first_index ] = array[ second_index ];
array[ second_index ] = temp;
}
/******************************
DIPLAY ARRAY FUNCTION
*****************************/
void displayArray (signed int array[], int size)
{
cout << "{";
for ( int i = 0; i < size; i++ )
{
if ( i != 0 )
{
cout << ", ";
}
cout << array[ i ];
}
cout << "}";
}
|