count how many times a number has been used in a column

Hi;
(using C++) I am trying to count how many times a number is used in a column,
example:
23 45 67
45 67 89
23 45 67
this is the file I would like to count how many times in the first column the numbers are used in this case the result would be 23=2 45=1. 2nd column 45=2 67=1. 3rd column 67=2 89=1
the file I have is much larger.
this isn't school work this is something I am doing on my own.
thanks
so much
happy
Last edited on
Are those numbers in an array or... is it just a plain text file with numbers?
I'm new to C++ but not new to programming.

-If you mean the numbers are in a text file, seperated by white space, you could read it using a read-file function and put it into a multi-dimension array.

example:
1
2
3
4
5
ARRAY = array(
  array(23,45,67),
  array(45,67,89),
  array(23,45,67),
)


And this little PHP-like/javascript-like code will count the numbers in each column, storing the results in an multi-dimension array.

1
2
3
4
5
6
7
8
9
10
11
COUNTED_ARRAY = new Array();
ARRAY = ... //the array with numbers
for ( a = 0; a < ARRAY.length ; a++ ) {
    for ( b = 0; b < ARRAY[a].length ; b++ ) {
        if(isSet(COUNTED_ARRAY[b][ ARRAY[a][b] ])) {
            COUNTED_ARRAY[b][ ARRAY[a][b] ] ++;
        } else {
            COUNTED_ARRAY[b][ ARRAY[a][b] ] = 1;
        }
    }
}


Result should be this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Array (
  Array(
    [23] = 2;
    [45] = 1;
  )
  Array(
    [45] = 2;
    [67] = 1;
  )
  Array(
    [67] = 2;
    [89] = 1;
  )
)


the example is not compilable with C or C++.], so you have to'create a simular function in C++.
If you put $ in front of the variable names, this example should work in PHP.

good luck
Thank you gamebuster
and yes I have the numbers in a text file so the code will be reading that infile. The result you show is want I am looking for.
thank you
happy
Topic archived. No new replies allowed.