generate all possible combinations of array values in C - c

I have an array of N values from 0 to k (0 <= k <= N), I need to generate all possible combinations of N values
void generate(int n, int k) {
int q = -1;
char res = '|';
int r;
int j;
for (j = 1; j <= n; j++) {
q = j / (k + 1);
r = j % (k + 1);
printf("%d %c", r, res);
}
}
int main() {
int n = 2;
int k = 2;
int i, nbr_comb;
nbr_comb = pow((k + 1), n); number of combinations
for (i = 0; i < nbr_comb; i++) {
generate(n, i);
printf("\n");
}
return (EXIT_SUCCESS);
}
for this test (N=2 K=2) I had those combinations
0 |0 |
1 |0 |
1 |2 |
1 |2 |
1 |2 |
1 |2 |
1 |2 |
1 |2 |
1 |2 |
you see that it begins to generate but it fixed at a point, I can't find why ! ?
Expected Examples:
for n=2 k=2 n=3 k=2
0 0 0 0 0
0 1 0 0 1
0 2 0 0 2
1 0 1 0 0
1 1 1 0 1
1 2 1 0 2
2 0 1 1 0
2 1 1 1 1
2 2 1 1 2
1 2 0
1 2 1
1 2 2
2 0 0
.....

This is how your loops unfurl at n=2, k=2:
for (i=0; i<nbr_comb; i++)
i=0: generate(2,0) --> j=1: 1 mod 1 = 0
j=2: 2 mod 1 = 0
i=1: generate(2,1) --> j=1: 1 mod 2 = 1
j=2: 2 mod 2 = 0
i=2: generate(2,2) --> j=1: 1 mod 3 = 1
j=2: 2 mod 3 = 2
i=3: generate(2,3) --> j=1: 1 mod 4 = 1
j=2: 2 mod 4 = 2
i=4: generate(2,4) --> j=1: 1 mod 5 = 1
j=2: 2 mod 5 = 2
i=5: generate(2,5) --> j=1: 1 mod 6 = 1
j=2: 2 mod 6 = 2
i=6: generate(2,6) --> j=1: 1 mod 7 = 1
j=2: 2 mod 7 = 2
i=7: generate(2,7) --> j=1: 1 mod 8 = 1
j=2: 2 mod 8 = 2
i=8: generate(2,8) --> j=1: 1 mod 9 = 1
j=2: 2 mod 9 = 2
As you can see, your j for-loop in generate() just keeps calling modulo on j, the result of which will always be j once argument k is greater than j.
What you need is a nested for-loop that will take the current combination (range [0..(k+1)^n]) and the current array index (range [0..n-1]) into consideration when it decides which value to print from the set of [0..k].
If you think of the output as rows and columns, then in the right-most column, the value should change on each row, iterating from 0..k. In next column, the value should change every (k+1)th row. In next column, the value should change every (k+1)^2 row.
For example, when n = 3 and k = 2, then for the first 9 rows, the right-most column should look like 0,1,2,0,1,2,0,1,2. The middle column should look like 0,0,0,1,1,1,2,2,2. The left-most column should look like 0,0,0,0,0,0,0,0,0.
Thus, you end up with something like this:
int n = 2;
int k = 2;
int row, col;
int cell;
int rdiv;
int nbr_comb = pow(k+1, n);
for (row=0; row < nbr_comb; row++)
{
for (col=n-1; col>=0; col--)
{
rdiv = pow(k+1, col);
cell = (row/rdiv) % (k+1);
printf("%d |", cell);
}
printf("\n");
}

Related

efficient way to count adjacent empty points of a connected group on a grid

