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:
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.