numbers array

Hey I am having trouble converting this program to display the number of times each of the numbers from 1 through 9 appears in numbers array. the book says use a one-dimensional array of counter variables but I can't figure it out.


#include <iostream>
using namespace std;

int main()
{
//declare numbers array
int numbers[5][3] = {{1, 2, 7} , {2, 5, 3}, {1, 9, 4}, {2, 6, 5}, {7, 2, 2}};


system("pause");
return 0;
} //end of main function
Use a one dimensional array of counter variables. The array should be of size 10 to accommodate digits 0 to 9. Each time you come across a digit, increment the related counter array index. You'll need nested for loops to explore your given two dimensional array, and then another for loop after them to show the results.

Or let's just plain out give a full program as rocketboy9000 did below...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main(){
	//declare numbers array
	int numbers[5][3] = {{1, 2, 7} , {2, 5, 3}, {1, 9, 4}, {2, 6, 5}, {7, 2, 2}};
	int i;
	int count[10]={0,0,0,0,0,0,0,0,0,0};
	for(i=0;i<5*3;i++)count[numbers[i%5][i/5]]++;
	for(i=0;i<10;i++)cout << i << " occurred " << count[i] << " times\n";
	cin.getline(NULL,0); 
	return 0;
} //end of main function 
Thanks a lot that program worked great!
Topic archived. No new replies allowed.