A connected group means a set of vertices of equal values on a grid being adjacent horizontally or vertically.
For example, on this grid where . is an empty point, there are 4 connected groups.
X O O . X O O
. X O X -> X O X
X X O X X X O X
You can also see that each group has 1 connected empty point.
I'm trying to count the number of these connected empty points given a grid and the coordinates that point to a vertex of a group.
If the input is,
. . X X X O O .
X X . X . X O X
. X X X X X O X
X X . O O X . X
(0, 2)
The output should be 7 because the big group including the vertex at (0, 2) (row, column) has 7 connected empty points.
If you are familiar of the game of Go (baduk), connected empty point is in other words liberty. This operation is the core of a routine analyzing a position in Go, and it has to be done fast.
Below is my try. It's terribly inefficient in many ways involving a lot of branches and recursion. I'm tracking the 4 possible directions and incrementing the count whenever there's an empty space, and put a mark on the vertex at which the counting has been done to not count twice.
Could you shed some light on how to efficiently solve this problem?
General algorithmic improvement and x86-AVX2-specific optimizations are both welcome.
typedef __attribute__((vector_size(32))) char vec8_c;
enum {H = 4, W = 8};
static int count_();
static int count__();
static int count(vec8_c x, int i, int j) {
vec8_c m = {0};
return count_((char *)&x, (char *)&m, i, j, i * W + j);
}
static int count_(char *x, char *m, int i, int j, int idx) {
m[idx] = 1;
int r = 0;
if (j - 1 >= 0) {
r += count__(x, m, i, j - 1, idx - 1, idx);
}
if (j + 1 < W) {
r += count__(x, m, i, j + 1, idx + 1, idx);
}
if (i - 1 >= 0) {
r += count__(x, m, i - 1, j, idx - W, idx);
}
if (i + 1 < H) {
r += count__(x, m, i + 1, j, idx + W, idx);
}
return r;
}
static int count__(char *x, char *m, int i, int j, int idx, int idx_) {
if (!m[idx]) {
if (!x[idx]) {
m[idx] = 1;
return 1;
} else if (x[idx] == x[idx_]) {
return count_(x, m, i, j, idx);
}
}
return 0;
}
Run online.
From the description I would convert the problem into intersection/union;
Make a mask C from the connected component
Make a mask E from the empty pixels
Make a larger mask M by concatenating the shifted version of C
M = C<<1 | C^^1 | C >> 1 | C^^-1
return PopCount(M & E)
This approach should easily vectorize and even autovectorize.
When C is large enough, use SIMD registers to work in blocks of 16x8, where each bit represents a boolean in the mask. One can then shift a whole block up/down by alignr / left/right with _mm_slli_epi16/_mm_srli_epi16 or their equivalents in AVX2/-512, where unfortunately the cross bank shifting is a bit costly.
For the specific inputs:
. . X X X O O . -> C = 0 0 1 1 1 0 0 0 E = 1 1 0 0 0 0 0 1
X X . X . X O X 1 1 0 1 0 1 0 0 0 0 1 0 1 0 0 0
. X X X X X O X 0 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0
X X . O O X . X 1 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0
Then the mask M would be the union of C shifted to left,right,up,down
M = 0 0 0 1 1 1 0 0 0 0
0 |1 1 1 1 1 1 0 0| 0 <-- you only need the
1 |1 1 1 1 1 1 1 0| 0 inner / 'valid' area
0 |1 1 1 1 1 1 1 0| 0
1 |1 1 1 1 1 1 1 0| 0
0 1 1 0 0 0 1 0 0 0
M.*E = 1 1 0 0 0 0 0 0
0 0 1 0 1 0 0 0
1 0 0 0 0 0 0 0
0 0 1 0 0 0 1 0, sum == 7

Compute recursion for one column conditionally on values in another columns

