2D Array

Hey guys! I am working on arrays and I pretty much almost done if I can only solve the problem with my code, it builds but crashes when the user enter a number to divide by. I am not sure why so I need some suggestions with that then I need to call getDivisible function to create the the list of evenly number values of the user input. Then cout to the screen the values were found divisible and then call printArray function to list all the evenly divisible numbers. Any hints, clues or suggestions will be appreciated. Here is my code:

#include <iostream>
#include <ctime>

using namespace std;

void fill2Darray(int ar[][10], int size);
void print2Darray(const int ar[][10], int size);
int getDivisible(int ar[][10], int size, int num);
void printArray(int ar[][10], int size);

int main()
{
int ar[10][10];

fill2Darray(ar, 10);
print2Darray(ar, 10);

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

cout << "There are " << getDivisible(ar, 10, userInput) << " even divisible number. They are: " << endl;
printArray(ar, 10);

return 0;
}
void fill2Darray(int ar[][10], int size)
{
srand(unsigned int(time(0)));
for(int row = 0; row < 10; row++)
{
for(int col =0; col < 10; col++)
{
ar[row][col] = rand() % 1001;
}
}
}
void print2Darray(const int ar[][10], int size)
{
for (int row = 0; row < 10; 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 counter = 0;
for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 10; col++)
{
if ((ar[row][col]) % num == 0)
counter++;
}
}
return counter;
}
void printArray(int ar[][10], int size, int num)
{
int counter = 0;
for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 10; col++)
{
cout << ar[num] << endl;
}
}
}
Last edited on
1
2
3
4
for (int col = 0; row < 10; col++)
                  ^^^^^^^

row never changes in the inner loop and so the it counts off the end of the array.  this should be col


Last edited on
I might make sure they don't enter 0 as the number to divide by.
Last edited on
Whoops! thanks jonnin for pointing that out!. Now it runs but I am still working on the last function which is printArray that will print all the divisible numbers from getDivisible.

Edit: I already figured it out thanks for all the help!
Last edited on
Topic archived. No new replies allowed.