1. ## Problem with looping.

Hi all.
I am having a problem with my loop.
I need anyone here explain to me whats going on with the code below.

Input euc_total[cluster][i]:
1.0000 0.0000 2.8284 4.2426
5.0000 4.2426 1.4142 0.0000
Code:
```void Get_Min_Distance()
{
int clust = 0, doc = 0, doc_cluster = 0, doc_count = 0;
double min = 0.0;

for (int i = 0; i < ROW; i ++ )
{
min = euc_total[0][i];
for (int cluster = 0; cluster < cluster_num; cluster++)
{
if (euc_total[cluster][i] < min)
{
min = euc_total[cluster][i];
clust = cluster;
printf("\nmin= %.4f cluster=%d",min,cluster);//1
}
}
printf("\n%.4f -> Document [%d] in cluster [%d]", min, i, clust);//2
}
}```
In the function above, I have 2 printf.
The first printf will display the "min" value and "cluster" value.
The problem is, it will display cluster 1 only, and I am wondering why it doesnt display starting with cluster 0.

Output:
min= 1.4142 cluster=1
min= 0.0000 cluster=1

The second printf has no such problem. It displays starting from cluster 0.

Output:
1.0000 -> Document [0] in cluster [0]
0.0000 -> Document [1] in cluster [0]
1.4142 -> Document [2] in cluster [1]
0.0000 -> Document [3] in cluster [1]

I am thinking why the first printf cant display cluster starting with 0 while the second printf can do that??

Anyone..pls explain.
And how to solve this problem.

2. Looks like all your indices are swapped.

Say
min = euc_total[i][0];

if (euc_total[i][cluster] < min)

3. Do I need to change the 2D array

[cluster][i] to [i][cluster ??

4. Well without knowing how you declared your arrays, or how you stored data in the array, who knows what the right answer should be.

All I'm suggesting is that you appear to be summing columns instead of rows.

Perhaps changing your loop variables to be row,col instead of i,cluster would make the code more readable.

> for (int i = 0; i < ROW; i ++ )
The for ( int style of declaration needs either a C++ compiler, or one of the rare C99 compilers.

5. Are there actually any C99 compilers?

6. gcc conforms mostly to C99, possibly more than any other compiler:

http://gcc.gnu.org/c99status.html

I wouldn't call gcc rare, Salem

7. Originally Posted by cwr
gcc conforms mostly to C99, possibly more than any other compiler:

http://gcc.gnu.org/c99status.html

I wouldn't call gcc rare, Salem
And neither one of us would call code that mostly followed the standard as ANSI code.

Quzah.

8. gcc allows a lot of C99 stuff (and a lot of GNU specific stuff) unless you specifically state that you want it to be C89.

As such, a lot of stuff tends to creep in without you being aware of it only to present a portability problem later on (if not with the code, then with the grey stuff between your ears which suddenly realises it no longer knows ANSI-C but just another dialect of C).

9. What does gcc mean?

10. Originally Posted by Alexthunder
What does gcc mean?