I was given dataset named Temp.dat with 2 columns (Dataset here). I initially formed structure named structure data_t data[100] so that I could arrange the columns in an increasing order based on the first column (Column 0 = min(failure time, censored time), Column 1 indicates 1 = Death observation, 0 = censored observation). A portion of the structured dataset has the following form
0.064295 1
0.070548 1
0.070850 1
0.071508 0
0.077981 1
0.086628 1
0.088239 1
0.090754 1
0.093260 0
0.094090 1
0.094367 1
0.097019 1
0.099336 1
0.103765 1
0.103961 1
0.111674 0
0.122609 0
0.123730 1
Now, I want to write the C code to form different time periods whose endpoints always end with entry 1in the 2nd column. Looks like the following:
Expected output - 3rd column (Time Interval) added
0.064295 1 [0 0.064295)
0.070548 1 [0.064295 0.070548)
0.070850 1 [0.070548 0.070850)
0.071508 0 [0.070850 0.077891) ---> Skip 0.071508 here because of 0 in column 1
0.077981 1 [0.070850 0.077981)
0.086628 1 [0.077981 0.086628)
0.088239 1 [0.086628 0.088239)
0.090754 1 [0.088239 0.090754)
0.093260 0 [0.090754 0.094090)
0.094090 1 [0.090754 0.094090)
0.094367 1 [0.094090 0.094367)
0.097019 1 [0.094367 0.097019)
0.099336 1 [0.097019 0.099336)
0.103765 1 [0.099336 0.103765)
0.103961 1 [0.103765 0.103961)
0.111674 0 [0.103961 0.123730)
0.122609 0 [0.103961 0.123730)
0.123730 1 [0.103961 0.123730)
So far, I am unable to write the code to perform this. So if anyone could help on this step, I would sincerely appreciate it.
Next, I wrote up the following code to get the output shown below. Note that column 2 is not what I want, but this is the best thing so far I could get.
double array[8][MAX];
double total = 100;
for(int i = 0; i < MAX; i++) {
double start = 0;
double count = 0;
if(i) start = data[i - 1].x;
array[0][i] = data[i].x;
array[1][i] = data[i].y;
array[2][i] = start;
array[3][i] = data[i].x;
array[4][0] = count;
array[5][0] = count;
array[6][0] = total;
array[7][0] = 1;
/*keep track of number of deaths and censors at each time t_i*/
if (fmod(arr[1][i], 2.0) == 1)
{arr[4][i+1] = count + 1.0;
arr[5][i+1] = count;
}
else {arr[4][i+1] = count;
arr[5][i+1] = count + 1.0;
}
return(0);
}
Sample Output
0.064295 1 [0.060493 0.064295) 1.000000 0.000000 191.000000 0.950000
0.070548 1 [0.064295 0.070548) 1.000000 0.000000 190.000000 0.945000
0.070850 1 [0.070548 0.070850) 1.000000 0.000000 189.000000 0.940000
0.071508 0 [0.070850 0.071508) 1.000000 0.000000 188.000000 0.940000
0.077981 1 [0.071508 0.077981) 0.000000 1.000000 187.000000 0.935000
0.086628 1 [0.077981 0.086628) 1.000000 0.000000 186.000000 0.929973
0.088239 1 [0.086628 0.088239) 1.000000 0.000000 185.000000 0.924946
0.090754 1 [0.088239 0.090754) 1.000000 0.000000 184.000000 0.919919
0.093260 0 [0.090754 0.093260) 1.000000 0.000000 183.000000 0.919919
Column 7 stands for KM estimator of survival distribution function. It was computed based on the following rules:
1. If the i-th entry in column 1 is 0, simply save the corresponding i-th entry in column 6 equal to the previous (i-1)th- entry in the same column.
2. If the i-th entry in column 1 is 1 but one or multiple successive entries before it is 0 (for example, the last entry of column 1 is followed right before by two 0s), we compute the corresponding i-th entry in column 6 with the formula: (i-1)-th entry*(1- 1/(j-th entry in column 5)) where the j-th entry in column 5 corresponds to the nearest entry 1 in column 1 (for example, the last 4 rows of column 1 has 1 0 0 1 in it, which implies the last entry in column 6 would be computed as 0.890096*(1-1/177) where 177 = the first entry in column 5 which has the corresponding entry in column 1 = 1 (rather than 0).
Task left to finish: First, I need to form the right column 2 so that for a random input t in the range of column 0, the code would give the corresponding result in column 6.
Second, I want to compute the variance of KM estimator, using this formula: S(t)^2*(summation over t_i <= t) d_i/(r_i*(r_i-d_i)),
where S(t) = the KM estimator computed at time t (column 7 above), d_i is the total number of deaths up to index i (so, sum of entries up to d_i of column 5 above), r_i = i-th entry in column 6. For example, if t = 0.071, then t_i only has 3 possible values based on Column 0 (t_i would be 0.064295, 0.070548 and 0.070850). I came up with the following working code (not sure if the output was the correct ones)
N = [an integer]; #define size of array here
double sigma[N];
sigma[0] = 0;
double sum[N];
sum[0] = 0;
for(int i=1; i< N; i++){
sum[i] = sum[i-1] + (float)(arr[4][i]/(arr[6][i-1]*(arr[6][i])));
sigma[i] = pow(arr[7][i],2)*sum[i];
printf("%.0lf", sigma[i]);
}
Sample Output
0.004775
0.004750
0.004725
0.004700
0.004675
0.004700
0.004650
0.004625
0.004600
0.004575
0.004600
0.004550
0.004525
0.004500
0.004475
0.004450
0.004425
0.004450
0.004450
0.004400
0.004375
0.004350
0.004325
0.004300
0.004275
0.004250
0.004225
0.004200
0.004175
0.004149
0.004124
0.004150
0.004099
0.004074
0.004100
0.004049
0.004024
0.004051
0.003999
0.003974
0.004001
0.003949
0.003976
0.003923
0.003898
0.003926
0.003873
0.003848
0.003823
0.003797
0.003772
0.003747
0.003775
0.003722
0.003750
0.003696
0.003725
0.003671
0.003700
0.003646
0.003676
0.003621
0.003595
0.003570
0.003544
0.003519
0.003549
0.003494
This is a partial answer. First, lets declare the array as arr[MAX][8], that means you have MAX rows and 8 columns. This makes it easier to sort the data.
Next, lets create dummy data 0.100, 0.101, ... that's easier to look at it.
To find the 5th column, you can use an additional loop (for(int j = i; j < count; j++){...}) to find the next non-zero value.
We have to keep track of total dead counts (dead_count) and increment each time arr[i][1] is zero.
Kaplan-Meier formula is taken as 1 - (double)dead_count/(double)count
MCVE would look like:
#include <stdlib.h>
#include <stdio.h>
int compare_2d_array(const void *pa, const void *pb)
{
double a = *(double*)pa;
double b = *(double*)pb;
if(a > b) return 1;
if(a < b) return -1;
return 0;
}
int main(void)
{
double arr[][8] =
{
{ 0.100, 1, 0, 0, 0, 0, 0 , 0 }, //initialize columns
{ 0.101, 1 }, // we can skip adding the zeros, it's done automatically
{ 0.102, 1 },
{ 0.103, 0 },
{ 0.104, 1 },
{ 0.105, 1 },
{ 0.106, 1 },
{ 0.107, 1 },
{ 0.108, 0 },
{ 0.109, 1 },
{ 0.110, 1 },
{ 0.111, 1 },
{ 0.112, 1 },
{ 0.113, 1 },
{ 0.114, 1 },
{ 0.115, 0 },
{ 0.116, 0 },
{ 0.117, 1 },
};
int count = sizeof(arr)/sizeof(*arr);
//sort
qsort(arr, count, sizeof(arr[0]), compare_2d_array);
int dead_count = 0;
for(int i = 0; i < count; i++)
{
double start = i ? arr[i - 1][0] : 0;
double end = arr[i][0]; //<- I don't know what to use as default value!
//if arr[i][1] is zero, then end should equal the next non-zero value
double end;
for(int j = i; j < count; j++)
{
end = arr[j][0];
if(arr[j][1])
break;
}
arr[i][2] = start;
arr[i][3] = end;
arr[i][4] = arr[i][1];
arr[i][5] = !arr[i][1];
if(!arr[i][1])
dead_count++;
printf("%3d %.6lf %.0lf [%.6lf %.6lf) %.0lf %.0lf %3d %.6lf\n",
i,
arr[i][0],
arr[i][1],
start,
end,
arr[i][4],
arr[i][5],
count - i, 1 - (double)dead_count/(double)count );
}
return 0;
}
Output:
0 0.100000 1 [0.000000 0.100000) 1 0 18 1.000000
1 0.101000 1 [0.100000 0.101000) 1 0 17 1.000000
2 0.102000 1 [0.101000 0.102000) 1 0 16 1.000000
3 0.103000 0 [0.102000 0.104000) 0 1 15 0.944444
4 0.104000 1 [0.103000 0.104000) 1 0 14 0.944444
5 0.105000 1 [0.104000 0.105000) 1 0 13 0.944444
6 0.106000 1 [0.105000 0.106000) 1 0 12 0.944444
7 0.107000 1 [0.106000 0.107000) 1 0 11 0.944444
8 0.108000 0 [0.107000 0.109000) 0 1 10 0.888889
9 0.109000 1 [0.108000 0.109000) 1 0 9 0.888889
10 0.110000 1 [0.109000 0.110000) 1 0 8 0.888889
11 0.111000 1 [0.110000 0.111000) 1 0 7 0.888889
12 0.112000 1 [0.111000 0.112000) 1 0 6 0.888889
13 0.113000 1 [0.112000 0.113000) 1 0 5 0.888889
14 0.114000 1 [0.113000 0.114000) 1 0 4 0.888889
15 0.115000 0 [0.114000 0.117000) 0 1 3 0.833333
16 0.116000 0 [0.115000 0.117000) 0 1 2 0.777778
17 0.117000 1 [0.116000 0.117000) 1 0 1 0.777778

Algorithm to fill 2D array in C

and sorry for the noob question.
So I've been asked to create an algorithm to fill a 2D array. They didn't say what the rules were, however, I've been told that the resulting matrix should look like this:
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
I haven't been told if the matrix is necessarily square, but the size may vary.
So substantially what I'm seeing is the matrix is vertically, horizontally and diagonally symmetrical.
Now whatever I try, it ends up being super complicated, while as I look at it, I feel like it should be pretty simple...
Any trick or snippet on how you'd do it?
Thanks in advance.
You need 2 nested loops, to traverse through rows and columns. The content of the field is the minimum of the control variables and and the minimum of the difference of a control variable and the size of the array dimension, incremented by 1.
N = 5
0: min(0, N-0-1) + 1 = 1
1: min(1, N-1-1) + 1 = 2
2: min(2, N-2-1) + 1 = 3
3: min(3, N-3-1) + 1 = 2
4: min(4, N-4-1) + 1 = 1
#include <stdio.h>
#define N 5
#define MIN(a,b) (((a)<(b))?(a):(b))
int main()
{
int a[N][N];
for ( int i = 0; i < N; ++ i )
{
for ( int j = 0; j < N; ++ j)
{
int minI = MIN(i, N-i-1);
int minJ = MIN(j, N-j-1);
a[i][j] = MIN(minI, minJ) + 1;
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
Output:
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
See the live example

Copy certain number from one matrix to another

Im using matlab and I have a matrix
1 1
2 1
3 1
4 2
5 2
6 2
7 1
8 1
9 2
10 2
11 2
How can i copy matrix from second column but only certain number? the other number will be random either 1 or 2. Example
1 1 1 | | 1 1 1
2 1 1 | | 2 1 1
3 1 1 | | 3 1 1
4 2 2 | | 4 2 2
5 2 1 | OR | 5 2 2
6 2 1 | | 6 2 1
7 1 1 | | 7 1 1
8 1 1 | | 8 1 1
9 2 2 | | 9 2 2
10 2 2 | |10 2 1
11 2 1 | |11 2 1
If the third row of 2 become 1, the rest of the column will become 1. process repeat until it reach another set of 2
You can use the logical indexing and the function randi:
a = [1 1;
2 1;
3 1;
4 2;
5 2;
6 2;
7 1;
8 1;
9 2;
10 2;
11 2];
b = randi(2,length(a),1); %generation of random value ∈ [1,2]
b(a(:,2)==1) = 1; %if a(:,2) = 1 b = 1;
a = [a,b]
A= [1 1
2 1
1 1
4 2
5 2
6 2
7 1
8 1
9 2
10 2
11 2] ;
colLength = length (A(:,1)) ;
thridcol = randi (2,colLength,1)
A(:,3) = thridcol ;
flag = 1 ;
i = 1 ; ;
if ( sum (A(3,:) == 1) == length (A(2,:)))
while (flag && i < colLength)
A(3+i,3 ) = 1 ;
if (sum (A(3+i,:) == 2) == length (A(3+i,:)))
flag = 0 ;
end
i = i +1 ;
end
end

Debugging a function in C

This function takes an integer array, the number of elements
in the array and tries to find a majority element in the
array. If the majority element exists, it is placed in
*result and the function returns true.
If no majority element exists, then the function returns
false. In that case, *result should not be used.
My output isn't working correctly for the program I'm writing and it is because of this findMajority function I think.
This is what the output is supposed to look like: http://pastebin.com/Q5ycXHrg
This is what my output looks like: http://pastebin.com/7P1ZTpML
This is the input:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3
1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1
1 2 3
1 1 1
1 2 1
1 2
1 1
2
1 1 1 1 2 3 4 5 6 7
Here is the function:
int findMajority(int *array, int count, int *result){
int i, counter, bcount = 0, ccount = 0, candidate, j;
if(count == 1) {
*result = *array;
return true;
}
if(count % 2 != 0 ) {
for(i = 0; i < count; i++) {
if(*(array + i) == *(array + count)) {
counter++;
}
}
if(counter > (count/2)) {
*result = *(array + count);
return true;
}
else {
*(array + count) = 0;
count--;
}
}
for(j=0; j <= count; j += 2) {
if(*(array + j) == *(array + (j + 1))) {
*(array + (count + 1)) = *(array + j);
bcount++;//how many numbers on the end of the array
}
}
if(bcount == 1) {
int k = count;
while(*(array + k) == 0) {
candidate = *(array + k);
}
}
else
findMajority((array + count), count, result);
for(j=0; j <= count; j += 2) {
if(*(array + j) == candidate) {
ccount++;
}
}
if(ccount > (count/2)) {
*result = candidate;
return true;
}
else
return false;
}
Your function has a lot of problem.
Without intialising counter you are incrementing it
check whether array[count] is the valid last element or array[count-1] is the correct one
In this code for(j=0; j <= count; j += 2){
if(*(array + j) == *(array + (j + 1))){
*(array + (count + 1)) = *(array + j);
bcount++;//how many numbers on the end of the array
}}
for count= 3 you are accessing array[4] array[5] etc.
And this is a infinite loop. you are not modifying condition variable inside the loop while(*(array + k) == 0) { candidate = *(array + k); }
I suggest you learn how to use a debugger to debug your program. For example, after wrapping your code with the following:
#include <stdio.h>
typedef enum { false, true } boolean;
// ((( your function here )))
int main(int argc, char* argv[])
{
int result = 0;
int number = 0;
int test[] = { 1, 1, 1, 1, 1, 1, 1 };
result = findMajority(&test[0], sizeof(test) / sizeof(int), &number);
printf("Result = %d, Number = %d\n", result, number);
return 0;
}
Assuming you put this into 'question.c' you could then issue the commands (assuming you have gcc and gdb):
$ gcc -g -o question question.c
$ gdb ./question
(gdb) b findMajority
Breakpoint 1 at 0x80483ea: file question.c, line 6.
(gdb) run
Starting program: ./question
Breakpoint 1, findMajority (array=0xbffff4bc, count=7, result=0xbffff4d8) at question.c:6
6 int i, counter, bcount = 0, ccount = 0, candidate, j;
You can then use the n command to step to the next line, and the p command to print variables to see what's going wrong. For example, you could find some of the problems that Toms pointer out relatively quickly:
39 while(*(array + k) == 0){
(gdb) n
40 candidate = *(array + k);
(gdb) n
39 while(*(array + k) == 0){
(gdb) n
40 candidate = *(array + k);
(gdb) n
39 while(*(array + k) == 0){
(gdb) n
There's your infinite loop.
(gdb) p counter
$3 = -1207959944
And there's your uninitialized counter.
Part of learning programming is figuring out strategies of determining just what went wrong. Some people like to use text-based debuggers like gdb. Some people like graphical debuggers like you could find in Eclipse CDT. Some people put printf() statements throughout their code.
Once you're really good, like Toms, you can just read it and rattle off the problems. ;-)

Resources