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 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <conio.h>
#include <iomanip>
#include <windows.h>
using namespace std;
int main()
void insertV(int z[], int n) //void insertV(int v[100], int n)
{
for(int i=0; i<n; i++)
{
z[i]=rand()%10;
}
}
void showV(int z[], int n) //display (int v[100], int n)
{
for(int i=0; i<n; i++)
{
cout<<setw(3)<<z[i];
}
}
int sumaV(int z[], int n) //array sum
{
int s=0;
for(int i=0; i<n; i++)
{
s+=z[i];
}
return s;
}
int maxV(int z[], int n) // maximum element in array
{
int m=z[0];
for(int i=0; i<n; i++)
{
if (m<z[i]) m=z[i];
}
return m;
}
int maxp(int z[],int n) //maximum odd value;
{
int mp=z[0];
for(int i=0; i<n; i++)
{
if (z[i] %2==0 && mp < z[i]) mp=z[i];
}
return mp;
}
int mini(int z[],int n) //minimum even value;
{
int mi=z[0];
for(int i=0; i<n; i++)
{
if (z[i] %2 !=0 && mi > z[i]) mi=z[i];
}
return mi;
}
void ad(int z[],int n, bool q)// ascending & descending order
{
bool x=true;
while(x)
{
x=false;
for( int i=0,temp; i < n-1; i++)
{
if(q){
if(z[i] < z[i+1] )
{
x=true;
temp = z[i];
z[i] = z[i+1];
z[i+1] = temp; }}
if(z[i] > z[i+1] )
{
x=true;
temp = z[i];
z[i] = z[i+1];
z[i+1] = temp;
}
}
}
}
{
srand(time(0));
int v[100],n=insertINT("\n Enter n=");
insertV(v,n);
showV(v,n);
int sv=sumaV(v,n);
cout<<" sv="<<sv<<endl;
int m=maxV(v,n);
cout<<" max="<<m<<endl;
int pi=maxp(v,n);
cout<<" maxeven="<<pi<<endl;
int ci=mini(v,n);
cout<<" minodd="<<ci<<endl;
ad(v,n,true); //I am trying to somehow force the app to check both true
ad(v,n,false); //and false conditions
showV(v,n);
return 0;
}
|