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.


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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:

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

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:

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
72
73
74
#include <iostream>
#include <ctime>

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.