counting a divisible number in array

Hello, I need to write a program that will create a 2d array and then allow the user to input a number, from there I need to count and list all the numbers in that array that are evenly divisible by the user input. It doesn't work. This is what I have so far:

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
 void fillArray(int ar[][10], int size);
void printArray(int ar[][10], int size);
int getDivisible(int a[][10], int size, int num);


int main()
{
	srand((unsigned int)time(0));
	int ar[10][10];
	int count = 0;
	fillArray(ar, 10);
	printArray(ar, 10);

	int num;
	cout << "Enter a number to divide by" << endl;
	cin >> num;

	getDivisible(ar, 10, count);
	cout << " There are " << count << " evenly divisible numbers. They are : " << endl;

	cout << 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)
{

	int count = 0;
	for (int row = 0; row < size; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			if ((ar[row][col]) % num == 0)
				count++;
		}

	}
	return count;
} 


Last edited on
Basically, you need to pass the number from the user to getDivisible(),
so line 18 should be getDivisible(ar, 10, num);
Then you need to use the count returned from getDivisible(),
so line 18 really should be count = getDivisible(ar, 10, num);

That's it really, but I noticed you're trying to print pout the list of divisible numbers as well, you'll need to capture those numbers and return them from getDivisible(). I tweaked a little, adding a vector to store the divisible numbers:

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;
} 

Thank you tipaye for your help and time, and for posting your own code so I can study it step by step. I get what I was doing wrong now.
Last edited on
Topic archived. No new replies allowed.