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 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//This function needs to read the ten integers from the input file data2.txt into array A
void readArray(const int a[], int SIZE)
{
int i;
for(int i = 0; i < SIZE; i++)
{
cout << a[i];
}
}
//This function needs to copy array A into array B in reverse order
void reverseArray(const int a[], int SIZE)
{
int i;
for(int i = 9; i > 0; i--)
{
cout << a[i];
}
}
//This function needs to print array A
void printA(const int a[], int SIZE)
{
int i;
for(i = 0; i < 10; i++)
cout << a[i] << " ";
}
//This function needs to print array B
void printB(const int a[], int SIZE)
{
int i = 9;
while(i <= 0)
{
cout << a[i] << " ";
i--;
}
}
//This function needs to find the number of elements in array A that are >= 80 & <=100
int Elements(int a[], const int SIZE)
{
int counter = 0;
for(int i = 0; i < SIZE; i++)
{
if((a[i] >= 80) && (a[i] = 100))
counter ++;
}
return counter;
}
//this function needs to find the number of the elements in array A in which their contents are divisible by 5
int divisible(int a[], const int SIZE)
{
int counter = 0;
for(int i = 0; i < SIZE; i++)
{
if(a[i] % 5 == 0)
counter ++;
}
return counter;
}
//This function needs to find the index of the elements in array A in which their contents are divisible by 5
void index(int a[], const int SIZE)
{
for(int i = 0; i < SIZE; i++)
{
if(a[i] % 5 == 0)
cout << i;
}
}
////This function needs to find the mean(average) in array A and return the mean(average)
float findAvg(const int a[], int SIZE)
{
int total = 0;
for(int i=0; i < SIZE; i++)
{
total += a[i];
}
return (float)total / SIZE;
}
//This function needs to find the minimum number in array A and return the minimum number (which is 20), since we're returning something this function is not a void
int findMin(const int a[], int SIZE)
{
int i;
int min = a[0];
for(i = 0; i < SIZE; i++)
{
if(a[i] < min)
min = a[i];
}
return min;
}
int main()
{
const int SIZE = 10;
int a[SIZE];
float avg;
int min;
int elem;
int div;
ifstream fin;
fin.open("data2.txt");
if(!fin)
cout << "The input file doesn't exist" << endl;
else
{
while(!fin.eof())
{
readArray(a, SIZE); //function call
reverseArray(a, SIZE); //function call
printA(a, SIZE); //function call
printB(a, SIZE); //function call
elem = Elements(a, SIZE); //function call
cout << "Numbers of elements that are greater or equal to 80 and less than or equal to 100: " << elem << endl;
div = divisible(a, SIZE); //function call
cout << "Number of elements in array A in which their content is divisible by 5: " << div << endl;
index(a, SIZE); //function call
avg = findAvg(a, SIZE); //function call
cout << avg;
min = findMin(a, SIZE); //function call
cout << "The minimum is: " << min << endl;
}
fin.close();
}
return 0;
}
|