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
|
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector> //<-
using namespace std;
void fillArray(int ar[][10], int size);
void printArray(int ar[][10], int size);
int getDivisible(int ar[][10], int size, int num, vector<int> &results); //<-
int main()
{
srand((unsigned int)time(0));
int ar[10][10] = {0};
fillArray(ar, 10);
printArray(ar, 10);
int num;
cout << "Enter a number to divide by" << endl;
cin >> num;
vector<int> results; //<-
int count = getDivisible(ar, 10, num, results); //<-
cout << " There are " << count << " evenly divisible numbers. They are:" << endl;
for (auto i : results) // remember to compile with -std=c++1y or similar option
{
cout << i << '\t';
}
cout << endl << endl;
return 0;
}
void fillArray(int ar[][10], int size)
{
for (int row = 0; row < size; row++)
{
for (int col = 0; col < 10; col++)
{
ar[row][col] = rand() % 101;
}
}
}
void printArray(int ar[][10], int size)
{
for (int row = 0; row < size; row++)
{
for (int col = 0; col < 10; col++)
{
cout << ar[row][col] << "\t";
}
cout << endl;
}
}
int getDivisible(int ar[][10], int size, int num, vector<int> &results)
{
int count = 0;
for (int row = 0; row < size; row++)
{
for (int col = 0; col < 10; col++)
{
if ((ar[row][col]) % num == 0)
{
results.push_back(ar[row][col]); //<-
count++;
}
}
}
return count;
}
|