2 dimensional array issues

Ok, I have an issue. I'm stuck. Here is the problem specifications

"Code a program that displays the number of times a value appears in a 2 dimensional array. Create the program so that it displays the number of times each of the numbers from 1 through 9 appear in the numbers array."
Additional information
- use a 1 dim array of counter variables for the numbers 1 - 9
- use three for loops
- use an if statement
- array name must be arrayNumbers

my code so far follows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	//declare numbers array
	int arrayNumbers[5][3] = {{1, 2, 7} , {2, 5, 3}, {1, 9, 4}, {2, 6, 5}, {7, 2, 2}};
	int searchFor[9]={1,2,3,4,5,6,7,8,9};
	int timesOccured = 0;
		for(int row = 0; row < 5; row++)
			for(int column = 0; column < 3;column++)
				if (arrayNumbers[row][column] == searchFor[sub])
				timesOccured++;	
	system("pause");
	return 0;
}	//end of main function 


I understand that I need to search the arrayNumbers array and compare it to the subs in searchFor.
Beyond this.. I'm lost.
Can someone point me in the right direction?
1
2
3
4
5
	int searchFor[9] = {};
		for( int row = 0; row < 5; row++)
			for( int column = 0; column < 3;column++ )
				if ( arrayNumbers[row][column] > 0 &&  arrayNumbers[row][column]  <= 9 )
				searchFor[arrayNumbers[row][column] - 1]++;	
Where would the third for loop come into play?
I would assume it is to for displaying the number of times each number is in the array.
I do not know where the third loop is needed! :)
Maybe it should be used to output the frequencies of numbers?
Gotcha, that was my next question. Is there another way to write that if statement?
I'm a bit lost as to how that if statement is searching arrayNumbers and comparing it to the subscript of searchFor.
The if statement check whether the current element of the two-dimensional array is in the range of [1, 9]. If so then the corresponding element of ythe one-dimensional array is incremented.
ok, with your help, here is where I've gotten to but a desk check of the program yields incorrect results..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Advanced24.cpp - Displays the number of times
//the numbers from 1 through 9 appear in a
//two-dimensional array
//Created/revised by <your name> on <current date>

#include <iostream>
using namespace std;

int main()
{
	//declare numbers array
	int arrayNumbers[5][3] = {{1, 2, 7} , {2, 5, 3}, {1, 9, 4}, {2, 6, 5}, {7, 2, 2}};
	int searchFor[9]={1,2,3,4,5,6,7,8,9};
	int timesOccured = 0;
	for( int row = 0; row < 5; row++)
			for( int column = 0; column < 3;column++ )
				if ( arrayNumbers[row][column] > 0 &&  arrayNumbers[row][column]  <= 9 )
				searchFor[arrayNumbers[row][column] - 1]++;	
	timesOccured++;
	for (int sub = 1; sub < 9; sub++)
		cout << sub << " occured " << timesOccured << "times." << endl;
	system("pause");
	return 0;
}	//end of main function 
In my code there was

int searchFor[9] = {};

instead of

int searchFor[9]={1,2,3,4,5,6,7,8,9};

Also the loop shall be

for (int sub = 0; sub < 9; sub++)
cout << sub + 1 << " occured " << timesOccured[sub] << "times." << endl;
Last edited on
gotcha, since you did the > 0 and < 9 I suppose that takes care of that. I am still unable to figure out which variables to include in my cout statement.
See my previous post. I have updated it.
timesOccured is not an array, why did you use it like an array in the cout statement? I was using timesOccured as the increment counter ...
Perhaps I should clarify. An added specification to the problem was that it should be like the assignment the previous week which did the same thing only for a 1 dimensional array.
Here is my code for that assignment
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
Roy Hammond
Source code
Chapter 11, exercise 27
//Advanced27.cpp - displays the number of students
//earning a specific score
//Created/revised by Roy Hammond on 11-19-12

#include <iostream>
using namespace std;

int main()
{
	//declare array and variables
	int arrayScores[200] = {10, 50, 54, 50, 10, 75, 67, 89, 99, 100, 34, 99, 50, 34, 72, 88, 5, 
					        97, 76, 73, 72, 56, 73, 50, 5, 10, 50, 20, 86, 99, 50, 20, 5, 42, 68, 77, 34, 22,
					        23, 55, 20, 10, 5, 66, 88, 5, 88, 66, 43, 50, 47, 50, 55, 54, 22, 20, 5, 33, 55,
						    33, 44, 55, 66, 23, 5, 20, 50, 20, 55, 100, 99, 100, 23, 50, 66, 23, 55, 20, 5,
						    20, 10, 100, 5, 88, 100, 99, 20, 34, 55, 5, 10, 20, 10, 50, 23, 77, 10, 5, 55, 50,
						    5, 34, 55, 60, 100, 70, 75, 50, 23, 55, 20, 60, 100, 2, 5, 45, 49, 50, 100, 50, 74,
						    59, 70, 100, 10, 60, 23, 5, 100, 10, 5, 100, 77, 55, 43, 20, 50, 100, 66, 100, 77,
						    100, 5, 90, 10, 50, 10, 100, 55, 60, 45, 50, 11, 10, 55, 5, 11, 100, 50, 98, 85, 
						    5, 20, 10, 10, 50, 100, 20, 50, 53, 20, 100, 59, 20, 80, 90, 45, 100, 10, 59, 88, 90,
						    100, 50, 20, 100, 10, 55, 10, 100, 20, 54, 55, 100, 60, 10, 5, 10, 5, 88};

	int searchFor = 0;
	int numberScored = 0;

	//get score to search for
	cout << "Please enter a test score or enter -1 to end the program:";
	cin >> searchFor;
	system ("cls");
	while (searchFor > -1)
	{
		for (int sub = 0; sub < 200; sub += 1)
			if (arrayScores[sub] == searchFor)
				numberScored += 1;
		//end if

		//display the results
		cout << numberScored << " students scored " << searchFor << "." << endl;

		//get a new score
		cout << "please enter another score to search for:" ;
		cin >> searchFor;
		numberScored = 0;
		system ("cls");
	}	//end while
	
	system("pause");
	return 0;
}	//end of main function
I am sorry. You shall use searchFo instead of timesOccured. This variable is unnecessary..
Topic archived. No new replies allowed.