lets say i have 10 different integer: int1, int2, int3....
every integer can be either =0 or =1.
now i would like to know e.g. how many of them are =1 (are 5 =1 or 2 or all of them?).
how can i count how many integer are =0 or =1?
1. Define them as an array:
int test[10];
This means you now have 10 instances numbered 0 to 9. Access them like this:
test[3] = 1;
test[1] = 0;
test[7] = 15;
// etc...
2. To count their values, loop through them and check their values like this:
int num_var_equal_zero = 0;
int num_var_equal_one = 0;
int i = 0;
while ( i < 10 )
// loop through each variable of the array
{
if ( test[i] == 0 )
num_var_equal_zero++;
else if ( test[i] == 1 )
num_var_equal_one++;
i++;
}
After running the above piece of code, the variables should be counted and "num_var_equal_zero/one" should contain the number of variables out of the test[]-array that contained zero (or one respectively).
Any questions? :p
The easiest way in this case:
int how_many = int1 + int 2 + int3 + ... + int10;
In general, the following is better:
int i[10]; // declare i[0], i[1], i[2], ... i[9]
int how_many() {
int c, ret;
while(c < 10) {
if (i[c]) ret++;
c++;
}
return ret;
}
thanks. exacly wat i was looking for. :D