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
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
ifstream inF;
ofstream outF;
void printArray(int arr[], int n)
{
int x=0;
for(x=0;x<n;x++)
outF<<arr[x]<<" "<<endl;
}
void Add(int a[], int b[], int c[], int n)
{
int x=0;
for(x=0;x<n;x++)
c[x]=a[x]+b[x];
}
void Product(int a[], int b[], int c[], int n)
{
int x;
for(x=0;x<n;x++)
c[x]=a[x]*b[x];
}
void Average(int a[], int n)
{
float sum=0;
int x=0;
for(x=0;x<n;x++)
sum+=a[x];
outF<<(sum*1.0)/n<<endl;
}
int Largest(int arr[], int n)
{
int max=0,x;
for(x=0;x<n;x++)
if(arr[x]>max)
max=arr[x];
return max;
}
int Smallest(int arr[], int n)
{
int min=99, x;
for(x=0;x<n;x++)
if(arr[x]<min)
min=arr[x];
return min;
}
void Avg_Odd(int arr[], int n)
{
int count=0, sum=0, x=0;
for(x=0;x<n;x++)
if(arr[x]%2==1)
{
sum+=arr[x];
count++;
}
outF<<(sum*1.0)/count<<endl;
}
int main()
{
inF.open("numbers2.in");
if(!inF)
{
cout<<"Error, try beating it with your hand!";
return 0;
}
outF<<fixed<<setprecision(2);
outF.open("operation.out");
int a[25], b[25], c[25],x=0,max,min;
for(x=0;x<25;x++)
{
inF>>a[x];
b[x]=rand()%100+1;
}
outF<<"array A has : ",
printArray(a,25);
outF<<endl;
outF<<"array B has : ",
printArray(b,25);
outF<<endl;
Add(a,b,c,25);
outF<<"array C when A,B are added:",
printArray(c,25);
outF<<endl;
Product(a,b,c,25);
outF<<"array C when A,B are Multiplied:",
printArray(c,25);
outF<<endl;
outF<<"Average of array A is:";
Average(a,25);
outF<<endl;
outF<<"Average of array B is:";
Average(b,25);
outF<<endl;
outF<<"largest number in array A is:";
max=Largest(a,25);
outF<<max<<endl;
outF<<"largest number in array B is:";
max=Largest(b,25);
outF<<max<<endl;
outF<<"smallest number in array A is:";
min=Smallest(a,25);
outF<<min<<endl;
outF<<"smallest number in array B is:";
min=Smallest(b,25);
outF<<min<<endl;
outF<<"Average of odd numbers in array A is:";
Avg_Odd(a,25);
outF<<endl;
outF<<"Average of odd numbers in array B is: ";
Avg_Odd(b,25);
outF<<endl;
outF.close();
inF.close();
return 0;
}
|