count and list a divisible number in array

Hi. I need to create an array 10 x 10 numbers from 1-1000. Then enter a number that is evenly divisible. Then print a single dimension array to evenly divisible numbers. I found a solution on this board but they used vectors which we have not covered in class. I am stuck on the getDivisible and printarray of divisible number part.

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
#include <iostream>

using namespace std;

void fill2DArray(int a[][10], int size);
void print2DArray(int a[][10], int size);
int getDivisible(int ar[][10], int size, int num);
void printArray(int a[], int size);

int main()
{
	int ar[10][10];
	int count = 0;
	fill2DArray(ar, 10);
	print2DArray(ar, 10);
	printArray(a, 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 fill2DArray(int a[][10], int size)
{
	for (int row = 0; row < 10; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			a[row][col] = rand() % 1000 + 1;
		}
	}
}

void print2DArray(int a[][10], int size)
{
	for (int row = 0; row < 10; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			cout << a[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;
}

void printArray(int a[], int size)
{
	for (int i = 0; i < 10; i++)
		cout << a[i] << endl;
}
bump
I see the suggestion to make full use of the function parameters hasn't taken hold. Example:

1
2
3
4
5
void printArray(int a[], int size)
{
	for (int i = 0; i < 10; i++)
		cout << a[i] << endl;
}

Rather than ignore the valuable information of the size parameter, make use of it:
1
2
3
4
5
void printArray(int a[], int size)
{
	for (int i = 0; i < size; i++)
		cout << a[i] << endl;
}


As for the problem itself. In theory, all of the numbers might be divisible by the number entered. Thus you need a 1-D array capable of holding 10*10 = 100 elements.
Pass that array as an additional parameter to the function getDivisible() and store the relevant values as they are identified. You appear to correctly return the count of elements found. That is important, because you will need that count value when calling the function to print the contents of the array - you don't want to print all 100 elements, just those which were previously stored. (edit: actually, in the original program, the value of count is discarded, the function returns it properly, but when it is called from main() nothing is done with the value).

My version of that function looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int getDivisible (int a[][SIZE], int size, int num, int b[])
{
    int count = 0;
    for (int row = 0; row < size; row++)
    {
        for (int col = 0; col < size; col++)
        {
            if (a[row][col] % num == 0)
            {
                b[count] = a[row][col];
                count++;
            }
        }
    }
    return count;
}


Note, right at the start of the program, before main() and the other functions, I defined a constant:
 
const int SIZE = 10;

This means there is no need to have the number 10 scattered in multiple places throughout the program. That's really useful if you need to change it to another value, say 3 or 15 or whatever you like.

Also note SIZE and size are two different things. SIZE is the constant while size is an ordinary variable. Originally I had HEIGHT and WIDTH as two separate constants, but that was probably overkill here.
Last edited on
Topic archived. No new replies allowed.