Thread: Problem with looping.

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    5

    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.

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Looks like all your indices are swapped.

    Say
    min = euc_total[i][0];

    if (euc_total[i][cluster] < min)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    Do I need to change the 2D array

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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Are there actually any C99 compilers?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    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. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16
    What does gcc mean?
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Alexthunder
    What does gcc mean?
    http://www.google.com/search?q=gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM