Reputation: 1
I am just starting out in C++ and here is the issue I am having. 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 number in that array that are evenly divisible by the user input. I have not started the list portion of the code yet, but the issue I am having is that the counting function I am trying to use is dividing by zero so it will not run. Here is what I have so far and any help will be greatly appreciated
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;
}
Upvotes: 0
Views: 796
Reputation: 103693
getDivisible(ar, 10, count);
You didn't mean to pass count
in there, did you?
Because when you get here, in the getDivisible
function:
if ((ar[row][col]) % num == 0)
That's a problem, because num
in getDivisible
is count
from main
, which is 0
.
Upvotes: 3