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
|
#include <iostream>
#include <ctime>
using namespace std;
void fill2DArray(int ar[][20], int numRows);
void print2DArray(const int ar[][20], int numRows);
int createCoords(int ar[][20], int size, int x);
int fillLocations(int ar[][20], int a, int b);
int main()
{
srand((unsigned int) time(0));
int ar[20][20];
int y = 3;
fill2DArray(ar, 20);
print2DArray(ar, 20);
fillLocations(ar, 20, y);
cout << "Numbers divisible by " << y << " were found " << createCoords(ar, 20, y) << " times" << endl;
return 0;
}
void fill2DArray(int ar[][20], int numRows) //fills 20x20 2D array with random #s (1-100)
{
for(int row = 0; row < numRows; row++)
{
for (int col = 0; col < 20; col++)
{
ar[row][col] = rand() % 101;
}
}
}
void print2DArray(const int ar[][20], int numRows) //prints array
{
for(int row = 0; row < numRows; row++)
{
for(int col = 0; col < 20; col++)
{
cout << ar[row][col] << "\t";
}
cout << endl;
}
}
int createCoords(int ar[][20], int size, int x) //searches numbers divisible by 3, return # of values
{
int y = 0;
for(int row = 0; row < size; row++)
{
for(int col = 0; col < 20; col++)
{
if((ar[row][col] % x) == 0)
{
y++;
}
}
}
return y;
}
int fillLocations(int arr[][20], int a, int b) //fills in locations with -1
{
int x = -1;
createCoords(arr, a, b);
if(b == 3)
return x;
}
|