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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//This function reads the ten integers from the input file data2.txt into array \
A
void readArray(int a[])
{
//fill the array with #'s read from input file
ifstream fin;
fin.open("data2.txt");
int num;
if(!fin)
cout << "The input file doesn't exist " << endl;
else //file exists
{
int i = 0;
while(!fin.eof())
{
fin >> num;
a[i] = num;
i++;
}
}
fin.close();
}
//This function copies array A into array B in reverse order
void reverseArray(const int a[], int b[], int s)
{
for(int i = 0;i <s;i++)
{
b[i]= a[s-i-1];
}
}
//This function prints array A and displays it
void print( int a[], int SIZE)
{
for(int i = 0; i < SIZE; i++)
{
cout << a[i] << " ";
}
}
//This function finds the number of elements in array A that are >= 80 and <= 10\
0
int Elements(int a[], int SIZE)
{
int counter = 0;
for(int i = 0; i < SIZE; i++)
{
if((a[i] >= 80) && (a[i] = 100))
counter ++;
}
return counter; //value that is returned to the main function
}
//This function finds the number of the elements in array A in which their conte\
nts are divisible by 5 and returns the number of elements to the main function
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; //value that is returned to the main function
}
//This function finds the index of the elements in array A in which their conten\
ts are divisible by 5 and prints the elements
void index(int a[], const int SIZE)
{
for(int i = 0; i < SIZE; i++)
{
if(a[i] % 5 == 0)
cout << i << " ";
}
}
//this function finds the mean(average) in array A andd returns the mean(average\
) to the main function
float findAvg(const int a[], int SIZE)
{
float sum, ava;
for(int i = 0; i < SIZE; i++)
{
sum += a[i];
}
ava = sum / SIZE;
return ava; //the value that is returned to the main function
}
//This function finds the minimum number in array A and returns the minimum numb\
er to the main function
int findMin(int a[], int SIZE)
{
int min = 0;
for(int i = 0; i < SIZE; i++)
{
if(a[min] > a[i])
min = i;
}
return a[min]; //the value that is returned to the main function
}
bool userKey(int a[], int SIZE)
{
int key;
cout << "Please enter a key: " << endl;
cin >> key;
for(int i = 0; i < SIZE; i++)
{
if(a[i] == key)
return true;
else
return false;
}
}
int main()
{
const int SIZE = 10;
int a[SIZE];
int b[SIZE];
float avg;
int min;
int elem;
int div;
int key;
readArray(a); //function that reads array A gets called here
reverseArray(a, b, SIZE); //function that reads the reverse of array A\
gets called
cout << "Array A: " << endl;
print(a, SIZE); //function that prints array A gets called
cout << endl;
cout << "Array B/reverse: " << endl;
print(b, SIZE); //function that prints array B gets called
cout << endl;
elem = Elements(a, SIZE); //function that finds # of elements >=80 and\
100 <= gets called
cout << "Numbers of elements that are greater or equal to 80 and less \
than or equal to 100: " << elem << endl;
cout << endl;
div = divisible(a, SIZE); //function that finds the elements that are \
divisible by 5 gets called
cout << "Number of elements in array A in which their content is divis\
ible by 5: " << div << endl;
cout << endl;
cout << "Index of the elements in Array A in which their content in di\
visible by 5: " << endl;
index(a, SIZE); //function that finds the index of the elements that a\
re divisible by 5
cout << endl;
avg = findAvg(a, SIZE); //function that finds the average in array A g\
ets called
cout << "The mean average in Array A is: " << avg << endl;
cout << endl;
min = findMin(a, SIZE); //function that finds the minimum in array A g\
ets called
cout << "The minimum is: " << min << endl;
cout << endl;
key = userKey(a, SIZE);
cout << "The number entered is found in the array: " << endl;
if(key == 1)
{
cout << "True " << endl;
}
else
{
cout << "False " << endl;
}
return 0;
}
|