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
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void fillArray(int A[], int s);
void copyArray(const int A[], int B[], int s);
void printArrayA(int A[], int s);
void printArrayB(int B[], int s);
int bet80and100(int A[], int s);
int divby5(int A[], int s);
float FindAvg(int A[], int s);
int FindMin(int A[], int s);
int main()
{
const int SIZE = 10;
int A[SIZE];
int B[SIZE];
fillArray(A, SIZE);
copyArray(B, SIZE); // too few params: copyArray( A, B, SIZE );
bet80and100(A, SIZE);
divby5(A, SIZE);
FindAvg(A, SIZE);
FindMin(A, SIZE);
cout << "The number of elements between 80 and 100 is " << bet80and100(A, SIZE) << endl;
cout << "The number of elements between 80 and 100 is " << divby5(A, SIZE) << endl;
cout << "The average of all elements in Array A is " << FindAvg(A, SIZE) << endl;
return 0;
}
//A
void fillArray(int A[], int s)
{
//fill the array with #'s read from input file
ifstream fin;
fin.open("data2.txt");
if(!fin)
cout << "data2.txt does not exist." << endl;
else //data2.txt exists
{
for(int i=0; i<s; i++)
fin >> A[i];
}
}
//B
void copyArray(const int A[], int B[], int s)
{
for(int i=s-1, j=0; j<s; i--, j++)
B[j]= A[i];
}
//C
void printArrayA(int A[], int s)
{
cout << "Array A includes: ";
for(int i=0; i<s; i++)
cout << A[i] << " ";
cout << endl;
}
//D
void printArrayB(int B[], int s)
{
cout << "Array B includes: ";
for(int i=0; i<s; i++)
cout << B[i] << " ";
cout << endl;
}
//E
int bet80and100(int A[], int s)
{
int count = 0;
for(int i=0; i<s; i++)
if(A[i] <= 100 && A[i] >= 80)
count++;
return count;
}
//F
int divby5(int A[], int s)
{
int count = 0;
for(int i=0; i<s; i++)
if(A[i] % 2 == 0)
count++;
return count;
}
//G
//H
float FindAvg(int A[], int s)
{
int num = 0;
for(int i=0; i<s; i++)
num = A[i] + num;
float avg = num/3.0;
return avg;
}
//I
int FindMin(int A[], int s)
{
int min = A[0];
for(int i=1; i<s; i++)
if(A[i] < min)
min = A[i];
return min;
}
| |