Subj : Re: any function to handle this kind of counting? To : comp.programming From : Arthur J. O'Dwyer Date : Fri Jul 29 2005 05:21 pm On Fri, 29 Jul 2005, Ross wrote: > > i have generated a 2000 X 1 vector [of integers] > and this vector indeed has a corresponding vector [of letters] > and then i want to count, say, for "1", there are two A's, one E, ... > > it seems that a for loop can do the job but as we don't know how many > instances there are, i.e. don't know A, C, E and then what will occur in > this corresponding vector, is there any function which is devoted to handle > this kind of counting problem? thx!! The simple answer [UNTESTED CODE], in C: int how_many(const int *idxs, const char *chs, size_t n, int idx, char ch) { int i; int count = 0; for (i=0; i < n; ++i) if (idxs[i] == idx && chs[i] == ch) ++count; return count; } If you're looking for a more efficient answer (e.g., make a table of all (idxs * chs) possible queries and index into that), you're going to have to be more specific about what your input is. What are the ranges of your lists of numbers and letters? 1 to 10? A to Z? Arbitrary strings? Once you've nailed that down, it should be easy for you to come up with the algorithm for making the table on your own. (Which is a roundabout way of saying I think it's a homework problem.) HTH, -Arthur .