### C++; 2D arrays? Plot points for graph

Okay, I need some help finding the ---location--- of any value divisible by 3 in my createCoords function. And I know that the value "y" in my main function isn't set to a value so the code doesn't compile correctly; can I fix this so that it reads any value that's divisible by 3? Can someone give me a hint as to what I should do? I'm having some trouble with this. Thank you.

I'm trying to do this one function at a time, so ignore fillLocations for now.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` ``````#include #include using namespace std; void fill2DArray(int ar[][20], int numRows); void print2DArray(const int ar[][20], int numRows); int createCoords(int ar[][20], int size, int x); void fillLocations(); int main() { srand((unsigned int) time(0)); int ar[20][20]; fill2DArray(ar, 20); print2DArray(ar, 20); int y; cout << y << " was found " << createCoords(ar, 20, y) << " times" << endl; return 0; } void fill2DArray(int ar[][20], int numRows) //fills 20x20 2D array with random #s (1-100) { for(int row = 0; row < numRows; row++) { for (int col = 0; col < 20; col++) { ar[row][col] = rand() % 101; } } } void print2DArray(const int ar[][20], int numRows) //prints array { for(int row = 0; row < numRows; row++) { for(int col = 0; col < 20; col++) { cout << ar[row][col] << "\t"; } cout << endl; } } int createCoords(int ar[][20], int size, int x) //searches numbers divisible by 3, return # of values { int y = 0; for(int row = 0; row < size; row++) { for(int col = 0; col < 20; col++) { if((ar[row][col] == x)) { if(x % 3 == 0) y++; } } } return y; } void fillLocations() //fills in locations with -1 { }``````
Last edited on
assuming x equals 3

 ``123456789101112131415161718`` ``````int createCoords(int ar[][20], int size, int x) //searches numbers divisible by 3, return # of values { int y = 0; for(int row = 0; row < size; row++) { for(int col = 0; col < 20; col++) { if((ar[row][col] % x) == 0) { y++; } } } return y; }``````
Okay, thank you, Yanson, but do you know how I could find the location of the numbers divisible by 3? And specifically name the locations? I really don't even know how to start.

New code:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````#include #include using namespace std; void fill2DArray(int ar[][20], int numRows); void print2DArray(const int ar[][20], int numRows); int createCoords(int ar[][20], int size, int x); void fillLocations(); int main() { srand((unsigned int) time(0)); int ar[20][20]; fill2DArray(ar, 20); print2DArray(ar, 20); int y = 3; cout << "Numbers divisible by " << y << " were found " << createCoords(ar, 20, y) << " times" << endl; return 0; } void fill2DArray(int ar[][20], int numRows) //fills 20x20 2D array with random #s (1-100) { for(int row = 0; row < numRows; row++) { for (int col = 0; col < 20; col++) { ar[row][col] = rand() % 101; } } } void print2DArray(const int ar[][20], int numRows) //prints array { for(int row = 0; row < numRows; row++) { for(int col = 0; col < 20; col++) { cout << ar[row][col] << "\t"; } cout << endl; } } int createCoords(int ar[][20], int size, int x) //searches numbers divisible by 3, return # of values { int y = 0; for(int row = 0; row < size; row++) { for(int col = 0; col < 20; col++) { if((ar[row][col] % x) == 0) { y++; } } } return y; } void fillLocations() //fills in locations with -1 { }``````
To help log my coordinates, I'd like to use fillLocations to fill the locations in the array I've logged with -1.

For some reason, it's not printing out -1 for the coordinates that are divisible by 3. Any ideas? Thank you.

My attempt:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````#include #include using namespace std; void fill2DArray(int ar[][20], int numRows); void print2DArray(const int ar[][20], int numRows); int createCoords(int ar[][20], int size, int x); int fillLocations(int ar[][20], int a, int b); int main() { srand((unsigned int) time(0)); int ar[20][20]; int y = 3; fill2DArray(ar, 20); print2DArray(ar, 20); fillLocations(ar, 20, y); cout << "Numbers divisible by " << y << " were found " << createCoords(ar, 20, y) << " times" << endl; return 0; } void fill2DArray(int ar[][20], int numRows) //fills 20x20 2D array with random #s (1-100) { for(int row = 0; row < numRows; row++) { for (int col = 0; col < 20; col++) { ar[row][col] = rand() % 101; } } } void print2DArray(const int ar[][20], int numRows) //prints array { for(int row = 0; row < numRows; row++) { for(int col = 0; col < 20; col++) { cout << ar[row][col] << "\t"; } cout << endl; } } int createCoords(int ar[][20], int size, int x) //searches numbers divisible by 3, return # of values { int y = 0; for(int row = 0; row < size; row++) { for(int col = 0; col < 20; col++) { if((ar[row][col] % x) == 0) { y++; } } } return y; } int fillLocations(int arr[][20], int a, int b) //fills in locations with -1 { int x = -1; createCoords(arr, a, b); if(b == 3) return x; }``````
Last edited on
Topic archived. No new replies allowed.